What is the addView method in android ?

posted on
by Mohamad Wael

The addView method is used to add a View programmatically to a ViewGroup . A ViewGroup can be for example a LinearLayout or a RelativeLayout .. A ViewGroup is itself a View . The addView method is overloaded .

The addView method

The

			
addView(View child, int index, LayoutParams params)
					

will add a child View to a ViewGroup , at the specified index with the given LayoutParams .

  • child is the View that we want to add to the ViewGroup . A ViewGroup can be for example LinearLayout or RelativeLayout . A ViewGroup is itself a View, so it can be added as a child to another ViewGroup . Other views can be are for example ImageView , TextView
  • The index is the position of the child in the ViewGroup . A child can be positioned first , second , third … last . The position starts at zero , so the first child has a position of zero . If we want to add a child at the last position we can specify -1 . The position cannot be larger than the number of children .
  • LayoutParams is the layout of the child in the ViewGroup . In Layout params we must specify the width and height of a View , and each ViewGroup has its own parameters that we can set. For example a LinearLayout has weight and gravity , a RelativeLayout has ALIGN_START , ALIGN_TOP

The

			
addView(View child, int index) 
					

method will call addView(View child, int index, LayoutParams params) , passing the child , the index or position where this child will be added , and the child LayoutParams.

it will try to get the layout parameter from this child by calling the child.getLayoutParams() method , if the child doesn't specify LayoutParams , it will try to get the ViewGroup default layout parameters by calling ViewGroup.generateDefaultLayoutParams() .

For example The LinearLayout.generateDefaultLayoutParams() method returns a default layout parameters when the orientation of the LinearLayout is vertical of [ width : match parent , height: wrap content] and it returns a default layout parameters of [width : wrap content , height : wrap content] when the orientation is horizontal.

The

			
addView(View child)
					

method will call the addView(View child, int index) method , by specifying that the child must be added at the last index which is -1 . The addView(View child, int index) method will try to get the child Layout parameter , and it will call addView(View child, int index, LayoutParams params) .

The

addView(View child, LayoutParams params) 
					

will call the addView(View child, int index, LayoutParams params) , by specifying that the child will be added last so at index -1, and by passing the layout parameters and the child.

The

addView(View child, int width, int height)
					

method will generate a default layout parameters , by calling ViewGroup.generateDefaultLayoutParams() , and it will set the width and height of the LayoutParams , for the passed width and height . After that it will call addView(child, -1, params) , which will add this child to the last position with the specified layout parameters.

This is an example that illustrates how to use the addView method.

package com.difyel.whatisaddview;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WhatIsaddViewActivity extends AppCompatActivity {

    LinearLayout linearLayout;
    TextView textView;
    LinearLayout.LayoutParams linearLayoutLayoutPrams;


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

        /*
         * create a linearLayout
         * set its orientation to vertical
         * */
        linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);



        /*create a   first TextView*/
        textView = new TextView(this);
        textView.setText("View added by setting the layout parameter in the view, no position specified so added at position -1");
        textView.setTextSize(22.f);
        textView.setTypeface(Typeface.DEFAULT , Typeface.BOLD);
        textView.setTextColor(Color.WHITE);
        textView.setPadding(0,20,0,20);
        textView.setBackgroundColor(Color.MAGENTA);


        //create a LinearLayout.LayoutParams
        // width is set to match parent
        // height is set to wrap content
        linearLayoutLayoutPrams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT );


        // set the textView layout parameter  using the layout parameter we created earlier linearLayoutLayoutPrams
        textView.setLayoutParams(linearLayoutLayoutPrams);

        //add this textview to the linearLayout using addView
        // it will be added at position -1 or the last child of linearLayout
        // the LayoutParameter that will be used are the one specified
        linearLayout.addView(textView );


        /*create a  second TextView*/
        textView = new TextView(this);
        textView.setText("View added without specifying any " +
                "layout parameter so addView will get the default layout parameter ,position  " +
                "specified -1 , so last. " +
                "there is one view before this one which was added by calling the addView method " +
                "so this view should be the second. But we have added a view with position 1 so this view" +
                " will be positioned at position 2 or the third view .");

        textView.setTextSize(22.f);
        textView.setTypeface(Typeface.DEFAULT , Typeface.BOLD);
        textView.setTextColor(Color.WHITE);

        textView.setPadding(0,20,0,20);
        textView.setBackgroundColor(Color.DKGRAY);


        //add this textview to the linearLayout using addView
        // it will be added at position -1 or the last child of linearLayout
        // the LayoutParameter are not specified so addView will get default layout parameter
        // vertical linear layout  , width match parent , height is wrap content
        linearLayout.addView(textView , -1 );



        /*create a  third TextView*/
        textView = new TextView(this);
        textView.setText("View added by specifying the layout parameter using addView , will be added at position -1");
        textView.setTextSize(22.f);
        textView.setTypeface(Typeface.DEFAULT , Typeface.BOLD);
        textView.setTextColor(Color.BLACK);
        textView.setPadding(0,20,0,20);
        textView.setBackgroundColor(Color.LTGRAY);


        //create a LinearLayout.LayoutParams
        // width is set to match parent
        // height is set to wrap content
        linearLayoutLayoutPrams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT );


        //add this textview to the linearLayout using addView
        // it will be added at position -1 or the last child of linearLayout
        // the LayoutParameter that will be used are the one specified
        linearLayout.addView(textView  , linearLayoutLayoutPrams);


        /*create a   fourth TextView*/
        textView = new TextView(this);
        textView.setText("View added by calling addView specifying the width and height, will be added at position -1");
        textView.setTextSize(22.f);
        textView.setTypeface(Typeface.DEFAULT , Typeface.BOLD);
        textView.setTextColor(Color.BLUE);
        textView.setPadding(0,20,0,20);
        textView.setBackgroundColor(Color.YELLOW);




        //add this textview to the linearLayout using addView
        // it will be added at position -1 or the last child of linearLayout
        // the addView method will create  the layout parameters ,
        // and it will set the width and height to the specified one
        // width and height are specified in pixel ,
        // pixel will look different in different screen
        // since it depends on screen density
        // pixels by default are specified for  the screen density of mdpi
        // so for screen density of xxhdpi we devide the pixels
        // by 3 , so here
        // @mdpi the width in pixel is 1000 and the height is 300
        // @xxhdpi the width is 333.33 and height is 100
        linearLayout.addView(textView  , 1000 , 300);



        /*create a  fifth TextView*/
        textView = new TextView(this);

        textView.setText("View added by specifying the index : 1  , and layout parameter  , so added as second child ");
        textView.setTextSize(22.f);
        textView.setTypeface(Typeface.DEFAULT , Typeface.BOLD);
        textView.setTextColor(Color.WHITE);
        textView.setPadding(0,20,0,20);
        textView.setBackgroundColor(Color.RED);


        //create a LinearLayout.LayoutParams
        // width is set to match parent
        // height is set to wrap content
        linearLayoutLayoutPrams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT );



        //add this textview to the linearLayout using addView
        // it will be added at position -1 or the last child of linearLayout
        // the LayoutParameter that will be used are the one specified
        linearLayout.addView(textView , 1, linearLayoutLayoutPrams);




        //set the content of the activity to the linearlayout
        // setContentView will give the linearlayout a default width of match parent
        // and a default height of match parent
        // so the linear layout will take all the activity .
        setContentView(linearLayout);

    }
}

					
android add view method example result

The source code of of this example can be found on Github