0

I''m using a drawable for the cells in my table layout in my Android application. I want to change the cell colors on click event. Is it possible to change the color of the drawable in run time. my drawable is,

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle"  >
        <solid android:color="#FFFFFF"/>
        <stroke android:width="1dp"  android:color="#483D8B"/>
</shape>

And I want to change the white color "#FFFFFF" with the click event. Can Java edit the color in the XML drawable file? I just edited the background color and it dispersers the borders of the drawable.

String StatusColor=GetColor(Retailer_x.getStatus());
Stts.setBackgroundColor(Color.parseColor(StatusColor));
halfer
  • 19,824
  • 17
  • 99
  • 186
Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59

2 Answers2

4
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
                        sd1.getPaint().setColor(CommonUtilities.color);
                        sd1.getPaint().setStyle(Style.STROKE);
                        sd1.getPaint().setStrokeWidth(CommonUtilities.stroke);
                        sd1.setPadding(15, 10, 15, 10);

                        sd1.getPaint().setPathEffect(
                                new CornerPathEffect(CommonUtilities.corner));
                        ln_back.setBackgroundDrawable(sd1);
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
  • this is fine. But got some problem. I edited the CommonUtilities.stroke as "5". and sets the releven color for CommonUtilities.color. But it gives only the borders. Other part is empty. can you help me to fix this. Thank you!!! – Samantha Withanage Jan 21 '14 at 06:26
  • I fix it. I used a LinearLayout and add the textView into it. Set the background color for LinearLayout and set the drawable to textView. Thanks a lot!!!!! – Samantha Withanage Jan 21 '14 at 09:10
  • sd1.getPaint().setStyle(Style.STROKE); replace with sd1.getPaint().setStyle(Style.FILL_AND_STROKE); – Shankar Agarwal Sep 27 '14 at 06:47
1

Use selector of the Table Cell background to change state of the table cell when user click it.

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


    <!-- selected -->
    <item android:drawable="@drawable/red_drawable" android:state_selected="true"/>
    <!-- focused -->
    <item android:drawable="@drawable/gray_drawable"/>
    <!-- default -->

</selector> 
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
  • no no. I mean another thing. There are a number of states that the cell can represent and on click I can change the state. So I need to change the color on that state. any idea... Thank you!! – Samantha Withanage Jan 21 '14 at 06:13