Can you set the background color on the toggle button without closing the switch?

I have a ToggleButton. I want the background to be clear, for example, in the default application for applications for the days of the week. The code below shows a switch with a clear color. Is there a way to save the switch and change the background color without pressing my own toggle button? If not, it will be pretty bad in everything, imo. Also, do I really need to define a transparent color here or is there a built-in transparent color that I could use in my xml?

<ToggleButton android:background="@drawable/clear_toggle_button" android:id="@+id/btn_sunday" android:layout_width="50dp" android:layout_height="50dp" android:textOn="SUN" android:textOff="SUN" /> 

This is my color .xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="clear">#ffffff00</color> </resources> 

This is my list of color states in the drawable folder.

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/clear" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/clear" /> </selector> 
+5
source share
3 answers

I want the background to be clear.

Transparent? Yes, android has a color defined for this. You can access it:

 @android:color/transparent 

Or, in code:

 Color.TRANSPARENT 

@android:color/transparent is defined in res/values/colors.xml :

 <color name="transparent">#00000000</color> 

Alpha bits (first and second) are equal to zero.

But your definition for clear color:

 <color name="clear">#ffffff00</color> 

not transparent. You can render transparent like any color with its alpha set to zero . In your definition of clear alpha bits are full-blown to ff - 255 - opaque.

The definition of color gives the following:

enter image description here

Is there a way to save the switch and change the background color without pressing my own toggle button?

The thing is: the background color, and the switch is one extendable . The 'on' state is represented by one single possible, as well as the "off" state. You cannot just change color without losing toggle feedback. To change anything about ToggleButton's default background, you need to provide alternate drawings for each state.

I want the background to be clear, for example, in the default application for applications for the days of the week.

Setting the background to transparency will not work. I would advise you to study the source code and resources associated with creating ToggleButton . For example, the states on and off a ToggleButton represented by drawable resources. So, if you decide to change the background, you will need to provide ToggleButton at least two drawings: one for each state.

See how the default alarm clock app does it. Used ToggleButtons for days is defined as:

 <ToggleButton android:layout_width="wrap_content" android:layout_height="48dp" android:layout_gravity="center" android:padding="0dp" style="@style/body" android:textColor="@color/clock_gray" **android:background="@drawable/toggle_underline"** android:clickable="false" android:singleLine="true"/> 

Selectable toggle_underline is a state selector:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/toggle_underline_activated"/> <item android:state_checked="true" android:drawable="@drawable/toggle_underline_activated"/> <item android:drawable="@drawable/toggle_underline_normal"/> </selector> 

As you can see, if the ToggleButton parameter is set to on or checked (or when it is pressed ), then @drawable/toggle_underline_activated set to the background. Otherwise, @drawable/toggle_underline_normal is @drawable/toggle_underline_normal - the state is off .

toggle_underline_activated and toggle_underline_normal are graphical toggle_underline_normal with 9 patches.

toggle_underline_activated.9.png:

enter image description here

toggle_underline_normal.9.png:

enter image description here

You can get these drawings (and more) here: Link .

Thus, you can either create your own 9 patch panels with a transparent background, or use them with the state selector, or you can look at the default project for the alarm clock and use the drawings from its drawable-XXXX folders.

+10
source

If you just want to change the color of ToggleButton without losing its "Toggle", as I found, create a theme for it in your style. xml:

 <style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored"> <item name="colorButtonNormal">@color/primary_dark</item> <item name="android:textColor">@color/accent</item> </style> 

Then, on the xml of your button, simply set the theme:

  <ToggleButton android:id="@+id/part2" android:layout_width="60dp" android:layout_height="wrap_content" android:textOff="B" android:textOn="B" android:theme="@style/AppTheme.Button"/> 

With this, I set the button color to my primary_dark color, while maintaining its display.

Hope this can help someone.

0
source

try it

<ToggleButton android:background="@color/buttoncolor" android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="ON" android:textOff="OFF"/>

Create Values ​​/color.xml

<color name="buttoncolor">#FF4000</color>

-1
source

Source: https://habr.com/ru/post/1200495/


All Articles