Skip to content Skip to sidebar Skip to footer

I Have Spinner With Some Items, Some Of Which Have Long Text

I have Spinner with some items. Some of the items are having long text, so its not appearing on the spinner. How can I have scrolling text on the Spinner?

Solution 1:

For spinner, you have to create xml file

<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text1"
style="android:attr/dropDownItemStyle"android:singleLine="true"android:layout_width="fill_parent"android:layout_height="45px"android:ellipsize="marquee"
 android:textColor="#000000"android:gravity="center_vertical" />

Solution 2:

you have to create some custom spinner

Adapter.setDropDownViewResource(R.layout.spinner);

Solution 3:

you have to create a custom spinner

Globals in Activity

String[] spinnerValues = { "1-10", "10-100", "100-200","200-500", "500-1000","1000-2000","2000-5000","No. of Employees" };
 Private Spinner _spin;

In oncreate

_spin= (Spinner) findViewById(R.id.your_spinner_id);
_spin.setAdapter(newMyAdapter(this,R.layout.inflator_file,spinnerValues)); 
_spin.setSelection(spinnerValues.length - 1); // used to set a prompt in dropdown spinner.

Adapter class

publicclassMyAdapterextendsArrayAdapter<String> {
     
    publicMyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
    }

    @Overridepublic View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }
    @Overridepublic View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,ViewGroup parent) {
        LayoutInflaterinflater= getLayoutInflater();
        ViewmySpinner= inflater.inflate(R.layout.inflator_file, parent,false);
        TextViewmain_text= (TextView) mySpinner.findViewById(R.id.textone);
        main_text.setText(spinnerValues[position]);
        return mySpinner;
    }
}

inflater_file.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/textone"android:singleLine="true"android:textColor="#9c9a9b"android:gravity="left|center"android:typeface="serif"android:paddingLeft="8dp"android:textSize="14sp"android:layout_width="fill_parent"android:layout_height="24dp"android:ellipsize="marquee" />

Post a Comment for "I Have Spinner With Some Items, Some Of Which Have Long Text"