Android button button color changes to button size

I use the built-in theme for my Android app:

<style name="AppTheme" parent="android:Theme.Black"> <!-- Customize your theme here. --> </style> 

I am pleased with this theme, except that I want to change the background color of the button. Here's what it looks like by default:

default

This is what happens when I add a background color to this button ( android:background="@color/play_bg" ):

enter image description here

Hey!? He basically resized all button size, padding and margins!

I managed to get the expected result using the backgroundTint property ( android:backgroundTint="@color/play_bg" ):

enter image description here

Unfortunately, this is only supported from API version 21, which is unacceptable to me.

So, two questions:

  • Why changes in background noise with other button properties?
  • How to get the expected result without backgroundTint ?

And the bonus question: how can I get the expected result programmatically (I have dynamic buttons in my application, so that would be very useful)?

+8
java android button styles
source share
2 answers

You can change this color in your Java file. When you load your main class, you can take the object of this button and then change the color.

Here is how you define this button in the manifest file:

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PLAY" android:id="@+id/btn1" ... /> 

Now, when you add this XML layout to your Java file, you need to

 Button b = (Button)findViewByID(R.id.btn1); b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY); 

You can also use COLOR: RED Sometimes the code doesn’t work for me: - b.setBackgroundColor (int color)

+12
source share

In my case, I will do in this process

 <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@color/play_as" android:padding="8dp" android:text="Button" /> 

Or you can use a link which is an easier way to create buttons.

+1
source share

All Articles