9

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();
Rob
  • 2,243
  • 4
  • 29
  • 40

2 Answers2

21

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.

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
2

You are almost there: setBackgroundResource

setFeeling.setBackgroundResource(R.drawable.appIcon);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97