Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Button Drawable Programmatically [duplicate]

I have a button with drawable left:

<Button
    android:id="@+id/post_feeling_btn"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:drawableLeft="@drawable/feeling_btn"
    android:gravity="left|center_vertical"
    android:textColor="@color/gray"
    android:text="Sentimento"/>

In my on resume method, I change the button's text programmatically:

Button setFeeling;
FeelingButton selectedFeeling;

@Override
public void onResume() {
    super.onResume();
    selectedFeeling = ((FeedActivity)getActivity()).getSelectedFeeling();
    if(selectedFeeling != null){
        setFeeling.setText(selectedFeeling.getFeeling());
    }
}

Is there a way to change my drawable left and set the image in the same method, using the image I have in my selectedFeeling object? I'm looking for something like:

setFeeling.setDrawable();
like image 546
Rob Avatar asked Mar 13 '17 20:03

Rob


1 Answers

try something like this: for drawableLeft

int imgResource = R.drawable.your_drawable;
setFeeling.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
setFeeling.setCompoundDrawablePadding(8);     //for padding

Thanks to the Answer Here also.

like image 88
rafsanahmad007 Avatar answered Oct 19 '22 21:10

rafsanahmad007