简体   繁体   中英

rounded corner for button not working

I am getting color code #3a87ad from the server to set as the background, and I am also trying to give a rounded corner shape to my button. However it always shows the black color as background.

tv_img_tag = (Button) vi.findViewById(R.id.tv_img_tag);

tv_img_tag.setBackgroundColor(Color.parseColor(product
                .get("stop_status_color")));   

 tv_img_tag.setBackgroundResource(R.drawable.roundedtexts);

     tv_img_tag.setText(product.get("stop_status_name").toString());

roundedtexts.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>


    <corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
    <padding

        android:left="8dp"
        android:top="8dp"
        android:right="8dp"
        android:bottom="8dp" />
</shape>

Hi Brother try this tool and create wonderful android buttons.Tool provides you source code as well. Angry Tools

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="8dp" />

    <solid android:color="#54483C" />

    <padding
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp" />

</shape>

First your roundedtexts.xml has no defined color in drawable. You have to set some color to xml by setting color of < Solid > tag.

Second in your class file. You are setting background color first and then setting drawable to background. So here happening is your last call to set background is set not color.

To do so, you have to get that xml drawable instance in your class into gradientDrawable and then have to set your dynamic color to that gradientDrawable instance and Now you can set that instance to view as background.

Problem is you set color first and then you set background drawable but in roundedtexts.xml you didn't specify any color, so it gives you black background just try to add background color in roundedtexts.xml file like transperent color.

<?xml version="1.0" encoding="UTF-8"?>

<solid android:color="@android:color/transparent"/>

<corners 
    android:topLeftRadius="0dp"
    android:topRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="0dp"/>

<padding 
    android:left="2dp" 
    android:top="2dp" 
    android:right="2dp"                  
    android:bottom="2dp" />

Use below code

tv_img_tag = (Button) vi.findViewById(R.id.tv_img_tag);
tv_img_tag.setBackgroundResource(R.drawable.roundedtexts);
GradientDrawable sd = (GradientDrawable)  tv_img_tag.getBackground().mutate();
sd.setColor(0xff999999);
sd.invalidateSelf();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM