Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add LinearLayout on Android?

I have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them. How can it be done dynamically?

like image 246
Sushan Ghimire Avatar asked Feb 01 '12 15:02

Sushan Ghimire


People also ask

How to Add LinearLayout Dynamically in Android?

This example demonstrates how to add a TextView to a LinearLayout dynamically in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which code snippet allows you to programmatically add a TextView to a LinearLayout?

Is setContentView(R. layout.

In which file can we create Dynamic content in Android?

Dynamic layouts are developed using Java and can be used to create layouts that you would normally create using an XML file.

What is Dynamic layout in Android Studio?

DynamicLayout is a text layout that updates itself as the text is edited. This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or need to call Canvas.


2 Answers

LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
like image 197
ihrupin Avatar answered Oct 06 '22 12:10

ihrupin


The easiest way is to create a layout in xml and inflate it using

LayoutInflater.from(context).inflate(R.layout.my_linear_layout);

You may also want to setId() your added views so you can access them easily later on.

like image 33
josephus Avatar answered Oct 06 '22 14:10

josephus