1290 1290 - 21 days ago 7
Android Question

Android: How to display more views after clicking a button

SOLVED THE CODE BELOW WORKS

If I have a layout similar to the one below:

enter image description here

Those five temp

EditTexts
represent some info the user can enter (like the price of an item, the order number, etc.) If the user wants to add another item they would click on the Add button and I want another 5 textviews to appear on the screen right above the two buttons but right below the previous set of the 5
EditTexts
. Can someone give me a starting point on how I would do this.

My layout of the fragment goes like this:


  • I have a top level linear layout (Vertical Orientation).

  • Then a scrollview.

  • Inside the scrollview I have another linear layout.

  • In that linear layout I have those five EditText objects and the two buttons



The view above is a fragment (defined in the file below) which I pass to my FragmentAdapter in my MainActivity file:

public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);

scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);

Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);

addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}


Here is my layout file for this fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/device_fragment_linear_layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<EditText
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:id="@+id/item_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Item number"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:id="@+id/carrier_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Supplier"/>

<TextView
android:id="@+id/color_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="Color"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />

<Button
android:id="@+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>


dynamic-content.xml:

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>

</LinearLayout>

Answer

in your XML take

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/device_scroll_view">
    <LinearLayout
        android:id="@+id/layoutDynamic"a
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal">            
    </LinearLayout>
</ScrollView>

and create one more XML for your Item and design accordingly (It will add dynamically when you click on ADD) dynamic_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/device_fragment_linear_layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">

<TextView
                android:id="@+id/etDynamic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:hint="Color"
                android:layout_marginTop="10dp"/>
</LinearLayout>

so come to java Code

// take the reference of LinearLayout
linearLayout = (LinearLayout) romptView.findViewById(R.id.layoutDynamic);


// Take the reference of Add button 

Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);

addButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        final View addView = layoutInflater1.inflate(R.layout.dynamic_row, null);
        final TextView textView1 = (TextView) addView.findViewById(R.id.etDynamic);        
        textView1.setText(otheret.getText().toString().trim());                                                                           otheret.setText("");                                        linearLayout.addView(addView);    
    }
});


#and if you want to remove particular item
removeIcon.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        addView.getParent()).removeView(addView);
    }
});