Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button and drawable left

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));
like image 461
edi233 Avatar asked May 14 '12 09:05

edi233


2 Answers

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 );
like image 85
Dhruvil Patel Avatar answered Sep 28 '22 18:09

Dhruvil Patel


Try this,

Drawable icon= getContext().getResources().getDrawable(R.drawable.icon);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
like image 34
user370305 Avatar answered Sep 28 '22 19:09

user370305