0

I am developing an app that does some calculation. It consists of a horizontal linearLayout and each Layout has 2 edit text and and a spinner. I want to make it in such a way that the user can add layouts with button click when he needs to add more details.

In my XML I set android:visibility="gone" and in Java I set the onClick method to make the "gone" linear layout to view.VISIBLE. It worked but it makes all the linear layouts appear once I click the + button. I want them to appear one after the other (one appear and others remain "gone" until I click the button again)

Mike
  • 8,055
  • 1
  • 30
  • 44

2 Answers2

0

Use fragments for this situation. you can add any fragments to view via an events.

Nasim Bahar
  • 115
  • 3
  • 13
  • I am new to android more details please – Abdullahi Aliyu Nov 06 '16 at 05:50
  • A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). – Nasim Bahar Nov 06 '16 at 05:53
0

You can use Layout inflater.

create another resource file in which add layout and widgets which you required e.g item_resource.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

   <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

  <!-- other layout elements which are required -->

</LinearLayout>

then inflate above layout pragmatically on onClick event

  LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
  View view = inflater .inflate(R.layout.item_resource,null);
  Button button =(Button)view.findViewById(R.id.btn);
  button.setText("  ");   // set value

Add this view to your main layout e.g

 LinearLayout myLayout=(LinearLayout)findViewById(R.id.xx);
 mylayout.add(view);
Onkar
  • 679
  • 1
  • 8
  • 25
  • It worked for only one click, but if i click the button again it doesn't add. I want it to be in such a way that it adds layout on each button click. Thank you. – Abdullahi Aliyu Dec 16 '16 at 10:58
  • if you want add new view on button click then use RecycleView and custom Adapter. Add new item to RecyleView on button click by overriding getView () method of custom adapter – Onkar Dec 16 '16 at 15:13
  • Can you help me with a sample code please. Thank you. – Abdullahi Aliyu Dec 16 '16 at 15:29
  • Hope this will help you [ this ] (http://stackoverflow.com/questions/17525886/listview-with-add-and-delete-buttons-in-each-row-in-android) – Onkar Dec 16 '16 at 15:39