I've created a custom buttom with a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_np"
android:state_enabled="false"/>
<item android:drawable="#drawable/button_np_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/button_np"
android:state_focused="true"/>
<item android:drawable="#drawable/button_np"/>
</selector>
I would like to be able to colorize this button by using a gray button as image and then colorize it with a color I've defined.
Is that possible?
Thanks
It sure is possible (at least in code), you set a color filter.
import android.graphics.PorterDuff;
Button.getBackground().setColorFilter(0xFF00FF00,PorterDuff.Mode.MULTIPLY); // Green
Button.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY); // Red
Just choose your colors and put them in the setColorFilter parameters.
yes it works very well, i use it in mine, simply create a 9Patch button and put the name of the image in where you have "#drawable/button_np" the "button_pn" is the name of your button image
Related
I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..
I have one button. I have added background image to my button using background attribute. Now when I click on button, I am not getting the default orange color. How can I get that color.
One more query, In the above scenario how can I change the default orange color to some other color.
Try this http://developer.android.com/guide/topics/ui/controls/button.html#Style, change state of your button
You have to make your button custom to do that changes for that create an xml file named custom_btn in yoyr drawable folder and paste the code in it as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="#drawable/untick_btn" /> <!-- default -->
</selector>
and in the button you have to add android:button="#drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
if you're just trying to see the "default orange color" then use an ImageButton and apply your drawable to the src (rather than the background). You will then see the native image background behind your image and it will continue to do whatever it was doing before.
I am not getting the default orange color. How can I get that color.
When you add a background to a button it no more remains a default raw button , It becomes a custom button as you have rendered the default behavior by adding some background to it. Now you need to add color to your custom button on your own because the OS deals with only raw buttons not custom.
How can I change the default orange color to some other color.
To change the button state after its pressed can be done in two ways
1) Either add a background image.
2) or Add a xml to the button.
This is a very nice tutorial on Customizing Android buttons.
In my XML contains one CheckBox. How to change check box border color via programmatically in android.
My Sample ScreenShot is here
ScreenShot
Thanks.
you can set
check.setButtonDrawable(mContext.getResources().getDrawable(
R.drawable.radio));
in radio.xml you can set any background to your check box.
in radio.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/option_unselected" android:state_checked="false"/>
<item android:drawable="#drawable/option_selected" android:state_checked="true"/>
</selector>
here option_selected and option_selected are the graphics for selected and unslected state.
for more you can check http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you
I have many custom buttons (ToggleButton) in my app and want to apply different styles for each button. I created a selector for all the buttons and I currently change only the drawable for the button, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected" />
<item
android:drawable="#drawable/button_gradient" />
</selector>
When I try to change the style the same way:
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected"
style="#style/button_checked />
It does not work, I have tried to change the drawable in the style instead (and just stated the style in the selector), I've also tried to create a separate selector but nothing seems to work.
Any ideas?
Right now it's not possible to do this.