Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace "android:drawableLeft" image in CheckedTextView

I set image icon in layout using "android:drawableLeft". This is my layout code:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_marginLeft="8dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:gravity="left"/> 

What I need to do is, change this image into something else using my class. This is the code I used inside my Adapter class which is extend with BaseExpandableListAdapter.

if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);

But this is not replace the used image. Instead replacing it is added to the same line. What should I do to replace the current image with the new one?

like image 345
IBunny Avatar asked Apr 30 '26 02:04

IBunny


1 Answers

In your xml you are using

`android:drawableLeft="@drawable/ic_launcher"`

and in adapter you are doing

((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);

These are two different features of CheckedTextView . CheckMark drawable and drawable are different drawables. To add or change left,right,top,bottom image of CheckedTextView you should use

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

or

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

and to change CheckMark drawable CheckedTextView.setCheckMarkDrawable(R.drawable.ic_folder);

like image 189
Sourabh s Avatar answered May 02 '26 16:05

Sourabh s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!