Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round cornered button with background color in android

I need to do round cornered button with background color change in android.

How could i do that?

Example link/code is much appreciated.

like image 920
sanjay Avatar asked Mar 23 '11 04:03

sanjay


People also ask

How can change background color of button in Android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

What is default color of button in Android?

When I open a new android studio project, the default color for button is purple.


2 Answers

You want to use Android's Shape Drawables. http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

drawable/cool_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="@dimen/corner_radius" />
    <gradient
        android:angle="270"
        android:startColor="@color/almost_white"
        android:endColor="@color/somewhat_gray"
        android:type="linear" />
</shape>

Then you'd have to create a "selector" drawable from those shape drawables. This allows you to make the button appear different depending on the state. IE: Pressed, focused, etc. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

drawable/cool_button.xml

<?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/cool_inner_press_bottom" />
    <item android:state_focused="true" android:state_enabled="true"
        android:state_window_focused="true"
        android:drawable="@drawable/cool_inner_focus_bottom" />
    <item
         android:drawable="@drawable/cool_button_background" />
</selector>

Bonus: You might want to create a style for the button so you can have them be consistent throughout the program. You can cut this step out and just set the button's android:background="@drawable/cool_button".

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyCoolButton">
        <item name="android:background">@drawable/cool_button_background</item>
    </style>
</resources>

Finally, the button!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/appwidget_bg">
    <Button
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_weight="wrap_content"
        style="@style/CoolButton"
        />
</LinearLayout>
like image 177
Jeremy Edwards Avatar answered Nov 16 '22 01:11

Jeremy Edwards


Import PorterDuff and use setColorFilter() as follows

import android.graphics.PorterDuff.Mode;

Button btn = (Button) findViewById(R.id.myButton); 
btn.getBackground().setColorFilter(Color.GRAY, Mode.MULTIPLY);
like image 43
Romah Avatar answered Nov 16 '22 01:11

Romah