3

I am using the spinner for showing the value as dropdown, I am changing the spinner text value by using below code

<Spinner
        android:id="@+id/showUnit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:entries="@array/unitName"
        android:background="@drawable/gradient_spinner_map_miles_button" />

    showUnit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                String item = arg0.getItemAtPosition(arg2).toString();
                if (arg1 != null && arg1 instanceof TextView) {
                     ((TextView)arg1).setTextColor(Color.WHITE);
                    ((TextView) arg1).setTextSize(13);
                    ((TextView) arg1).setGravity(Gravity.CENTER);
                 }

}

showUnit = (Spinner) findViewById(R.id.showUnit);

But when I try to rotate the screen, ((TextView)arg0.getChildAt(0)) returns null.

I know that when I rotate the screen in landsacpe or portrait mode activity cycle is restart then why spinner is getting null.

Please give me appropriate solution.

Thanks

2 Answers2

2

Why are you using getChildAt when you have the appropriate view in View arg1 ?

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{           
     if (arg1 != null && arg1 instanceof TextView) {
         ((TextView)arg1).setTextColor(Color.WHITE);
     }
}

To change the color of a selected text in a spinner it's in the best practice to use a selector.

spinner_state.xml (in drawable folder)

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@color/black" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@color/red" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@color/red" />
    <item
        android:state_enabled="true"
        android:drawable="@color/gray" />
</selector>

in your spinner, apply the selector :

    android:dropDownSelector="@drawable/spinner_state"

Spinner source from : Spinner does not apply dropDownSelector attribute

It's doesn't explain why your values are null in onItemSelected but without your full code I can't see what's wrong.

Community
  • 1
  • 1
Andros
  • 4,069
  • 1
  • 22
  • 30
  • But same problem is getting while rotating the screen getting arg1 is null – user2828418 Jan 09 '14 at 10:20
  • I have just declare one spinner in XML and initialize in activity, my requirement is change the select spinner text color in spinier view, we can change spinner text color by using above code or ( ((TextView)arg0.getChildAt(0)).setTextColor(Color.WHITE)) when I enter on screen its working perfectly but as I rotate the screen ((TextView)arg0.getChildAt(0) is null means arg0.getChildCount() is coming zero – user2828418 Jan 09 '14 at 10:40
  • I have edited my question, please see above, I am not change only color also change size, gravity etc. can't use your above code. Thanks for you support – user2828418 Jan 09 '14 at 13:02
  • Please show the entire activity to see how you handle your rotation. – Andros Jan 09 '14 at 13:28
1

I had the same problem, so I moved the setTextColor on getView :

ArrayAdapter<CharSequence> adapter =
    new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, getResources().getTextArray(R.array.myarray)){
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent){
            View v = null;
            System.out.println("getDropDownView");
            v = super.getDropDownView(position, null, parent);
            // If this is the selected item position
            if (position == PosItemSelected) {
                v.setBackgroundColor(getResources().getColor(R.color.orange_energicod));
            }
            else {
                // for other views
                v.setBackgroundColor(Color.WHITE);
            }
            return v;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);                     
            ((TextView) v).setTextColor(getResources().getColor(R.color.white));
            return v;
        }
    };

// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
spinner.setOnItemSelectedListener(this); // set the listener, to perform actions based on item selection

And now:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // An item was selected. You can retrieve the selected item using parent.getItemAtPosition(pos)        
}

Hope it's work for you too.

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
Ehcnalb
  • 466
  • 4
  • 16