title: Showing last items in Spinner View Android
body: <p>I am trying to populate spinner from an array. All is working fine but when i click on the spinner the view takes me to the last items of the array. So i need to scroll up to see the first item.</p>
  
  <p>I want to get view from the beginning not from the end. Please look into my code. I used <strong><em>android:spinnerMode = "dialog"</em></strong> in my XML.</p>
  
  <p>My spinner1 is showing last items in the view but all items are coming in right order.</p>
  
  <pre><code>public class Pinbyplace extends AppCompatActivity {
  
  Spinner spinner1, spinner2, spinner3;
  ArrayAdapter&lt;String&gt; adapter1, adapter2, adapter3;
  
  public String[] state = {"Andaman and Nicobar Islands", "Andhra Pradesh", "Arunachal Pradesh", "Assam",
          "Bihar", "Chandigarh", "Chhattisgarh", "Dadar and Nagar Haveli",
          "Daman and Diu", "Delhi", "Goa", "Gujarat", "Haryana", "Himachal Pradesh",
          "Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Lakshadeep",
          "Madya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland",
          "Odhisa", "Pondicherry","Punjab", "Rajasthan", "Sikkim", "Tamil Nadu",enter code here
          "Tripura", "Uttaranchal", "Uttar Pradesh", "West Bengal", "Select State"};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.pincode_place);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);
      toolbar.setNavigationOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              onBackPressed();
          }
      });
  
      spinner1 = (Spinner) findViewById(R.id.spinner1);
      spinner2 = (Spinner) findViewById(R.id.spinner2);
      spinner3 = (Spinner) findViewById(R.id.spinner3);
  
      adapter1 = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item, state) {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View v = super.getView(position, convertView, parent);
              if (position == getCount()) {
                  ((TextView)v.findViewById(android.R.id.text1)).setText("");
                  ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount()));
              }
              return v;
          }
  
          @Override
          public int getCount() {
              return super.getCount()-1;
          }
      };
  
      //adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner1.setAdapter(adapter1);
      spinner1.setSelection(adapter1.getCount());
  
      adapter2 = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item) {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View v = super.getView(position, convertView, parent);
              if (position == getCount()) {
                  ((TextView)v.findViewById(android.R.id.text1)).setText("");
                  ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount()));
              }
              return v;
          }
  
          @Override
          public int getCount() {
              return super.getCount() - 1;
          }
      };
  
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      adapter2.add("Item 1");
      adapter2.add("Item 2");
      adapter2.add("Select District");
  
      spinner2.setAdapter(adapter2);
      spinner2.setSelection(adapter2.getCount());
  
      adapter3 = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item) {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View v = super.getView(position, convertView, parent);
              if (position == getCount()) {
                  ((TextView)v.findViewById(android.R.id.text1)).setText("");
                  ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount()));
              }
              return v;
          }
  
          @Override
          public int getCount() {
              return super.getCount()-1;
          }
      };
  
      adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      adapter3.add("Item 1");
      adapter3.add("Item 2");
      adapter3.add("Select Place");
  
      spinner3.setAdapter(adapter3);
      spinner3.setSelection(adapter3.getCount());
  
  }
   }
  </code></pre>
  
  <p>See the picture below:</p>
  
  <p><a href="https://i.stack.imgur.com/tPNIu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tPNIu.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/377Ld.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/377Ld.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE:</strong> I found a better and simple solution for this..</p>
  
  <pre><code>public class Pinbyplace extends AppCompatActivity {
  
  Spinner spinner_state, spinner_district, spinner_place;
  HintArrayAdapter hintAdapter_state, hintAdapter_district, hintAdapter_place;
  
  public String[] state = {"Select State", "Andaman and Nicobar Islands","Andhra Pradesh",
          "Arunachal Pradesh","Assam","Bihar", "Chandigarh","Chhattisgarh",
          "Dadra and Nagar Haveli","Daman and Diu","Delhi", "Goa","Gujarat","Haryana",
          "Himachal Pradesh","Jammu and Kashmir","Jharkhand", "Karnataka","Kerala",
          "Lakshadweep","Madhya Pradesh","Maharashtra","Manipur", "Meghalaya","Mizoram",
          "Nagaland","Odisha","Puducherry","Punjab","Rajasthan", "Sikkim","Tamil Nadu",
          "Telangana","Tripura","Uttar Pradesh","Uttarakhand","West Bengal"};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.pincode_place);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);
      toolbar.setNavigationOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              onBackPressed();
          }
      });
  
      spinner_state = (Spinner) findViewById(R.id.spinner1);
      spinner_district = (Spinner) findViewById(R.id.spinner2);
      spinner_place = (Spinner) findViewById(R.id.spinner3);
  
      hintAdapter_state = new HintArrayAdapter&lt;&gt;(getApplicationContext(), 0);
      hintAdapter_state.addAll(state);
      spinner_state.setAdapter(hintAdapter_state);
  
      spinner_state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              if(position &gt; 0) {
                  spinner_state.setSelection(position);
                  String state_name = (String) spinner_state.getSelectedItem();
                  Toast.makeText(getApplication(), state_name, Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  
      hintAdapter_district = new HintArrayAdapter&lt;&gt;(getApplicationContext(), 0);
      hintAdapter_district.add("Select Distrcit");
      spinner_district.setAdapter(hintAdapter_district);
  
      spinner_district.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              if(position &gt; 0) {
                  spinner_district.setSelection(position);
                  String district_name = (String) spinner_district.getSelectedItem();
                  Toast.makeText(getApplication(), district_name, Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  
      hintAdapter_place = new HintArrayAdapter&lt;&gt;(getApplicationContext(), 0);
      hintAdapter_place.add("Select Place");
      spinner_place.setAdapter(hintAdapter_place);
  
      spinner_place.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              if(position &gt; 0) {
                  spinner_place.setSelection(position);
                  String place_name = (String) spinner_place.getSelectedItem();
                  Toast.makeText(getApplication(), place_name, Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  }
  
  private class HintArrayAdapter&lt;T&gt; extends ArrayAdapter&lt;T&gt; {
  
      Context mContext;
  
      public HintArrayAdapter(Context context, int resource) {
          super(context, resource);
          this.mContext = context;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
          TextView texview = (TextView) view.findViewById(android.R.id.text1);
          texview.setTextColor(Color.BLACK);
          texview.setHintTextColor(Color.GRAY);
  
          if(position == 0) {
              texview.setText("");
              texview.setHint(getItem(position).toString()); //"Hint to be displayed"
          } else {
              texview.setText(getItem(position).toString());
          }
  
          return view;
      }
  
      @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
          View view;
  
          if(position == 0){
              view = inflater.inflate(R.layout.spinner_hint_list_item_layout, parent, false); // Hide first row
          } else {
              view = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
              TextView texview = (TextView) view.findViewById(android.R.id.text1);
              texview.setTextColor(Color.BLACK);
              texview.setText(getItem(position).toString());
          }
  
          return view;
      }
  }
  }
  </code></pre>