tags:

views:

185

answers:

3

Why changing Color of Default Button makes it look RECTANGLE shape ? I do not want to use custom background images for this. I want to do this programmatically for few conditions on which I change the colors of many small buttons on screen. Can anyone give a solution ?

P.S. ==> It seems there is no workaround by reading this http://groups.google.com/group/android-beginners/browse_thread/thread/e1313e2c98e9c52b

or is there any ?

A: 

You can use the following XML script instead of Assiging colors as backgroud. First of all let me tell you that, the Button view by default is a rectangle so when you just add an button view in your layout, it is having a default background style assigned and because of that you can see it with rounded corners. when you assign the backgroud, the default stype/theme will be overridden and you will get the button in the original form so here you have to apply the theme/stype as per your requirement. If you want it for few buttons then you can use the following XML scrupt. YOu just create an .xml file in your drawable under resources in android project. then assign that .XML file as background of your button view. And see what happens...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <solid android:color="#ffffff"/>
    <corners android:radius="6dp"/>
    <padding android:left="3dp" android:top="2dp"
            android:right="3dp" android:bottom="2dp" />
</shape>
Rahul Patel
Using above xml for button background gives some shape but changing its color to red programmatically again caused its shape become rectangle.
mob_king
A: 

For changing color :

Drawable d = findViewById(R.id.button_name).getBackground();
               PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
               d.setColorFilter(filter);

For removing color :

Drawable d = findViewById(R.id.button_name).getBackground();
               findViewById(R.id.button_name).invalidateDrawable(d);
               d.clearColorFilter();
mob_king