Android How to change icon with button

349 views Asked by At

This is my XML

<Button
  android:id="@+id/w1"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:background="@color/buttons_background"
  android:clickable="true"
  android:drawableRight="@drawable/icon_more"
  android:fontFamily="roboto medium"
  android:paddingLeft="30dp"
  android:paddingRight="30dp"
  android:text="@string/w1_btn_more"
  android:textColor="@color/text_color"
  android:textSize="17sp" />

How to change from icon_more to icon_next.

Piece of code Java

Button w1 = (Button) view.findViewById(R.id.w1);
w1.set("???");
7

There are 7 answers

0
N J On BEST ANSWER
Drawable img = getContext().getResources().getDrawable( R.drawable.your_drwable);
img.setBounds( 0, 0, 60, 60 );
imgView.setCompoundDrawables( null, null, img, null );
0
Rishi Paul On

Use This

w1.setBackgroundResource(R.drawable.icon_next);
0
Blackbelt On

use

w1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.next, 0);

to change the android:drawableRight property programmatically. Here the documentation

0
Bidhan On

Since you're using a drawableRight, you might want to use this

w1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_next, 0);
0
Haresh Chhelana On

Try to use setCompoundDrawablesWithIntrinsicBoundsto set respective drawable at run time :

button.setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom);

In your case :

button.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.icon_next,0);
0
anu On

try this

Drawable img = getContext().getResources().getDrawable( R.drawable.icon_next);

img.setBounds( 60, 60, 0, 0 ); txtVw.setCompoundDrawables( img, null, null, null );

0
Suman Dey On

Try This :

w1.setBackgroundResource(R.drawable.icon_next);