I want to use a switch in my app, but I want it to have custom colors. Right now it doesn't show up at all.
Switch in my xml:
<Switch
android:id="#+id/switch"
android:track="#drawable/switch_track"
android:thumb="#drawable/switch_thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
switch_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey_light"/>
<item android:state_pressed="true" android:drawable="#color/grey_light"/>
<item android:state_checked="true" android:drawable="#color/accent_light"/>
<item android:drawable="#color/grey_light"/>
</selector>
switch_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey"/>
<item android:state_pressed="true" android:drawable="#color/grey"/>
<item android:state_checked="true" android:drawable="#color/accent"/>
<item android:drawable="#color/grey"/>
</selector>
Do I need to add icons for the selectors instead of colors or is there a way to just change the color?
If you are using the support library, you could use the material properties to color a SwitchCompat:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
styles.xml:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorSwitchThumbNormal">#color/grey</item>
<item name="colorControlActivated">#color/accent</item>
</style>
Related
Im trying to set a background color from the style xml depending if the state of the button is enabled or not.
Button XML:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/loginButton"
style="#style/SuccessButton2"
android:layout_width="200dp"
android:layout_height="40dp"
android:text="Iniciar sesiĆ³n"
android:gravity="center"
android:enabled="true"
/>
Style SuccesButton2:
<style name="SuccessButton2" parent="TextAppearance.MaterialComponents.Button">
<item name="android:textColor">#color/button_text_color</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#color/button_background_color</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:textAllCaps">false</item>
<item name="fontFamily">#font/myriad_pro_regular</item>
</style>
XML button_background_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/dark_gray"/>
<item android:state_enabled="true" android:color="#color/red"/>
i have the same for the Text and its working fine:
button_text_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#4E4E4E"/>
<item android:state_enabled="true" android:color="#android:color/black"/>
The background item its not working, it show the button with a transparent background only, enabled or not.
You can try with android:backgroundTint .
Tint to apply to the background.
<item name="android:background">#color/button_background_color</item>
<item name="android:backgroundTint">#color/button_background_color</item>
I chnaged the Apptheme attributes according to the new material guidelines, due to this my material button which I have used in all my project lost their theme colors.
I have tried making custom styles by making the defaults its parent and making them change their primary and secondary colors.
XML file for Button
<com.google.android.material.button.MaterialButton
android:id="#+id/bs_create_business"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="#string/create_business"
android:textAllCaps="true"
app:backgroundTint="#color/browser_actions_bg_grey" />
styles.xml
<style name="AppTheme"parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/foopprimary_500</item>
<item name="colorPrimaryDark">#color/foopprimary_900</item>
<item name="colorAccent">#color/foopsecondary_500</item>
<itemname="android:windowBackground">#android:color/white</item>
<item name="fontFamily">#string/default_font</item>
<item name="colorSecondary">#color/foopsecondary_500</item>
<item name="colorOnSecondary">#color/foopprimary_500</item>
</style>
My output is coming like this :
To change the colors in the Widget.MaterialComponents.Button.OutlinedButton style just use something lke:
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton" >
<!-- Border color -->
<item name="strokeColor">#color/stroke_color_selector</item>
<!-- Text color -->
<item name="android:textColor">#color/text_color_selector</item>
<!-- Background color -->
<item name="backgroundTint">#color/text_btn_bg_color_selector</item>
</style>
The default value for the selector are:
StrokeColor:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_checked="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_checked="false"/>
</selector>
TextColor:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true"/>
<item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true"/>
<item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
Background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.08" android:color="?attr/colorPrimary" android:state_checked="true"/>
<item android:color="#android:color/transparent" android:state_checked="false"/>
</selector>
I have been trying to figure out how to change the tint of a compoundDrawable of an EditText for some time with no success. I am using the EditText as a button and would like to change the color of the drawable right like the EditText underline color. How do I add a drawable state to the compoundDrawables?
I would like to tint the drawable like the underline tint when clicked.
Here is my styles.
<style name="EditTextDropdown" parent="#android:style/Widget.EditText">
<item name="android:clickable">true</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:focusable">false</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:singleLine">true</item>
<item name="android:cursorVisible">false</item>
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:drawableRight">#drawable/ic_drop_down_arrow_24dp</item>
</style>
EditText
<EditText
android:id="#+id/phone"
style="#style/EditTextDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add phone" />
Here is what it looks like normal
Here is what it looks like when pressed
you can directly override the appearance of the EditText by re-assigning it's attributes in the style of the Theme you are using in your app.
given code overrides the EditText Appearance.
<item name="android:editTextBackground">
#drawable/myenglishviolettheme_edit_text_holo_light
</item>
and the file in drawable is something like this myenglishviolettheme_edit_text_holo_light.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="#drawable/myenglishviolettheme_textfield_default_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="#drawable/myenglishviolettheme_textfield_disabled_holo_light" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/myenglishviolettheme_textfield_activated_holo_light" />
<item android:state_enabled="true" android:state_activated="true" android:drawable="#drawable/myenglishviolettheme_textfield_focused_holo_light" />
<item android:state_enabled="true" android:drawable="#drawable/myenglishviolettheme_textfield_default_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/myenglishviolettheme_textfield_disabled_focused_holo_light" />
<item android:drawable="#drawable/myenglishviolettheme_textfield_disabled_holo_light" />
</selector>
You may want to change the drawable resources to match your requirement
I have created following style for custom AppCompatButton.
<style name="AppTheme.secondary_button" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/secondary_button_color</item>
<item name="colorControlActivated">#color/menu_blue</item>
<item name="android:colorButtonNormal">#color/secondary_button_color</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#color/gray</item>
</style>
What is the attribute to change text color on button pressed?
To do this, you just need to replace the textColor you specified with a selector:
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/grey" /> <!-- (for accessibility) -->
<item android:color="#color/black" />
</selector>
and then assign it like this:
android:textColor="#drawable/button_foreground_selector"
<android.support.v7.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/CustomAppCompatButtonStyle"
android:text="Hi, Neo"/>
Not clicked button:
Clicked button:
Style I used:
<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/red</item>
<item name="colorControlActivated">#color/blue</item>
<item name="android:colorButtonNormal">#color/green</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#drawable/button_foreground_selector</item>
</style>
I have follow the answer in Android: Create a toggle button with image and no text everything was fine but when I load my application and when I click on my toggle button, nothing happen like it didn't change from off state to on state. So what do i have to do to make the sliding works? Do i need to code anything if yes what should i code?
Thanks
btn_toggle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/btn_toggle" />
</layer-list>
btn_toggle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/close" />
<item android:state_checked="true"
android:drawable="#drawable/open" />
</selector>
layout.xml
<ToggleButton
android:id="#+id/toggleButton"
style="#style/toggleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn_toggle_bg"
android:onClick="onToggleClicked"
android:layout_marginLeft="150dp" />
style.xml
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<style name="toggleButton" parent="#android:Theme.Black">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
</style>
Follow the three steps and after it all your app's togglebutton got the new style!
toggle.xml: (drawable-folder)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/toggle_on" />
</selector>
styles.xml: (values-folder)
<style name="toggle" parent="#android:style/Widget.Button.Toggle">
<item name="android:background">#drawable/toggle</item>
<item name="android:textOn"> </item>
<item name="android:textOff"> </item>
</style>
and last step you have to teach the current AppTheme that you have a custom toggle-button:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyleToggle">#style/toggle</item>
</style>