How to build dynamic Layouts in Android


Building layout with XML is easy. But sometimes you want to build a layout dynamically for fulfilling a particular need. 

In this Article, we are going to create a linear layout dynamically using java.

Linear Layout

Linear layout is used to arrange the component in a single row or single column.

How to build a linear layout

Android framework provides a class by the name LinearLayout which provides its constructor and some commonly used methods.

public LinearLayout(Context context);

Some commonly used methods of LinearLayout are:

  • setOrientation(int orientation) : This method is used to set the orientation of layout, i.e. whether the layout components are added in horizontal or vertical order.
  • setGravity(int alignment) : This method is used to set the alignment of the layout component. Ex. - Left, Right, Center, Bottom, Top.
  • setLayoutParams(param) : This method is used to set the width and height of your layout.

Steps involved in implementing layout dynamically using java

  • Create a new LinearLayout
     new LinearLayout(this);
  • Set its orientation to vertical.
    parent.setOrientation(LinearLayout.VERTICAL);
  • After that, set its parameters i.e width and height.
    LayoutParams param = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    	parent.setLayoutParams(param);
  • Now create a new TextView item and set its parameters. Then add its gravity. You can also choose what text to be displayed.

How to use setContentView()

While building layouts with XML, we link them with java using setContentView(R.id....) method. But when you are building layout dynamically using java then you just need to pass the layout of root element i.e. in our case:  setContentView(parent).

package com.example.dynamiclayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {
	LinearLayout parent;
	TextView name;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		parent = new LinearLayout(this);
		parent.setOrientation(LinearLayout.VERTICAL);
		LayoutParams param = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
	    parent.setLayoutParams(param);
	    name = new TextView(this);
	    name.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
	    name.setText("Created dynamically");
	    name.setGravity(Gravity.CENTER);
	    parent.addView(name);
	   setContentView(parent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

In this tutorial, we learned about developing layout dynamically using java.