Is there a way to create Android Button that have bottom margin when state_pressed is false, and that have rounded corners without bottom margin when state_pressed is true?
I have this, but it's without bottom margin when state_pressed is false:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
android:shape="rectangle">
<solid android:color="#B5B5B5" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape
android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</selector>
EDIT: Added pictures
android:state_pressed="false"
android:state_pressed="true"
<selector xmlns:android="schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_for_pressed_state" android:state_pressed="true"/>
<item android:drawable="#drawable/drawable_for_normal_state" android:state_pressed="false"/>
</selector>
Add this code your drawable folder and use separate button drawables for the different button state
Related
So i have 4 buttons, and i want to make if one button is clicked by the user it will change its color to red and stay that way until if the user pressed the other 3 buttons
enter image description here
i have read other post about this, but they showed to me to use selector state_selected and etc, but it doesnt work for me. and the other use setBackground in the java code, but i will be using 8 buttons and probably more, and its not going to be efficient to do it that way, is there any more efficient way to do this?
this is the code i have on the drawable xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<solid android:color="#color/red_maroon"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle" >
<solid android:color="#color/red_maroon"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/white"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
</selector>
If selector state_selected does not work, check if your button style has this attribute: <item name="android:selectable">true</item>
The style:
<style name="AppButtonOutlineSelectable" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearance">#style/AppButtonShape</item>
<item name="strokeColor">#color/color_selectable_button_stoker_selector</item>
<item name="android:selectable">true</item>
<item name="strokeWidth">1dp</item>
<item name="backgroundTint">#color/color_selectable_button_bg</item>
</style>
<style name="AppButtonShape" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerSize">8dp</item>
</style>
And use color selector under color directory, not drawable selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red" android:state_selected="true" />
<item android:color="#color/white" />
</selector>
The button:
<com.google.android.material.button.MaterialButton
android:id="#+id/btn"
style="#style/AppButtonOutlineSelectable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Set onClick:
btn.setOnClickListener {
btn.isSelected = btn.isSelected.not()
}
I have a num keypad that I have created using buttons and have hide my android soft keyboard.
This is how my every button looks like for different numbers
<Button
android:id="#+id/seven"
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/CustomStyle"
android:background="#drawable/gridlayout_border"
/>
Notice that I am using CustomStyle whose code looks like this with the filename button_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomStyle">
<item name="android:textSize">24dp </item>
<item name="android:textColor">#fdfdfd</item>
<item name="android:textStyle">bold </item>
<item name="android:padding">20dp</item>
</style>
</resources>
Also, I am using gridlayout_border as a background
gridlayout_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-10dp" android:right="0dp" android:top="-10dp"
>
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<stroke android:color="#ffffff" android:width="2dp"/>
</shape>
</item>
</layer-list>
Since, I have used up the style and background tag in button. How can I now apply one more style/color when my button is being pressed on num keypad?
Thanks in advance
You can use selector to determine different states of a view. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<!-- Write your focused state code-->
</item>
<item android:state_pressed="true">
<!-- Write your pressed state code-->
</item>
<item>
<layer-list>
<item android:left="-10dp" android:right="0dp" android:top="-10dp"
>
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<stroke android:width="2dp" android:color="#ffffff"/>
</shape>
</item>
</layer-list>
</item>
</selector>
I have a specific need to texcolorlist put the drawable with the background button.
My 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="#android:color/black" />
<item android:state_checked="true" android:color="#android:color/black" />
<item android:color="#8e000000"/>
</selector>
My Button State:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#005b7f"
android:startColor="#051725"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#e6e6e6"
android:startColor="#b3b3b3"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</selector>
I know that in this way I can put the button color: android:textColor="#drawable/button_text_color"
I wonder if the button text color can be put directly into My Button State.
This is something, but it does not work:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:textColor="#000000">
<gradient
android:angle="90"
android:endColor="#e6e6e6"
android:startColor="#b3b3b3"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</selector>
You need to define selector also to the text color:
android:textColor="#drawable/yourselector"
Here example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#color/blue" />
<item android:state_focused="true" android:state_pressed="true" android:color="#color/red" />
<item android:state_focused="false" android:state_pressed="true" android:color="#color/green" />
</selector>
Edit: I didn't saw I refereed to that possibility. I don't think what you want is possible, because what you define in your selector refer to the android attribute you assign it. Since the attribute is background there is no meaning to text color there.
I wonder if the button text color can be put directly into My Button State.
No, it cannot. Drawable XML resources can not set properties on the elements they are applied to. What you might be interested in, is using a Style.
values/styles.xml
<style name="MyButtonStyle">
<item name="android:textColor">#drawable/button_text_color</item>
<item name="android:background">#drawable/button_background</item>
</style>
MyLayout.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyButtonStyle"/>
This question was answered before:
Android customized button; changing text color
Hope it will help.
After having been looking for a while I've not been able to find an answer to this...
I have a recycler view with items which when selected have a red background and white text (beforehand the background is white and text is black). To do this I am using a selector.
I have recently tried to add a ripple effect to this, but unless I long click on the item the background of the item goes straight to red without the ripple. I am assuming this is because the selector state state_selected overrides the ripple on sate_pressed?
Does anyone know if there is a way around this? Here is the selector code I use:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_red_dark" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/ripple"
android:state_pressed="true"/>
<item
android:drawable="#android:color/holo_red_dark"
android:state_selected="true"/>
<item android:drawable="#android:color/white"/>
</selector>
</item>
</ripple>
Thanks in advance!
To create a selector background that has a ripple effect and shows selected status I do the following:
Start by defining your highlight color, with some transparency:
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="selector_color">#660000ff</color>
</resources>
You probably want to have compatibility pre-lollipop. Put a typical old-school selector inside drawable folder:
drawable/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selector_color" android:state_pressed="true"/>
<item android:drawable="#color/selector_color" android:state_selected="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
And then add the following layer drawable inside drawable-v21 folder:
drawable-v21/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_selected="true"
android:drawable="#color/selector_color" />
<item android:drawable="#android:color/transparent" />
</selector>
</item>
<item>
<ripple android:color="#color/selector_color">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
</item>
</layer-list>
Now you can use #drawable/selector_background for your selector.
So I have another case in which I had to use selector as well as layer list for that
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<!-- ripple color -->
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/c_unread_notifications_item" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<!-- ripple color -->
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
</selector>
This worked, for your need what you can do it just replace the item under ripple with your item shape if you don't have any layering.
Hope this helps
It will be better if you wrap your recyclerview item view in FrameLayout and set android:background="?selectableItemBackground" of FrameLayout and the child layout of FrameLayout background="#drawable/background"
background.xml
<item android:drawable="#color/red" android:state_selected="true"/>
<item android:drawable="#color/red" android:state_focused="true"/>
<item android:drawable="#color/red" android:state_pressed="true"/>
<item android:drawable="#color/white"/>
And then child layout must has attribute android:duplicateParentState="true"
The goal of my selector (applied to an ImageView) is to change both color background and image when the state is pressed. The following xml works correctly. The problem is that changing the size of the background also scales up the drawable in the center. How can I keep the the drawable size and the background independent?
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false">
<layer-list>
<item>
<shape android:shape="oval">
<size android:width="100dp" android:height="100dp"/>
<solid android:color="#android:color/black"/>
</shape>
</item>
<item android:drawable="#drawable/ic_white"/>
</layer-list>
</item>
<item android:state_enabled="true" android:state_pressed="true">
<layer-list>
<item android:drawable="#android:color/white" />
<item android:drawable="#drawable/ic_black" />
</layer-list>
</item>
</selector>