Android View Introduction


Android View Tutorial and Examples

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.

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.

Quick View Examples and HowTo's

1. How to Measure Width and Height of a View

Pass us a View object and we measure it's height and width.

    /** 
     * Measure the width and height of the View 
     */ 
    public static void measureWidthAndHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
    } 

2. How to Clear Text From Multiple Views

You pass us the views and we clear text from them.

    public static void clearText(final View... views) {
        for (final View view : views) {
            if (view instanceof FloatLabelTextViewBase) {
                clearText(((FloatLabelTextViewBase) view).getInput());
                continue;
            }
            if (view instanceof TextView) {
                ((TextView) view).setText(StringUtils.empty());
            }
        }
    }

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:




Recommendations


What do You Think


Previous Post Next Post