Nik Faris
Nik Faris

Reputation: 35

Add new spinner dynamically

I have a spinner, and have a button right to the spinner. When the button is clicked, new spinner is created under the existed spinner. It is created with a remove button at the right.

so my problem is :-

1) How can I get the value of the new spinner if I added two or three new spinners. Because if I added more than one new spinner. The value will be replaced all over.

2) How can I remove the value of new spinner when i clicked on remove button.

Here are my codes:

btnAddRow.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        final View addView = layoutInflater.inflate(R.layout.add_row, null);
                        spBidang2 = (Spinner) addView.findViewById(R.id.spSpecification2);

                        Button btnRemRow = (Button) addView.findViewById(R.id.btnRemRow);
                        btnRemRow.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                ((LinearLayout)addView.getParent()).removeView(addView);
                                spBidang2.setVisibility(View.GONE);
                            }
                        });

                        container.addView(addView);
                    }
                });

FOr xml :-

<LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="5dp"
                        android:orientation="horizontal"
                        android:weightSum="2" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1.25"
                            android:orientation="vertical" >

                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="36dp"
                                android:background="#206531"
                                android:gravity="center"
                                android:text="Pengkhususan"
                                android:textColor="#ffffff" />

                            <Space
                                android:layout_width="match_parent"
                                android:layout_height="match_parent" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="0.75"
                            android:orientation="vertical"
                            android:weightSum="2" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="0.5" >

                                <Spinner
                                    android:id="@+id/spSpecification"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dp"
                                    android:layout_weight="0.5"
                                    android:background="@drawable/field_shape"
                                    android:entries="@array/pengkhususan"
                                    android:singleLine="false" />

                                <Button
                                    android:id="@+id/btnAddRow"
                                    style="?android:attr/buttonStyleSmall"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1.5"
                                    android:text="+" />
                            </LinearLayout>

                            <LinearLayout
                                android:id="@+id/container"
                                android:layout_width="fill_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical" >
                            </LinearLayout>
                        </LinearLayout>

This is the xml for new spinner row :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2" >

    <Spinner
        android:id="@+id/spSpecification2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.5"
        android:background="@drawable/field_shape"
        android:entries="@array/pengkhususan"
        android:singleLine="false" />

    <Button
        android:id="@+id/btnRemRow"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.5"
        android:text="-" />

</LinearLayout>

Upvotes: 0

Views: 878

Answers (3)

Krupa Patel
Krupa Patel

Reputation: 3359

Refer this :

http://www.edumobile.org/android/android-development/dynamic-spinner/

This example will show how to create spinner with list view at run time in android. In this example you will be able to assign the contents of a spinner to another spinner at run time on a button click.

Hope this may help you!

Upvotes: 0

Michael Shrestha
Michael Shrestha

Reputation: 2555

For getting value of new spinners use ArrayList of Spinners
eg.
ArrayList<Spinner> spinners = new ArrayList<Spinner>();

then when you create new spinner add it to the list
i.e. spinners.add(newSpinner)

you can get value by
spinners.get(index).getSelectedItem().toString()

Upvotes: 1

Alok Nair
Alok Nair

Reputation: 4004

Better create a layout with all the spinners you need and make their visibility gone. Make only the ones needed initially visible, hide rest others and when you need to add new row, make it visible. This way you can access the value of spinner using its id and even make it disappear while clicking right clear button.

Hope this helps :)

Upvotes: 0

Related Questions