0

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, the ToggleButton has a darker color.

Selected state. enter image description here

A Spinner overlays the ToggleButton and allows the user to select a difficulty for the topic.

How do I change the text color (to white) of the Spinner when the ToggleButton is pressed? I think I can handle changing the spinner selector, but I'm struggling to work out a way to change the text color.

Blake Mumford
  • 17,201
  • 12
  • 49
  • 67

4 Answers4

1

On Spinner onItemSelected Method you have to change like this:

public void 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);



  }
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
  • Simply changing the color of the spinner item when it is selected will not solve the problem. I.e. if my toggle button is in its unselected state (white background) then changing the spinner item text color to white will mean I'll have white text on a white background. – Blake Mumford Jul 02 '13 at 05:19
1

Try the following way.

  1. Create a xml named

spinnertext.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns: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 = new ArrayAdapter<String>(this, R.layout.spinnertext, your_array);

sp.setAdapter(sp_adapter);

Then work with toggle button

    ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
    tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

        }
    });
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • thanks for your answer, it helped me get halfway there. I will post an answer of what I ended up doing as soon as possible. – Blake Mumford Jul 04 '13 at 23:58
0

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.

HalR
  • 11,411
  • 5
  • 48
  • 80
0

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:

ToggleButton cardiologyToggle = (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.

Blake Mumford
  • 17,201
  • 12
  • 49
  • 67