Sergey
Sergey

Reputation: 11928

Creating activity with layout defined in runtime in android application

Usually the activity has a predefined layout which is described in the xml file. What if I know the exact number and types of UI elements only during the runtime?(for example, I need to display as many TextBoxes as user defined) Is it possible to create an activity with a layout defined during runtime and if it is, how?

Upvotes: 0

Views: 932

Answers (2)

Dalmas
Dalmas

Reputation: 26547

First set an identifier to a view, where you want to insert your views at runtime :

<LinearLayout
    android:id="@+id/linear_layout" 
    ... >

Then you can add child views to this LinearLayout programatically, whenever you want :

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
linearLayout.removeAllViews();

// Add a TextView (it could be any kind of View)
TextView textView = new TextView(this);
textView.setText("...");
linearLayout.addView(textView);

Upvotes: 2

Geralt_Encore
Geralt_Encore

Reputation: 3771

setContentView(layout);

This layout you can define during runtime

Upvotes: 1

Related Questions