androidandroid-spinnerandroid-gravity

Android changes "spinner text"'s gravity after selecting from "listitem"


In my scenario, when I click on the spinner the values dropdown will pop with listview text items aligned to left. Now what I wanted is after selecting from the list item when it sets to the spinner value the text item should be right aligned.

Unable to find a solution.

Below is my spinner code

 ArrayAdapter<String> adapter = new d4ValuesListAdapter(getActivity(),R.layout.spinner_dropdown_item, sVals);

       dynamicSpinner.setAdapter(adapter);

       dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> parent, View view,
                   int position, long id) {


               Log.v("item", (String) parent.getItemAtPosition(position));
               dynamicSpinner.setGravity(Gravity.END);
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
               // TODO Auto-generated method stub
           }
       });

Below is my adapter code

  public View getCustomView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   //return super.getView(position, convertView, parent);

   LayoutInflater inflater= ((FragmentChangeActivity) context).getLayoutInflater();
   View row=inflater.inflate(R.layout.spinner_dropdown_item, parent, false);
   TextView label=(TextView)row.findViewById(R.id.spinnervalues);
   label.setText(DayOfWeek[position]);
   sharedPrefrences =getActivity().getSharedPreferences(Constants.PREFERENCE_FILENAME,Activity.MODE_PRIVATE);
   int spinneritem = sharedPrefrences.getInt(Constants.SPINNER_LISTITEM, -1);
   if(spinneritem == position ){
       label.setBackgroundColor(getResources().getColor(R.color.turquise_blue));
       label.setTextColor(getResources().getColor(R.color.white_color));
   }

   return row;
  } 

Solution

  • To format and manage drop values you need to override method getDropDownView()

    and

    For Selected Item you need to override getView() or getContentView() method, so it will work.