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. // Defining the RelativeLayout layout parameters with Fill_Parent
  4. 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. // Creating a new Left Button with Margin
  5. Button button2 = new Button(this);
  6. button2.setText("Button2");
  7. // Creating a new Center Button
  8. Button button3 = new Button(this);
  9. button3.setText("Button3");
  10. // Creating a new Bottom Button
  11. Button button4 = new Button(this);
  12. 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. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />
  14. </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, 30, 80, 0, 0);
  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, 0, 0, 0, 0);
  53. }
  54. }
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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.dynamicbuttoncreation.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
Output
Clipboard01.jpg