Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android बटन पर प्रोग्रामेटिक रूप से drawableLeft कैसे सेट करें?

यह उदाहरण दर्शाता है कि 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 का उपयोग करें।
  1. Android में बटन का उपयोग करके स्क्रीन ओरिएंटेशन प्रोग्रामेटिक रूप से कैसे बदलें Android में बटन का उपयोग करके स्क्रीन ओरिएंटेशन प्रोग्रामेटिक रूप से कैसे बदलें

    परिचय`setRequestedOrientation()` का उपयोग करके एक्टिविटी की ओरिएंटेशन को रनटाइम पर बदला जा सकता है। यह लेख दो बटनों के साथ पोर्ट्रेट और लैंडस्केप मोड के बीच स्विच करने का उदाहरण दिखाता है।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<Button android:id=@+id/buttonSetPotrait android:text=Click here c

  1. Android बटन पर प्रोग्रामेटिकली drawableLeft (आइकन) कैसे सेट करें? Android बटन पर प्रोग्रामेटिकली drawableLeft (आइकन) कैसे सेट करें?

    Android Button में setCompoundDrawablesWithIntrinsicBounds() से आइकन सेट करना XML के बजाय Java कोड से बटन के बाएं/दाएं/ऊपर/नीचे ड्रॉएबल (आइकन) प्रोग्रामेटिकली जोड़ने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट में बटन (XML में drawableStart पहले से है)

  1. एंड्रॉइड में TextView का लेआउट वेट प्रोग्रामेटिकली कैसे सेट करें? एंड्रॉइड में TextView का लेआउट वेट प्रोग्रामेटिकली कैसे सेट करें?

    एंड्रॉइड में प्रोग्रामेटिकली Layout Weight सेट करना Layout Weight (android:layout_weight) केवल LinearLayout (या उसके सबक्लास) के चाइल्ड व्यूज पर काम करता है — RelativeLayout, ConstraintLayout, FrameLayout में इसका कोई प्रभाव नहीं पड़ता। मूल लेख का उदाहरण RelativeLayout में बटन का वेट सेट करने की कोश