यह उदाहरण दर्शाता है कि Kotlin में setCompoundDrawablesWithIntrinsicBounds का उपयोग करके बटन पर बाएं तरफ ड्रॉएबल (आइकन) कैसे सेट करें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - ड्रॉएबल रिसोर्स जोड़ें
अपने प्रोजेक्ट में एक ड्रॉएबल रिसोर्स जोड़ें (उदाहरण: res/drawable/ic_alarm.xml या ic_baseline_alarm_24.xml Vector Asset के रूप में)।
चरण 3 - activity_main.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:text="Android बटन पर DrawableLeft"
android:textColor="@android:color/holo_purple"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="4dp"
android:background="@android:color/holo_blue_light"
android:padding="4dp"
android:text=" अलार्म"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 4 - MainActivity.kt में ड्रॉएबल सेट करें
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val button = findViewById<Button>(R.id.button)
// ContextCompat का उपयोग करके ड्रॉएबल प्राप्त करें (थीम-संवेदनशील)
val drawable: Drawable = ContextCompat.getDrawable(this, R.drawable.ic_baseline_alarm_24)!!
// बाएं तरफ ड्रॉएबल सेट करें (left, top, right, bottom)
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
// वैकल्पिक: ड्रॉएबल और टेक्स्ट के बीच पैडिंग सेट करें
button.compoundDrawablePadding = 16
}
}
चरण 5 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.buttondrawable">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
ऐप चलाएं। बटन पर अलार्म आइकन बाईं ओर और "अलार्म" टेक्स्ट दाईं ओर दिखाई देगा।
XML में विकल्प: लेआउट XML में सीधे सेट करें:<Button android:drawableLeft="@drawable/ic_baseline_alarm_24" android:drawablePadding="16dp" ... />नोट: RTL भाषाओं के लिएdrawableStart/drawableEndका उपयोग करें।