In this Aricle help us to create a dynamic view group.
public class MainActivity extends Activity {
Button btn, btn1;
LayoutInflater linflater;
LinearLayout l;
static int pos = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
btn1 = (Button) findViewById(R.id.Button02);
l = (LinearLayout) findViewById(R.id.LinearLayout01);
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
btn.setOnClickListener(new myListener());
btn1.setOnClickListener(new myListener1());
}
class myListener implements View.OnClickListener {
@Override
public void onClick(View v) {
View myView = linflater.inflate(R.layout.dynamicoption, null);
myView.setId(pos);
pos++;
l.addView(myView);
}
}
class myListener1 implements View.OnClickListener {
@Override
public void onClick(View v) {
if (pos != 0) {
pos--;
View myView = l.findViewById(pos);
l.removeView(myView);
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal">
<Button android:id="@+id/Button01" android:background="@drawable/plus"
android:layout_height="45dip" android:layout_width="45dip"></Button>
<Button android:id="@+id/Button02" android:background="@drawable/minus"
android:layout_height="45dip" android:layout_width="45dip"></Button>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
dynamicoption.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Icons
http://jmez.net/el/images/Minus.png
http://www.workingwithrails.com/images/plus.png?1213632569
public class MainActivity extends Activity {
Button btn, btn1;
LayoutInflater linflater;
LinearLayout l;
static int pos = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
btn1 = (Button) findViewById(R.id.Button02);
l = (LinearLayout) findViewById(R.id.LinearLayout01);
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
btn.setOnClickListener(new myListener());
btn1.setOnClickListener(new myListener1());
}
class myListener implements View.OnClickListener {
@Override
public void onClick(View v) {
View myView = linflater.inflate(R.layout.dynamicoption, null);
myView.setId(pos);
pos++;
l.addView(myView);
}
}
class myListener1 implements View.OnClickListener {
@Override
public void onClick(View v) {
if (pos != 0) {
pos--;
View myView = l.findViewById(pos);
l.removeView(myView);
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal">
<Button android:id="@+id/Button01" android:background="@drawable/plus"
android:layout_height="45dip" android:layout_width="45dip"></Button>
<Button android:id="@+id/Button02" android:background="@drawable/minus"
android:layout_height="45dip" android:layout_width="45dip"></Button>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
dynamicoption.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Icons
http://jmez.net/el/images/Minus.png
http://www.workingwithrails.com/images/plus.png?1213632569