Android View Introduction

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.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        View myView=new View(this);

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.

Methods Related to Events

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.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post