Yusuf Ali
Yusuf Ali

Reputation: 173

how to add linearlayout inside of linearlayout when application running

I want to know, there is a linearlayout and use that with setContentView function. Also there is a spinner inside of linearlayout. What I want to do is, create a new layout inside of /res/layout folder and add it into layout that I set with setContentView.

Is there anyway or I need to do that programmatically?

EDIT: I think I couldn't tell.

I have 2 two layouts(ready). I use the first layout with setContentView.For example, there is a buton and if user click that button, I want to add second layout bottom of first layout when application running.

Upvotes: 0

Views: 40

Answers (2)

hakanostrom
hakanostrom

Reputation: 1081

Easiest you do that with include in the xml of your main layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <include layout="@layout/second" />

</LinearLayout>

It´s also possible to do it programmatically, but this way I think it is more clear.

Edit: To do this programmatically, put this code in listener of the first button.

RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);

Button b = new Button(getApplicationContext());
b.setText("Click me too!");

view.addView(b);

Instead of creating a button (or whatever you want) you can also inflate a premade layout.

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);

view.addView(v);

Upvotes: 1

Nitin Sethi
Nitin Sethi

Reputation: 1336

I don't think you can change the res folder programmatically. You need to add any layout programmatically only.

Edited:

Get the 2nd layout's instance using findViewById and use setVisibility method to control the layout's visibility.

Upvotes: 0

Related Questions