Is it possible to duplicate an element, (such as a spinner that I already have on my activity screen) by pressing a button? And I'll be able to use both spinners and add as many as I want?
Asked
Active
Viewed 618 times
0
-
1If you mean the XML that describes a layout the answer is no. These files are not mutable. However you can programmatically add views to your layout. Please clarify what you really want to do. – Henry Aug 05 '18 at 05:11
-
1Sorry, I probably explained that badly. For example I have a spinner, and when I press a button that says 'add', that spinner will be duplicated and I'll be able to use both. – Aug 05 '18 at 05:13
-
3https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views – Henry Aug 05 '18 at 05:15
1 Answers
2
This code show you how to create spinner programmatically and add it to a LinearLayout.
You can call makeSpinner method and get a duplicated spinner.
Then add it to your ViewGroup as a child view by calling addView method.
Tip: you should store reference of added spinner for accessing them later. I stored them in mSpinners.
public class MainActivity extends AppCompatActivity {
private LinearLayout mLinearLayout;
private ArrayList<Spinner> mSpinners; // to hold reference of added spinner for reading purposes
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinners = new ArrayList<>();
mLinearLayout = findViewById(R.id.my_linearLayout);
mLinearLayout.addView(makeSpinner()); // First spinner
Button duplicateSpinner = findViewById(R.id.bt_duplicate);
duplicateSpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Spinner spinner = makeSpinner();
mLinearLayout.addView(spinner); // Add another spinner
}
});
Button getSpinner = findViewById(R.id.bt_getSpinner);
getSpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < mSpinners.size(); i++) { // Read all spinners
Spinner spinner = mSpinners.get(i);
Log.i("TAG", spinner.getSelectedItem().toString());
}
}
});
}
private Spinner makeSpinner() {
Spinner spinner = new Spinner(this, Spinner.MODE_DROPDOWN);
// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
spinner.setLayoutParams(layoutParams);
// Prepare data
ArrayList<String> data = new ArrayList<>(); // change it based on your code
data.add("Item 1");
data.add("Item 2");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mSpinners.add(spinner); // store it
return spinner;
}
}
This is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_linearLayout"
android:orientation="vertical"
tools:context="com.khahani.app.stack.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_duplicate"
android:text="Duplicate"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_getSpinner"
android:text="Get Selected Item"/>
</LinearLayout>
Enjoy it.

Mohammad Reza Khahani
- 678
- 4
- 16
-
1If you just keep a reference to the Spinner, you don't really need an id at all. – Code-Apprentice Aug 05 '18 at 06:33
-
You're right. I change the sample code. Thanks @Code-Apprentice – Mohammad Reza Khahani Aug 05 '18 at 06:47
-
When I try that in my code it gives me error: error: class, interface, or enum expected – Aug 05 '18 at 08:44
-
1You're not using the code correctly. take a look at this: [link](https://stackoverflow.com/questions/31665663/android-studio-error-class-interface-or-enum-expeted) – Mohammad Reza Khahani Aug 05 '18 at 09:43
-
1@crazyCoderN If you have additional problems after trying this solution, you should post a new quesiton. – Code-Apprentice Aug 05 '18 at 17:06