Learn How To Dynamically Create the Layout in Android

Introduction

 
This article explains how to dynamically create the layout in Android. Android Studio is used for the sample.
 
Typically we develop the layout for an Android application by creating the XML file. But in this, we will create the layout for the activity using code in the class file. In this, I have created four buttons inside a RelativeLayout that uses methods and variables to change their layout. First, you will create the Relativelayout and call LayoutParams to set the parameters as in the following:
  1. // Creating a new RelativeLayout  
  2. RelativeLayout relativeLayout = new RelativeLayout(this);  
  3.   
  4. // Defining the RelativeLayout layout parameters with Fill_Parent  
  5. RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); 
Now you will create the buttons that you want to show on screens as in the following:
  1. // Creating a new Left Button  
  2. Button button1 = new Button(this);  
  3. button1.setText("Button1");  
  4.   
  5. // Creating a new Left Button with Margin  
  6. Button button2 = new Button(this);  
  7. button2.setText("Button2");  
  8.   
  9. // Creating a new Center Button  
  10. Button button3 = new Button(this);  
  11. button3.setText("Button3");  
  12.   
  13. // Creating a new Bottom Button  
  14. Button button4 = new Button(this);  
  15. button4.setText("Button4"); 
Step 1
 
Create a project as in the following: 
 
Clipboard02.jpg
 
Step 2
 
Default XML file
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.    
  16. </RelativeLayout> 
Step 3
 
Create a Java file and provide this in it:
  1. package com.dynamicbuttoncreation;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.Button;  
  5. import android.widget.RelativeLayout;  
  6. import android.content.ContentValues;  
  7. import android.content.Context;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.database.sqlite.SQLiteOpenHelper;  
  11. import android.graphics.Color;  
  12. import android.os.Bundle;  
  13. import android.app.Activity;  
  14. import android.view.Menu;  
  15. import android.widget.Button;  
  16. import android.widget.RelativeLayout;  
  17. public class MainActivity extends Activity   
  18. {  
  19.  @Override  
  20.  public void onCreate(Bundle savedInstanceState)   
  21.  {  
  22.   super.onCreate(savedInstanceState);  
  23.   RelativeLayout relativeLayout = new RelativeLayout(this);  
  24.   RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);  
  25.   Button button1 = new Button(this);  
  26.   button1.setText("Button1");  
  27.   Button button2 = new Button(this);  
  28.   button2.setText("Button2");  
  29.   Button button3 = new Button(this);  
  30.   button3.setText("Button3");  
  31.   Button button4 = new Button(this);  
  32.   button4.setText("Button4");  
  33.   AddButtonLayout(button1, RelativeLayout.ALIGN_PARENT_LEFT);  
  34.   AddButtonLayout(button3, RelativeLayout.CENTER_IN_PARENT);  
  35.   AddButtonLayout(button4, RelativeLayout.ALIGN_PARENT_BOTTOM);  
  36.   LayoutAddButton(button2, RelativeLayout.ALIGN_PARENT_LEFT, 308000);  
  37.   relativeLayout.addView(button1);  
  38.   relativeLayout.addView(button3);  
  39.   relativeLayout.addView(button4);  
  40.   relativeLayout.addView(button2);  
  41.   setContentView(relativeLayout, relativeLayoutParams);  
  42.  }  
  43.  private void LayoutAddButton(Button button, int centerInParent, int marginLeft, int marginTop, int marginRight, int marginBottom)   
  44.  {  
  45.   RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  46.   buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);  
  47.   buttonLayoutParameters.addRule(centerInParent);  
  48.   button.setLayoutParams(buttonLayoutParameters);  
  49.  }  
  50.  private void AddButtonLayout(Button button, int centerInParent)   
  51.  {  
  52.   LayoutAddButton(button, centerInParent, 0000);  
  53.  }  
Step 4
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.dynamicbuttoncreation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.dynamicbuttoncreation.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Step 5
 
Output
 
Clipboard01.jpg


Similar Articles