Devrath
Devrath

Reputation: 42824

How to put icon for a custom spinner in android

What i have:: I have a actionbar with a custom spinner

What i am trying to do::

enter image description here


FragmentContainer.java

public class FragmentContainer extends Fragment{
    View spinnerView;

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

        View view=inflater.inflate(R.layout.fragment_main, container, false);
        spinnerView = inflater.inflate(R.layout.layout_spinner, null);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        Spinner spinner = (Spinner) spinnerView.findViewById(R.id.my_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_items_array, R.layout.spinner_item);
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // Do whatever you want with your selected item. You can get it as: parent.getItemAtPosition(position); 
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

        getActivity().getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_launcher));//set your actionbar logo
        getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE );

        LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.RIGHT; // set your layout's gravity to 'right'
        getActivity().getActionBar().setCustomView(spinnerView, layoutParams); //place your layout on the actionbar

    }
}

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new FragmentContainer()).commit();
    }

}

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cab_spinner_item"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="@android:color/white" /> 

spinner_drop_down_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="@android:color/white" />

layout_spinner

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        tools:listitem="@android:layout/simple_spinner_dropdown_item" />

</LinearLayout>

Upvotes: 0

Views: 1117

Answers (1)

Leonard Feehan
Leonard Feehan

Reputation: 481

In your onItemSelected(AdapterView parent, View view, int position, long id) set the view text to null with :

((TextView)view).setText(null);

Then set the icon you want via:

spinner.setBackgroundDrawable(/* your icon drawable*/);

Style up the xml of the Spinner element with heights and widths appropriate to your icon.

Upvotes: 1

Related Questions