I have a spinner. If selected item is (e.g) 1, then add EditText with button. I used switch method to check item_name. What you recommend in adding edittext with button? Is it a good way to create new layout? How can i add layout to screen?
Asked
Active
Viewed 50 times
-3
-
Here is how you can add views https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views – Themelis Nov 18 '18 at 10:51
2 Answers
2
Try to use OnItemSelectedListener.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Muhammadjon
- 1,476
- 2
- 14
- 35
1
Inside your initial layout create a container layout to inflate with your desired generated views.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
Then create the separate layouts (your_layout) that you want to generate upon spinner selection. When that happens, use a layout inflator to replace the container with the layout you want.
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.your_layout, null, false);
View container = inflater.inflate(R.id.container, null, false);
container.addView(layout);

Nikos Hidalgo
- 3,666
- 9
- 25
- 39