title: How to put icon for a custom spinner in android
body: <p><strong>What i have</strong>:: I have a actionbar with a custom spinner</p>
  
  <p><strong>What i am trying to do</strong>:: </p>
  
  <ul>
  <li>as seen in the snapshot below on the actionbar i can see selection
  insted i always want to show a icon as default which must not change
  when i select a spinner selection</li>
  <li>How to achieve this</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/ga2V6.png" alt="enter image description here"></p>
  
  <hr>
  
  <p><strong>FragmentContainer.java</strong></p>
  
  <pre><code>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&lt;CharSequence&gt; 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&lt;?&gt; 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&lt;?&gt; 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
  
      }
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p><strong>spinner_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" /&gt; 
  </code></pre>
  
  <p><strong>spinner_drop_down_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" /&gt;
  </code></pre>
  
  <p><strong>layout_spinner</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" &gt;
  
      &lt;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" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>