Android Programming

Android View

All android views are direct or indirect children of View.

The resides in the android.view package.

package android.view;

The View class implements 3 interfaces.

  1. Drawable.Callback
  2. AccessibilityEventSource
  3. KeyEvent.Callback
public class View implements Callback, android.view.KeyEvent.Callback, AccessibilityEventSource {}

Creation of Views

The android.view.View class provides us several constructors to create views.

All the public constructors for creation of views at least take a android.Content.Context object as a parameter.

No. Constructor
1. public View(Context context);
2. public View(Context context, AttributeSet attrs);
3. public View(Context context, AttributeSet attrs, int defStyleAttr);
4. public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes);

Let's see an example where we create a View programmatically and set it as the ContentView of our MainActivity.

package com.tutorials.hp.viewapp;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View myView=new View(this);
        myView.setBackgroundColor(Color.GREEN);
        setContentView(myView);
    }
}

RESULT Android View Example

In the example above we first instantiate our view object, passing in the constructor.

We then set the backgroundcolor of the view using the setBackgroundColor() method. We pass in an android.Graphics.Color object.

We then set the view as our ContentView using the SetContentView() method defined in the MainActivity.

Customizing Views

Once created views can be customized using various methods. We've just seen how to set it's backgroundcolor using the setBackgroundColor() method.

Below are some of the methods you can use to either get or set your customizations.

Method Return Type Description
setBackgroundColor(int color) void Sets the background color of the view.
setBackground(Drawable background) void Sets the passed drawable as the background of this view.
getBackground() Drawable Return the drawable used as the background of this view.
getHeight() int Returns the height of this view.
getWidth() int Returns the width of this view.
getContext() Context Returns the context this view is running on. You can then use the context to access current theme,resources etc.
setLayoutParams(LayoutParams params) void Sets the LayoutParams associated with this view. These parameters will provide specifications to the parent of how the view should be arranged.
getLayoutParams() LayoutParams Gets the LayoutParams associated with this view. These parameters will provide specifications to the parent of how the view should be arranged.
getParent() ViewParent Returns the parent of this view. This parent is a ViewParent object and not necessarily a View.
getRootView() View Returns the topmost view in the current view hierarchy.
Method Return Type Description
isClickable() boolean Returns whether this view can be clicked or not.
isLongClickable() boolean Returns whether this view can be long clicked.
isFocusable() boolean Returns whether this view can take focus.
setClickable(boolean clickable) void Sets this view to react to click events.
setLongClickable(boolean longClickable) void Sets this view to react to long click events.
setFocusable(boolean focusable) void Set this view to be able to take focus or not.