user4078066
user4078066

Reputation:

How to add spinner with multiple selection and show it in edittext?

I want to create spinner with multiple selection,like user can select more than one items and get that items in edittext,for that i am following this example but its not working properly http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html in my fragment class

check out this link here i got answer for activity class not but not for fragment Get the alert dialog selected value to edittext

Upvotes: 2

Views: 2414

Answers (1)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

If you are following this tutorial "http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html" its working correctly in Activity.

If you are facing problem in creating same for Fragment, here is the code.

main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </FrameLayout>



</RelativeLayout>

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.testandroid.MultiSelectionSpinner
        android:id="@+id/mySpinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mySpinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="118dp"
        android:onClick="onClick"
        android:text="Get Items" />

    <EditText
        android:id="@+id/values"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MyFragment.java class

public class MyFragment extends Fragment {

    MultiSelectionSpinner spinner;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate (R.layout.fragment_layout, null);

        String[] array = { "one", "two", "three" };
        spinner = (MultiSelectionSpinner) v.findViewById (R.id.mySpinner1);
        spinner.setItems (array);


        final EditText values = (EditText) v.findViewById (R.id.values);

        Button button1 = (Button) v.findViewById (R.id.button1);
        button1.setOnClickListener (new OnClickListener() {

            @Override
            public void onClick (View v) {

                String s = spinner.getSelectedItemsAsString();  
                values.setText (s);

            }
        });
        return v;
    }
}

In your main activity

MyFragment fragment = new MyFragment ();
FragmentTransaction ft = getFragmentManager ().beginTransaction ();
ft.replace (R.id.container, fragment, "fragment");
ft.commit ();

Screen 1 enter image description here

Screen 2 enter image description here

Upvotes: 1

Related Questions