Skip to content Skip to sidebar Skip to footer

Change Android Spinner Text Color When Button Pressed

I have a series of ToggleButtons which represent a series of topics. When toggled, the ToggleButton changes its background color to indicate its state. When in the checked state, t

Solution 1:

Try the following way.

  1. Create a xml named

spinnertext.xml

<?xml version="1.0" encoding="utf-8"?><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/spinnerText"style="?android:attr/spinnerItemStyle"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingBottom="2dp"android:paddingLeft="6dp"android:textColor="#41f2f3" />

Now in code.

ArrayAdapter<String> sp_adapter = newArrayAdapter<String>(this, R.layout.spinnertext, your_array);

sp.setAdapter(sp_adapter);

Then work with toggle button

ToggleButtontb= (ToggleButton) findViewById(R.id.toggleButton1);
    tb.setOnCheckedChangeListener(newOnCheckedChangeListener() {

        @OverridepublicvoidonCheckedChanged(CompoundButton arg0, boolean isChecked) {
            TextViewtv= (TextView) findViewById(R.id.spinnerText);
            if (isChecked)
                tv.setTextColor(Color.RED);
            else
                tv.setTextColor(Color.BLUE);

        }
    });

Solution 2:

On Spinner onItemSelected Method you have to change like this:

publicvoid onItemSelected(AdapterView<?> parent, View arg1, int arg2,
    long arg3) {
// TODO Auto-generated method stub

item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);



  }

Solution 3:

One thing that we have on our app is a custom spinner view. It has a translucent black rounded square behind it and slightly larger than it. It works over any background.

Solution 4:

The answer from Gunaseelan helped point me in the right direction. As he suggested, working with the ToggleButton is the way to go. The ToggleButton widget has an android:onClick XML attribute which you can use to specify a method to run when the ToggleButton is toggled/clicked (as described here.

Setting the color of the spinner text and the spinner selector can be a bit difficult. To change the spinner text color:

ToggleButtoncardiologyToggle= (ToggleButton) findViewById(R.id.cardiology_toggle);
        if (cardiologyToggle.isChecked()) {
            spinnerText.setTextColor(Color.WHITE);
        } else {
            spinnerText.setTextColor(Color.BLACK);
        }

This changes only the text displayed when the spinner has been selected.

Post a Comment for "Change Android Spinner Text Color When Button Pressed"