In Android sometimes you need to create layouts programatically or dynamically according to your need. So today’s is about how to create layouts programatically in android.
Below is a method that I am going to use in this article. In this i am creating 5 Buttons dynamically and setting their ID’s for further use. After creating add this Button to Layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private void showVerticalLayout() { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //Layout params for Button //Loop for 5 items for (int i = 0; i < 5; i++) { Button button = new Button(this);//Creating Button button.setId(i);//Setting Id for using in future button.setText("Item " + i);//Setting text button.setTextSize(15);//Text Size button.setPadding(5, 5, 5, 5);//paading button.setLayoutParams(params);//Setting Layout Params button.setTextColor(Color.parseColor("#000000"));//Text Colort button.setGravity(Gravity.CENTER);//Gravity of Text button.setOnClickListener(this);//Setting click listener layoutHolder.setOrientation(LinearLayout.VERTICAL);//Setting Layout orientation layoutHolder.addView(button);//Finally adding view } } |
In this tutorial, we are going to learn how to create layouts programatically and do something when they clicked.
1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.
2. Open res=>values=strings.xml and add the following strings to it.
1 2 3 4 5 6 |
<resources> <string name="app_name">Adding Dynamic Layout Demo</string> <string name="vertical">Vertical Layout</string> <string name="horizontal">Horizontal Layout</string> <string name="customHorizontal">Custom Horizontal Layout</string> </resources> |
3. Create a xml layout naming activity_main.xml and add the following code. In this layout there are three Buttons for showing different layouts and a fragment holder for placing layouts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/verticalLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/vertical" /> <Button android:id="@+id/horizontalLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/horizontal" /> <Button android:id="@+id/customHorizontalLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/customHorizontal" /> <!-- Layout for placing custom layout --> <LinearLayout android:id="@+id/layoutHolder" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="5dp" /> </LinearLayout> </ScrollView> |
4. Now come to your MainActivity.java and add the following code. In this class there is a Button[] and TextView[] array for holding a reference of Button and TextView respectively.
Note: Set the TextView/Button ID for using this view like setting click listener.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package com.dynamiclayout_demo; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static Button vertical, horizontal, customHorizontal; private static LinearLayout layoutHolder; //Array for holding refrences private static Button[] buttonHolder; private static TextView[] textHolder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); setListeners(); } //Init all Views private void initViews() { vertical = (Button) findViewById(R.id.verticalLayout); horizontal = (Button) findViewById(R.id.horizontalLayout); customHorizontal = (Button) findViewById(R.id.customHorizontalLayout); layoutHolder = (LinearLayout) findViewById(R.id.layoutHolder); } //Set click listeners over all buttons private void setListeners() { vertical.setOnClickListener(this); horizontal.setOnClickListener(this); customHorizontal.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.verticalLayout: showVerticalLayout(); break; case R.id.horizontalLayout: showHorizontalLayout(); break; case R.id.customHorizontalLayout: showCustomHorizontalLayout(); break; //Cases for dynamic added Button and TextViews //In this we are changing text of view case 0: ((TextView) view).setText("Text Changed"); break; case 1: ((TextView) view).setText("Text Changed"); break; case 2: ((TextView) view).setText("Text Changed"); break; case 3: ((TextView) view).setText("Text Changed"); break; case 4: ((TextView) view).setText("Text Changed"); break; } } //Show vertical layout method private void showVerticalLayout() { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //Layout params for Button layoutHolder.removeAllViews();//Remove all views from Layout before placing new view buttonHolder = new Button[5];//Setting size of Button Array //Loop for 5 items for (int i = 0; i < 5; i++) { Button button = new Button(this);//Creating Button button.setId(i);//Setting Id for using in future button.setText("Item " + i);//Setting text button.setTextSize(15);//Text Size button.setPadding(5, 5, 5, 5);//paading button.setLayoutParams(params);//Setting Layout Params button.setTextColor(Color.parseColor("#000000"));//Text Colort button.setGravity(Gravity.CENTER);//Gravity of Text button.setOnClickListener(this);//Setting click listener buttonHolder[i] = button;//Setting button reference in array for future use layoutHolder.setOrientation(LinearLayout.VERTICAL);//Setting Layout orientation layoutHolder.addView(button);//Finally adding view } } //Show Horizontal Layout Method private void showHorizontalLayout() { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); //Layout params for Button layoutHolder.removeAllViews();//Remove all views from Layout before placing new view buttonHolder = new Button[3];//Setting size of Button Array //Loop for 3 items for (int i = 0; i < 3; i++) { Button button = new Button(this);//Creating Button button.setId(i);//Setting Id for using in future button.setText("Item " + i);//Setting text button.setTextSize(15);//Text Size button.setPadding(5, 5, 5, 5);//paading button.setLayoutParams(params);//Setting Layout Params button.setTextColor(Color.parseColor("#000000"));//Text Colort button.setGravity(Gravity.CENTER);//Gravity of Text button.setOnClickListener(this);//Setting click listener buttonHolder[i] = button;//Setting button reference in array for future use layoutHolder.setOrientation(LinearLayout.HORIZONTAL);//Setting Layout orientation layoutHolder.addView(button);//Finally adding view } } //Adding xml view to Layout Method private void showCustomHorizontalLayout() { layoutHolder.removeAllViews();//Remove all View from layout textHolder = new TextView[3];//init size of TextView array LayoutInflater in = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);//Layout inflater for inflating layout views //Loop for 3 items for (int i = 0; i < 3; i++) { View v = in.inflate(R.layout.customlayout, null);//Getting xml view TextView text = (TextView) v.findViewById(R.id.customTextView);//Finding Id of textView text.setId(i);//Setting id of textview text.setText("Text " + i);//setting text text.setOnClickListener(this);//implementing click listener textHolder[i] = text;//Setting text reference for future use layoutHolder.setOrientation(LinearLayout.HORIZONTAL);//Setting layout orientation layoutHolder.addView(v); } } } |
5. In above class in customHorizontalLayout() i am using xml layout to add over LinearLayout, so for that we need to create xml naming customlayout.xml and add the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/customTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:textColor="#000000" android:textSize="15sp" /> </LinearLayout> |
6. Finally, all done you can use above steps for using custom font in your app. You can use RelativeLayout, FrameLayout, etc. also.
Thanks.
Subscribe to us and get the latest news.
1 Comment
Coşkun Güngör
Wednesday, November 7th, 2018Thank you very much. very useful article.