user2955143
user2955143

Reputation: 177

adding a layout once a button is clicked

In my application I have to create a button(plus). Upon clicking that button, it should append a view like shown below dynamically below another.

view the view has two edittext in a row. I don't know how to do that one. Please suggest me some tutorial for it. Do I have to make custom view for that? Thanks in advance.

Upvotes: 0

Views: 62

Answers (2)

gaurav5430
gaurav5430

Reputation: 13882

say this is your main layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   </LinearLayout>

and this is your layout, to be added (in a separate file)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/hidden_layout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

    <TextView  android:id="@+id/text_view"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text of hidden layout"/>

    <EditText  android:id="@+id/edit_text"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is your name"/>
</LineraLayout>

in your activity

LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);

now your button.onclick code would look like

 View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
 main.addView(view);

Upvotes: 0

yahya
yahya

Reputation: 4860

Create a layout to generate it dynamically, layout needs to contain your email edittext and your spinner.

Then create your main layout as one linearlayout, oriented as vertical, and one your plus button. OnClick of this button, you can inflate generic layout and add it to above linearlayout, so it will automatically push your plus button.

Upvotes: 1

Related Questions