how to set button color - android

I want to custom button like this
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA"/>
<corners android:topLeftRadius="8dip"
android:bottomRightRadius="8dip"
android:bottomLeftRadius="1dip"
android:topRightRadius="1dip"
/>
<item android:drawable="#drawable/photo2"
android:state_pressed="true" />
<item android:drawable="#drawable/photo1" />
but at
<item android:drawable="#drawable/photo2"
android:state_pressed="true" />
<item android:drawable="#drawable/photo1" />
it doesn't work ( if it work it will chang button backgroud when press or un press button )
What should I do?

First Import LightningColorFilter, then you can change the color by applying this code:
Start_Button.getBackground().setColorFilter(new LightingColorFilter(0x11111111, 0x11111111));
This is to be placed inside your acitivity, and not the XML file.

to set the style and color to default button set this code in any new xml and give orgnal button for background as this xml like:
in orignal button layout xml :-
andorid:background="#drawable/mylayout"
in mylayout.xml:-
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#color/yellow1"
android:endColor="#color/yellow2"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#color/orange4"
android:startColor="#color/orange5"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#color/blue2"
android:startColor="#color/blue25"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
and to change on focus button or click event try to set onfocuschnage other layout:
<item android:state_focused="true" >
and set diffrent color to change the button color or style..

Related

How to create Shadow on Android Button (Material Design)

Here i want to ask a simple question that make me so confuse,
i have developing an android app that i want to use material design theme.
But when i try to code the Shadow under the RaisedButton, it's doesn't work.
I mean it just make a stroke around the button and not the shadow.
Please master, help me is there any simple way to make this shadow??
Thanks
NB. This is my custon_button.xml
<item android:state_pressed="true" >
<shape>
<solid
android:color="#color/solidpress"
/>
<stroke
android:width="1dp"
android:color="#color/strokepress" />
<corners
android:radius="2dp" />
<padding
android:left="5dp"
android:top="10dp"
android:right="1dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" > //focused fuck shit man
<shape>
<solid
android:color="#color/solidfocus"
/>
<stroke
android:width="3dp"
android:color="#color/strokefocus" />
<corners
android:radius="2dp" />
<padding
android:left="5dp"
android:top="10dp"
android:right="5dp"
android:bottom="10dp" />
</shape>
</item>
<item> // normal
<shape>
<solid
android:color="#color/solidnormal" />
<stroke
android:width="1dp"
android:color="#color/strokenormal" />
<corners
android:radius="2dp" />
<padding
android:left="5dp"
android:top="10dp"
android:right="1dp"
android:bottom="10dp" />
</shape>
</item>
and i apply that code to my activity with
android:background="#drawable/custom_button
Please look this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"><layer-list>
<item><shape>
<gradient android:angle="270" android:centerColor="#00c0f0" android:endColor="#036b86" android:startColor="#01bff3"></gradient>
<corners android:radius="5dp" />
</shape></item>
<item android:bottom="3dp"><shape>
<gradient android:angle="270" android:centerColor="#00c0f0" android:endColor="#01bff3" android:startColor="#01bff3"></gradient>
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
<corners android:radius="5dp" />
</shape></item>
</layer-list></item>
<item android:state_pressed="true"><layer-list>
<item ><shape>
<gradient android:angle="270" android:centerColor="#00c0f0" android:endColor="#01bff3" android:startColor="#036b86"></gradient>
<corners android:radius="5dp" />
</shape></item>
<item android:top="3dp"><shape>
<gradient android:angle="270" android:centerColor="#00c0f0" android:endColor="#01bff3" android:startColor="#01bff3"></gradient>
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
<corners android:radius="5dp" />
</shape></item>
</layer-list></item>
</selector>
If rajan ks' answer does't work, you can try wrapping the button in a CardView (android.support.v7.widget.CardView), and give the CardView some elevation using its app:cardElevation attribute.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="false">
<Button />
</android.support.v7.widget.CardView>
shadow.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:right="5dp" android:top="5dp">
<shape>
<corners android:radius="3dp" />
<solid android:color="#D6D6D6" />
</shape>
</item>
<item android:bottom="2dp" android:left="2dp">
<shape>
<gradient android:angle="270"
android:endColor="#E2E2E2" android:startColor="#BABABA" />
<stroke android:width="1dp" android:color="#BABABA" />
<corners android:radius="4dp" />
<padding android:bottom="10dp" android:left="10dp"
android:right="10dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
and Add this code to your Button
android:background="#drawable/shadow"
Have a look at this for Material Kind of Shadow: Shadow Background Android

Custom button in android & giving custom border color

I am Trying to get a clickable effect for a white button that shows clickable effect on clicking it
My button
What i have tried is
I have achieved clickable effect for the button using a selector as below
black_button_for_account.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#343434" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:angle="270"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<corners android:radius="4dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</selector>
How to give a border of like 3dp size for the button retaining the above effect color for the border like be black?
just change the following
<stroke
android:width="3dp"
android:color="#000" />

Button setPressed() onClick

this is an easy question,
in my xml file i have :
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/Bf"
android:background="#drawable/button_purple"
android:layout_weight="1"
android:textColor="#ffffff"
android:onClick="action"
/>
And in my activity i have that :
public void action (View v)
{
s = "m";
changeCouleur("blue");
v.setPressed(true);
}
When i pressed the button it's working but the button don't stay pressed.
I don't use an image this is what i use for the color :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#449def"
android:endColor="#2f6699"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Thanks in advance if you noticed something wrong.
Please anyone have any idea ?
From what I understand is that you are trying to use a button to turn a state off/on, also the button's state will clearly indicate the feature's state.
If I am correct then use custom check box. You will have to anyways define selector for different states of checkbox (as mentioned by user1071979).
you have to use two images to do this.
button_normal
button_pressed
then create a xml resource in drawable folder
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/button_normal" />
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
</selector>
then, set this file as a background for the imageview. here we are using imageview as button. dont forget to include those two buttons in the drawable folder.

How can I change the font padding of an Android button?

I want to remove the font padding from an Android button?
I've tried set includeFontPadding to false, but it has no effect.
How can i change the font padding of the button?
You need to define a seperate drawable resource for the button something like this.
For example this is button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#F5B800"
android:endColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#F5B800"
android:startColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#ff9900"
android:startColor="#ffcc00"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Here you can arrange the padding as you want for the button.
And when you are declaring the button use this as the background.
Something like.
<Button android:id="#+id/sample_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#FFFFFF"
android:background="#layout/button_layout"
android:text="Sample Button" />
You can try using android:includeFontPadding. set it to false. also set the padding to 0.

Android - How combining Shape drwable and text color different states for button?

I have some troubles managing Android State List for a button. I specified some Shape drwable item for different states, but i also need to change textColor depending on the current state.
My actual state list xml is:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/light_gray"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/classic_red1" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/classic_red1"
android:endColor="#color/classic_red2"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#color/white" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
I need also to change textColor based on these 2 states.
Thanks in advance.
selector for the button BG:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/light_gray"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/classic_red1" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<solid android:color="#424242" /> //another custom shape here for focus state
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/classic_red1"
android:endColor="#color/classic_red2"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#color/white" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
selector for the button text color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" /> <!-- pressed -->
<item android:color="#color/black" /> <!-- default/unchecked -->
</selector>

Categories

Resources