change button text color when pressed
I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?
5 Answers 5
Yes, you can do it like that:
See the section called State List in this bit of documentation. Drawable Resources.
You can define two different Button xml files one for the transparent ‘default’ state and another with the button as Red for your ‘pressed’ state. You then define a selector which switches the drawable resources in the different states.
EDIT: As per devunwired’s comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.
I like the solution proposed by Konstantin Burov in the other issue: Android customized button; changing text color
You can actually manage more states than just pressed and normal. But it should solve the problem.
Then you can use that selector drawable in your button changing the text color attribute like below. Note that the selector in the example below is named «button_text_color»
Using the same drawable approach you can also solve the background color of the button. Just remember that in the selector instead of using the «android:color» attribute you need to use the «android:drawable» attribute like below.
And then in the button itself do, note that this time the selector name is «button_background»
Источник
Android : change button text and background color
How can I change both text and background colors when my button is pressed, with xml ?
To change text color I can do :
To change the background I can do (using it in a selector/item with drawable reference) :
But how can I do both ? Let’s say I want to have :
- Default : black text / white background
- Pressed : white text / blue background
EDIT : answer
I totaly forgot that the background and text color are managed separately, so this is how I did it :
In mybackgroundcolors.xml I manage the background and in filtersbuttoncolors.xml I manage the text color. In both xml files I manage the status (pressed, selected, default)
7 Answers 7
Since API level 21 you can use :
you only have to add this in your xml
Here is an example of a drawable that will be white by default, black when pressed:
I think doing this way is much simpler:
And you need to import android.graphics.Color; not: import android.R.color;
Or you can just write the 4-byte hex code (not 3-byte) 0xFF000000 where the first byte is setting the alpha.
Just complementing @Jonsmoke’s answer.
For API level 21 and above you can use :
in XML for the button layout.
For API level below 21 use an AppCompatButton using app namespace instead of android for backgroundTint.
add below line in styles.xml
in button, add android:theme=»@style/AppTheme.Gray» , example:
When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.
name=»Theme.AppBaseTheme» means that you are creating a style that inherits all the styles from parent=»@android:style/Theme.Light» . This part you can ignore unless you want to inherit from AppBaseTheme again. =
@drawable/custom_background is a custom image I put in the drawable’s folder. It is a 300×300 png image.
#295055 is a dark blue color.
My code changes the background and text color. For Button text, please look through Google’s native stlyes (the link I gave u above).
Then in Android Manifest, remember to include the code:
Источник
How to change Android button click color
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.
5 Answers 5
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:
and in the button you have to add android:button=»@drawable/custom_btn»
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.
Источник
Standard Android Button with a different color
I’d like to change the color of a standard Android button slightly in order to better match a client’s branding.
The best way I’ve found to do this so far is to change the Button ‘s drawable to the drawable located in res/drawable/red_button.xml :
But doing that requires that I actually create three different drawables for each button I want to customize (one for the button at rest, one when focused, and one when pressed). That seems more complicated and non-DRY than I need.
All I really want to do is apply some sort of color transform to the button. Is there an easier way to go about changing a button’s color than I’m doing?
20 Answers 20
I discovered that this can all be done in one file fairly easily. Put something like the following code in a file named custom_button.xml and then set background=»@drawable/custom_button» in your button view:
Following on from Tomasz’s answer, you can also programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
If you start with a standard grey shaded button:
will give you a red shaded button,
will give you a green shaded button etc., where the first value is the colour in hex format.
It works by multiplying the current button colour value by your colour value. I’m sure there’s also a lot more you can do with these modes.
Mike, you might be interested in color filters.
try this to achieve the color you want.
This is my solution which perfectly works starting from API 15. This solution keeps all default button click effects, like material RippleEffect . I have not tested it on lower APIs, but it should work.
All you need to do, is:
1) Create a style which changes only colorAccent :
I recommend using ThemeOverlay.AppCompat or your main AppTheme as parent, to keep the rest of your styles.
2) Add these two lines to your button widget:
Sometimes your new colorAccent isn’t showing in Android Studio Preview, but when you launch your app on the phone, the color will be changed.
Sample Button widget
You can now also use appcompat-v7’s AppCompatButton with the backgroundTint attribute:
I like the color filter suggestion in previous answers from @conjugatedirection and @Tomasz; However, I found that the code provided so far wasn’t as easily applied as I expected.
First, it wasn’t mentioned where to apply and clear the color filter. It’s possible that there are other good places to do this, but what came to mind for me was an OnTouchListener.
From my reading of the original question, the ideal solution would be one that does not involve any images. The accepted answer using custom_button.xml from @emmby is probably a better fit than color filters if that’s your goal. In my case, I’m starting with a png image from a UI designer of what the button is supposed to look like. If I set the button background to this image, the default highlight feedback is lost completely. This code replaces that behavior with a programmatic darkening effect.
I extracted this as a separate class for application to multiple buttons — shown as anonymous inner class just to get the idea.
Источник
How to change button color
I have few buttons. I’d like to change color of a button when it is pressed. And to change color of other buttons to normal state.
The problem is that after simple changing the background of a button, it’s shape is also changed. After changing the color the default android button with shadows is just replaced with painted rectangle with more button height. Yes. It is possible to define button in normal and pressed positions as drawable XMLs. But I’d like to use default android buttons instead of fancy mine.
My code is below:
My app after start looks as follows pic1 (Sorry I have not permission to post images)
After I press a button it is as follows: pic2
Very strange, that the size of the buttons was changed.
There are many similar questions in google. But it seems they do not suit me. For example this link. But there is not written how to define normal or pressed button in drawable.
So. What is the best way to change (switch) colors of a few buttons. Thanks!
6 Answers 6
create a new xml file under your drawable resourses with the properties you want to apply to your button. for example.
then in the on click listener aplyy it like this.
Yeah, don’t try to change the button color in your onClick event.
What you are looking for is a StateListDrawable . As an example, check out how the Android platform defines the drawable btn_default_holo_light. You will want to follow exactly this pattern, providing your own drawables (e.g. PNG images) for the different states.
Once you’ve got these drawables created, you simply reference it in your layout file like this
You can do it like this,create a xml file int project’s res/drawable,eg:»buttonGenderAny_bg.xml»:
then set button’s background in layout xml file,eg:
Actually I also did some thing like this a few months back with buttons, but I don’t recommend it. I think for your situation it is much simpler to use radio buttons.
This is what it looked like!
The advantage of using the RadioGroup method is that you do not have to manage the color state of your «buttons»
To setup the following 3 or 4 files are needed (This is my EXACT usage, but I have also provided a few hints on how to get it like how you want it):
HINT: Above I wanted mine to look like a bar, for yours switch the android:orientation=»horizontal» to vertical.
values\style.xml (To bring together all the styling for the radio buttons)
What is important to note is the android:button above, this hides the checkbox of the radiobutton You can also remove the android:layout_weight I used this in my bar so that all the radio buttons would get equal width. The parent=»@android:style/Widget.Holo.Button.Borderless» will allow you to keep the buttons the same size.
The same can be done for the @color/menu_button_text. (For example you could invert the color of the text when it is selected)
Then to know which one is selected in the onCheckedChanged event I did this.
Источник