0

Basically, from what I have noticed is that when you add a background color to a button, the rounded corners element disappears, so basically there are no rounded corners. But when I don't have a background color, the corners of the button are round.

I don't understand whats happening. Please Help

Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
user2005921
  • 39
  • 3
  • 6

3 Answers3

2

The border is part of the default background drawable so you have to replace it by one that contains borders also if this is what you want. Take the default background resource and edit it to have some color. Alternatively you can use

    button = (Button) findViewById(R.id.button);
    button.getBackground().setColorFilter(color, Mode.SRC_ATOP);

which perhaps gives something closer to what you expect.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1
    <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
                <item android:state_enabled="false">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:padding="10dp" android:shape="rectangle">
                        <solid android:color="#FFb888"/>
                        <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
                    </shape>            
                </item>

                <item android:state_enabled="true" android:state_pressed="true">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:padding="10dp" android:shape="rectangle">
                        <solid android:color="#FFD488"/>
                        <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
                    </shape>            
                </item>

                <item android:state_enabled="true">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:padding="10dp" android:shape="rectangle">
                        <solid android:color="#FF8C00"/>
                        <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp"    android:topRightRadius="15dp"/>
                    </shape>            
                </item>
        </selector>

    <Button
    android:id="@+id/btn_add_list"
    android:layout_width="125dp"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_orange_button"
    android:text="@string/action_new_list"
    android:textColor="@color/white"
    android:gravity="center"
    android:textSize="18sp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="10dp"
    android:layout_alignParentRight="true"               
    android:onClick="doNewList"/>

You can save the above xml file that will be the background for your button in a file called rounded_orange_button.xml in the res/drawable folder. So it would be res/drawable/rounded_orange_button.xml. This will also give your button feedback to the user when they select it, follows the dsign guidelines from developer.android.com a little more closely but it is your preference. there are a million ways to skin a cat. Then reference what you created in the drawable folder inside your_layout.xml file by the second example. The color values are optional and can also be set from another resource file in the res/values/colors.xml if need be. Use the res/values/strings.xml for any strings you want displayed inside the button. Hope this helps. Also, the amount of the curve on the buttons can be adjusted by playing with the radius values in the

Sean
  • 196
  • 11
0

The background of the button is going to give you square corners. For a nice custom background, I would suggest making a background image and using as either the button's background or src. So your xml would look like:

android:src="@drawable/mybuttonimage"
android:bacgkround="@null"

or

android:background="@drawable/mybuttonimage"

I would test out both, as they can stretch your image differently.

Another option is to to create a shape in your res/drawable folder.

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#33DDFF" />
        <corners android:radius="4dp" />
    </shape>
Eichhörnchen
  • 592
  • 2
  • 12
  • 27
  • This worked but the button background color is black. How do i change it to a green color – user2005921 Mar 03 '13 at 02:38
  • I edited the post to make it a green color. Change `` to another color code to change the green. Check out the color chart [here](http://www.pagetutor.com/common/bgcolors1536.png). – Eichhörnchen Mar 03 '13 at 16:58