Button and drawable left

android, android-layout

Solution

Drawable image = getContext().getResources().getDrawable( R.drawable.icon );
image.setBounds( 0, 0, 60, 60 );
button.setCompoundDrawables( image, null, null, null );

do this

Update:

Since `getContext().getResources().getDrawable` is now deprecated, use this instead:

  Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
  image.setBounds( 0, 0, 60, 60 );
  button.setCompoundDrawables( image, null, null, null );

Problem

I create button in xml file like this: ``` <Button android:id="@+id/call_button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="30dp" android:background="@drawable/button" android:layout_weight="40" android:drawableLeft="@drawable/symbol_phone" android:paddingLeft="20dp" android:drawablePadding="-25dp" android:text="Call" android:textColor="@color/white" /> ``` I would like to know how I can do drawableLeft in activity. I know that is stupid but I need do this in activity because I create button there. How I can do the same what I have in xml file in my activity? I need add drawableLeft and drawable padding and padding left. This is how I create button in activity ``` Button button1 = new Button(this); button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth, buttonHeight)); button1.setText(systemTexts.getShowCallButton()); button1.setBackgroundDrawable(new button1.setTextColor(Color.parseColor(buttonTextColor)); ```

Original source