CounterFlame
CounterFlame

Reputation: 1632

Android Toggle Button - Background Color

I'm trying to use a Toggle Button, supporting back to API 11. The problem is this standard Toggle Buttom seems to have a greyish background color, while I just want it to be white (like the background).

Example picture

I've been trying:

android:background="@color/white"
android:background="@null"

but then I also lose the indicator light, which I want to keep. I've seen a few answers about changing the background color of the toggle button (creating own selector), but I seem to be losing the indicator light with those as well.

This is the relevant part of the layout:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/event_bottom_sheet_attenders"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/white"
                android:drawableLeft="@drawable/state_list_people_person_bottom_sheet"
                android:drawableStart="@drawable/state_list_people_person_bottom_sheet"
                android:gravity="center"
                android:padding="10dp"
                android:text="Attenders"
                android:textColor="@color/material_teal_500" />

            <ToggleButton
                android:id="@+id/event_bottom_sheet_toggle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:gravity="center"
                android:padding="10dp"
                android:textColor="@color/material_teal_500"
                android:textOff="Not going"
                android:textOn="Going" />

</LinearLayout>

How do I go about fixing this?

Thanks in advance.

Upvotes: 2

Views: 2912

Answers (2)

sihao
sihao

Reputation: 431

You can consider looking at different android themes for a start.

Choose a theme that is ideal for your liking or you can customize your own if you want something special.

From what you have described, you can try Android Light theme. Change your activity information as such:

  <activity android:theme="@android:style/Theme.Light">

P/s: Applying a theme also allows your app too look more consistent.

Upvotes: 1

AgentLog
AgentLog

Reputation: 107

Check this out :-

ToggleButton Btn=new ToggleButton(this);// or get it from the layout by ToggleButton Btn=(ToggleButton) findViewById(R.id.IDofButton);
    Btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked)
                buttonView.setBackgroundColor(Color.GREY);
            else buttonView.setBackgroundColor(Color.WHITE);
        }
    });

Do it accordingly as you need.

Hope this helps. :)

Upvotes: 0

Related Questions