I have a button in android which I have made circular in shape. Also I have an xml which changes the background color on pressed, but the color goes back to normal after the pressed state is changed.
I made the following change to code
arg0.setBackgroundColor(getResources().getColor(R.color.greenText));
But now the background takes rectangular shape and not the oval shape.
Okay as you have said the background shape changes when using setBackgroundColor, i think here's what you want,
1.you might be interested in color filters like this
Button btn = (Button) findViewById(R.id.button1);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
You use different values according to your required colour.If you want to know the constant values of colours, you can refer the documents.
2.you can programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
For example for a red shaded button
btn.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
For a green shaded button
btn.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
And so on.What it actually does is, it works by multiplying the current button colour value by your colour value.
3.You can also use an online tool like this Android Button Maker to customize your button and use android:background="#drawable/custom_btn" in your layout(inside the tag) to define the customized button.
Now i believe there are more ways to achieve what you want but i think these are some easy and quick fixes you can use.Hope this helps.
Related
I have an android application where when I click on a button (made with a Textview) I have a brief change background color (black to green) before changing of activity.
Sometimes, things go wild and the green color spread on other elements.
Green color everywhere instead of black background with bottom button which can become green on click:
Actually I am not sure at all that the green come from the button on click color. It might be another reason.
How can I get rid of this bug.?
Thank you for your help
I finally forced the background color programmatically in the onCreate...
I want the button to appear as though it has space in between the bottom of it's face and the canvas. Then, when clicked, it will appear as though there is no space between the button and the canvas. I know I can style the button with xml to give it those 2 looks but I don't know how to do the elevated look.
My question is how would one go about doing this?
Easiest approach to this is creating 2 background images, each fitting the state you want, then you can either set the backgrounds according the button's state, or change the backgrounds by setting an onTouchListener to the button.
I am using the same background shape in a layer list for each button, then in that layer list a specific image related to the function of that button.
I know that I can create a selector for each button, however is there any way to reuse a selector and dynamically change the image (normal, focused and pressed all use the same image) for each button?
Even if there is no way to reuse a selector, are there any other good approaches for reuse and as opposed to creating 3 separate layer-list drawables and a selector for each button?
Cheers
EDIT: The button each have a text component
use ImageButton
android:background="#drawable/yout_selector"
android:src="drawable/image"
I am creating a MCQ application in which there are four option Buttons.Initially all the buttons's color is default color.Whenever user clicks on correct answer background color of the Button changes to green and when user clicks on wrong option Background color of the Button changes to red and display correct answer Button in green. After that user clicks on next question button. Now I want to change the background color of all the optionButtons to its initial state i.e default color. I don't know how to do it. Please help me in changing the Button image to its default color/image.
Inside the onClick() of the NEXT BUTTON place a the code to change the option button colors to the colors of your choice.
onClick()
{
button.setBackgroundColor(color);
}
The default color is not a color, it is StateListDrawable of many 9-patch images
I suggest you set the background of buttons to be white for option buttons and then set it back to white when NEXT button is pressed.
I am using a button with my own back ground color in android and that button is not getting focus what is the problem?
Buttons normally will not have focus by touch. Also focus depends on the background.
So, check the background - it needs to be of type drawable with multiple states or you need to make a gradient. Check Standard Android Button with a different color