How to programmatically set drawableLeft on Android button?

set drawable left programmatically android textview
set drawable padding programmatically android
set drawable programmatically android
android:drawableleft
android edittext drawableright click programmatically
set drawable tint programmatically android
how to remove drawable right programmatically in android
set drawable left programmatically android kotlin

I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic.

<Button
    android:id="@+id/buttonIdDoesntMatter"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="buttonName"
    android:drawableLeft="@drawable/imageWillChange"
    android:onClick="listener"
    android:layout_width="fill_parent">
</Button>

This is what I have so far. I can do everything but the drawable.

linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
    new LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT,         
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT
    )
);      

linear.addView(button);

You can use the setCompoundDrawables method to do this. See the example here. I used this without using the setBounds and it worked. You can try either way.

UPDATE: Copying the code here incase the link goes down

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );

or

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

or

txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

How to programmatically set drawableLeft on Android button?, This example demonstrates how do I programmatically set drawableleft in android.Step 1 − Create a new project in Android Studio, go to File  This example demonstrates how do I programmatically set drawableleft in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java


Simply you can try this also

txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);

Android Note 49 : How to programatically set DrawableLeft on a , How to programmatically set drawableLeft on Android button? <Button. android:id="@+id/buttonIdDoesntMatter" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="buttonName" android:drawableLeft="@drawable/imageWillChange" android:onClick="listener" android:layout_width="fill_parent"> How to programmatically set drawableLeft on How to programmatically set drawableLeft on Android button? 0 votes . 1 view. asked Jul 24, 2019 in Java by Nigam (4.1k


For me, it worked:

button.setCompoundDrawablesWithIntrinsicBounds(com.example.project1.R.drawable.ic_launcher, 0, 0, 0);

android, Both are the same thing actually but i prefer option 1 because it is much easier to read. They key point is .setCompoundDrawablesWithIntrinsicBounds. use this  This example demonstrates how do I set background drawable programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java


Kotlin Version

Use below snippet to add a drawable left to the button:

val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)

.

Important Point in Using Android Vector Drawable

When you are using an android vector drawable and want to have backward compatibility for API below 21, add the following codes to:

In app level build.gradle:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

In Application class:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
    }

}

Android Gotchas: #1 Setting a Drawable on a TextView , android - Set Button DrawableLeft programmatically. activity_main.xml. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout  Actually, DrawableLeft/Right’s usage is not very common, developers/designers are not using it so much but even if you’re using might be you could face this problem :)


myEdtiText.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley,0, 0, 0);

java, Android Gotchas: #1 Setting a Drawable on a TextView programmatically. AG is a mini-series on the quirks and idiosyncrasies of Android app development.. This example demonstrates how to set the part of the Android text view as clickable in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml


How to programmatically set drawableLeft on Android button?, I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic. This is what I have so far. I can do​  I have a textView in xml here. As you can see I set the DrawableLeft in xml. I would like to change the drawable in code. Is there anyway to go about doing this? Or setting the drawableLeft in code for the text view? Tip: Whenever you know any XML attribute but don't have clue about how to use it at runtime. just go to the description of that


How to programmatically set drawableLeft on Android button?-Msrblog, This example demonstrates how do I programmatically set drawableleft in android.Step 1 − Create a new project in Android Studio, go to File  As cephus mentioned android:drawablePadding will only force padding between the text and the drawable if the button is small enough.. When laying out larger buttons you can use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button.


java: How to programmatically set drawableLeft on Android button?, I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic. How to customize a button to set text and color in Android? How to programmatically turn on Wifi on Android device? Disable Orientation Change in iOS; HTML DOM click() method; How to change the text color of Menu item in Android? How to get Spinner Value in android? How to programmatically set drawableLeft on Android button?