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));
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 );
Try this,
Drawable icon= getContext().getResources().getDrawable(R.drawable.icon);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With