0

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?

  • 1
    If 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
  • 1
    Sorry, 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
  • 3
    https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views – Henry Aug 05 '18 at 05:15

1 Answers1

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>

enter image description here

Enjoy it.