Rounded corners + background Color Dynamic setting for Android button?

I would like to give below settings for Button in my application. You may think this is a theme change for buttons.

Style Rounded or Regular

Color Red or yellow or blue or any Color code

I know that in Shape defined in XML, I can achieve Rounded Corners. With setBackgroundColor I can set any color as the background.

Problem

Both setBackgroundColor , setBackground override each other depending on how I call them. Therefore, I can not achieve both effects on the same button. How can I achieve these two effects simultaneously. How can I get multiple buttons from the same Button class. Thanks at Advance.

enter image description here

+5
source share
2 answers

You can set the color of the shape and background of the button in the resource with the ability to draw and assign it as android:background in XML or setBackgroundDrawable() in the code.

Below is an example of such a button:

/res/drawable/button.xml
This file should set the rounded shape and background color

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> <corners android:radius="16dp" /> <stroke android:width="3dp" android:color="#ff0000" /> </shape> </item> <item android:state_selected="true"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> <corners android:radius="16dp" /> <stroke android:width="3dp" android:color="#ff0000" /> </shape> </item> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#ff0000" /> <corners android:radius="16dp" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> <corners android:radius="16dp" /> <stroke android:width="3dp" android:color="#0000ff" /> </shape> </item> </selector> 

/res/color/button.xml
(This file should set the text color)

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#0000ff" /> <item android:state_focused="true" android:color="#0000ff" /> <item android:state_selected="true" android:color="#0000ff" /> <item android:color="#000000" /> </selector> 

Then you can initialize the button in your layout as:

 <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/button" android:background="@drawable/button"/> 

The wil button looks like below (by default and pressed states from top to bottom):

enter image description here

+3
source

Well, I did it using GradientDrawable

  int color = Color.rgb(255,0,0); //red for example int radius = 5; //radius will be 5px int strokeWidth = 2; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(color); gradientDrawable.setCornerRadius(radius); gradientDrawable.setStroke(strokeWidth, color); button.setBackground(gradientDrawable); 
+1
source

Source: https://habr.com/ru/post/1203390/


All Articles