This question already has answers here:
Android button background is taking the primary color
(6 answers)
Closed 1 year ago.
I want to customize a button using my custom drawable file as follows:
custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#48cae4" />
<corners android:radius="10dp" />
</shape>
But after changing the background of the button(as follows) there is no effect
android:background="#drawable/custom_button"
But if I change <style name="Theme.A" parent="Theme.MaterialComponents.DayNight.NoActionBar"> to <style name="Theme.A" parent="Theme.AppCompat.DayNight.NoActionBar">, then custom drawble takes effect on the button, but then the Material Components I used, convert to normal widgets.
You cannot use custom drawable for MaterialButton or using Material Theme. You can only set solid color states and set them with app:backgroundTint attribute. Ref
Create tint.xml inside res/color/ directory:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_pressed="true"/>
<item android:color="#color/colorSecondary" />
...
</selector>
Use it in your button or button style:
app:backgroundTint="#color/tint"
Related
I am creating a button shape look like WWTBAM game show:
I have created the shape for it but don't know how to put the gradient in it.
This is my styles.xml file I created:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ButtonShapeArrow">
<item name="cornerFamily">cut</item>
<item name="cornerSize">50%</item>
</style>
</resources>
And button in layout_main.xml:
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"
app:shapeAppearance="#style/ButtonShapeArrow"/>
How do I set gradient for that button?
Create a custom background, you can change angle to change to position of the gradient
bg_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#802BC0E4" />
</shape>
I want to have a rounded corner for the items in navigation drawer like this :
it's an example of material design in material.io website
is it possible ?
Just use the app:itemShapeAppearanceOverlay attribute:
<com.google.android.material.navigation.NavigationView
app:itemShapeAppearanceOverlay="#style/ShapeAppearanceOverlay.Nav"
...>
with:
<style name="ShapeAppearanceOverlay.Nav" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
First, I recommend you move to Flutter, it is more intuitive and you got the best integration flow of Material guidelines.
Now, for add round corners, color, font and padding to a checked item with XML and Android Studio, you have the 'app' attributes on NavigationView:
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
...>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
...
app:itemIconTint="#color/custom_color_config"
app:itemTextColor="#color/custom_color_config"
app:itemBackground="#drawable/custom_drawable_resource"
app:itemTextAppearance="#style/custom_style"/>
With itemIconTint and itemTextColor you set the color config of the hole item (icon and text) when is checked or non-checked. First, do res > new > directory, and name directory as 'color'. Then, create the color resource file in color directory with new > color resource file > custom_color_config (name) and put this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="your_checked_item_color"
android:state_checked="true" />
<item
android:color="your_non_checked_item_color"/>
</selector>
The item with the state_checked=true attribute will apply his color to the current navigation checked item.
To add the background rounded box, create in drawable directory a new drawable resource file to set at itemBackground later. So, new > drawable resource file > custom_drawable_resource (name) and put this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="9dp"
android:right="9dp"
android:top="2dp"
android:bottom="2dp">
<shape>
<solid android:color="#color/new_color_resource_name"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
And next, create again a second color resource file in color directory to associate with the solid color attribute in file custom_drawable_resource (new_color_resource_name) and there put this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="your_background_checked_item_color"
android:state_checked="true" />
<item
android:color="your_background_non_checked_item_color"/>
</selector>
And
VOILA! just add to text appearance a custom style with some semi bold font.
PD: Sorry if I've a bad english, I usually read more than I write, regards from MX.
This is done with navigationview and menuItem and create a shape file for the round border and a selected_state file was created with the selected_checked when it is active and when it is deactivated.
I want to have a background color that changes color according to the state of the view.
I tired creating it exactly like the example from Google (except I'm using colors from my resources instead of hardcoding the hex values :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#color/transparent" />
<item
android:color="#color/gray_3"
android:state_selected="true" />
</selector>
(this is located in res/color)
then I set this as a background to my view.but when I run the code, I get a crash with this error:
Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
It's telling me that the "item" tag in my selector needs drawable, not color to define the color.
when I change it to a drawable, as such:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable ="#color/transparent" />
<item
android:drawable ="#color/gray_3"
android:state_selected="true" />
</selector>
that crash goes away, but Android Studio gives me a lint warning saying that "color" is required
my only suspicion is that maybe I'm not supposed to set the view background as a color selector
Try this:
drawable xml code:-
transparent color use like this
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#android:color/transparent"" />
<stroke
android:width="1dp"
android:color="#color/selected" />
<corners android:radius="4dp" />
</shape>
Define #color/selected in color xml.
As Google's documentation here, you should not define the default statement in the first item. Here's the example they provide.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Try using hex color codes instead of color values.
I currently have a set of ToggleButtons, enclosed by a RadioGroup, with each button referring to a selector that changes the color of the button state, and writing code to implement the radio-buttonesque behavior. This works great. (EDIT: I should mention that the selector is what the android:background element of the button is referred to)
The Selector is something like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gold"
android:state_checked="true" />
<item android:drawable="#color/white" />
</selector>
Now I want a second set of buttons to have the exact same behavior. The catch is that this time, I want Images on some of them, not text.
There are lots of StackOverflow posts on how to change the Image based on the Button's state but I don't want to change the image based on the state. I want to change the color of the button based on the state (the images have transparent backgrounds).
I'm really trying to avoid modifying the button images themselves.
I believe the solution involves making a selector with a layer-list but this is new territory for me.
Create a xml named colors.xml in res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
</resources>
In drawable folder, create a xml file my_btn_toggle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#color/red" />
<item android:state_checked="true" android:drawable="#color/green" />
</selector>
and in xml section define your toggle button:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btntoggle"
android:background="#drawable/my_btn_toggle"/>
I think you are looking for tinting attributes - mentioned here.
In your case, try to set ColoStateList to android:tint attribute. In ColorStateList doc you have example that shows color selector - like drawable selector, but for colors.
(I'm not sure it's already support pre-Lollipop devices or you should do it programatically with PorterDuff filters for older versions.)
What apparently one needs to do is create a layered drawable in the selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item android:drawable="#color/gold"
android:gravity="fill"/>
<item android:drawable="#drawable/image"
android:gravity="fill"/>
</layer-list>
</item>
<item android:state_checked="false">
<layer-list>
<item android:drawable="#color/white"
android:gravity="fill"/>
<item android:drawable="#drawable/image"
android:gravity="fill"/>
</layer-list>
</item>
</selector>
The standard approach to variable state button selectors is what Priyank mentions above. Just the graphic must be transparent and declared like above, such that it will be layered with a background color to pull off the color state change
I use this for my own button to set the background for state pressed and unpressed:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/btn1_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn1_unpressed"/> <!-- default -->
But I also like the textColor to become white in state pressed.
I tried to just add textColor="#ffffff" to the item but I guess that's not the correct way, since it did not work.
What would be the best approach without having to code it in each java file that uses this button?
Thanks!
The best is to set Textcolor and Background resource separate.
android:background="#drawable/background"
android:textColor="#drawable/button_text_color"
so your Background: as you wrote it yourself
and Textcolor:
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffffff" />
<item android:color="#ff000000" />
</selector>