How to programmatically set drawableLeft on Android button in Kotlin?

MainActivity.kt
package com.example.espl.drawableleftkotlinbutton
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /**
         * Sets the Drawables (if any) to appear to the left of, above, to the
         * right of, and below the text. Use 0 if you do not want a Drawable there.
         * The Drawables' bounds will be set to their intrinsic bounds.
         * <p>
         * Calling this method will overwrite any Drawables previously set using
         * {@link #setCompoundDrawablesRelative} or related methods.
         *
         * @param left Resource identifier of the left Drawable.
         * @param top Resource identifier of the top Drawable.
         * @param right Resource identifier of the right Drawable.
         * @param bottom Resource identifier of the bottom Drawable.
         */

        button1.setOnClickListener(View.OnClickListener {

            //Programmatically set drawableLeft on Android button - setting the smiley PNG image
            button2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
//            or
//            val drawable = ContextCompat.getDrawable(this, R.drawable.smiley)
//            button2.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)

            //Programmatically set drawableLeft on Android button - setting the andriod VectorDraawable
                        button3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_android_black_24dp, 0, 0, 0);
//            or
            //            val vectorDrawable = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp)
//            button3.setCompoundDrawablesWithIntrinsicBounds(vectorDrawable, null, null, null)
        })

    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:padding="20dp"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button
            android:layout_gravity="center"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to programmatically set drawableLeft on below buttons"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_centerInParent="true"
            android:layout_below="@id/button1"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON 2 -set png image"
            android:drawablePadding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_centerInParent="true"
            android:layout_below="@id/button2"
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON 3 -set VectorDrawable"
            android:drawablePadding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</RelativeLayout>

Testing devices:
//Tested on below devices working fine on both devices (tested for above kotlin code)
// HTC Desire 620G - Android OS - Android Version - 4.4.2 - KitKat API 19
// RedMi note 3 - Android OS - Android Version - 6.0.1 - Android – Marshmallow - API level 23
It's working on Kitkat device When you are using an android vector drawable, So the above Kotlin code has backward compatibility for API below 21

No comments:

Post a Comment

Popular Posts