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

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

Android Button में setCompoundDrawablesWithIntrinsicBounds() से आइकन सेट करना

XML के बजाय Java कोड से बटन के बाएं/दाएं/ऊपर/नीचे ड्रॉएबल (आइकन) प्रोग्रामेटिकली जोड़ने का तरीका।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project।

चरण 2: लेआउट में बटन (XML में drawableStart पहले से है)

res/layout/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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button/DrawableLeft"
        android:textSize="24sp"
        android:textStyle="bold"
        android:drawableStart="@drawable/ic_brush" />

</RelativeLayout>

चरण 3: ड्रॉएबल रिसोर्स जोड़ें

res/drawable/ic_brush.xml (वेक्टर) या कोई PNG इमेज res/drawable/ में रखें।

चरण 4: MainActivity.java में प्रोग्रामेटिकली सेट करें

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);

        // ड्रॉएबल प्राप्त करें
        Drawable img = getResources().getDrawable(R.drawable.ic_brush);

        // बाएं (start) पर सेट करें - left, top, right, bottom
        button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);

        // अन्य स्थितियों के लिए:
        // button.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null); // Top
        // button.setCompoundDrawablesWithIntrinsicBounds(null, null, img, null); // Right (end)
        // button.setCompoundDrawablesWithIntrinsicBounds(null, null, null, img); // Bottom
    }
}
महत्वपूर्ण: setCompoundDrawablesWithIntrinsicBounds() ड्रॉएबल के आंतरिक बाउंड्स का उपयोग करता है। यदि कस्टम साइज़ चाहिए तो setCompoundDrawables() उपयोग करें और मैन्युअली drawable.setBounds() कॉल करें।

चरण 5: AndroidManifest.xml

मानक मैनिफेस्ट।

ऐप चलाएं

बटन पर ब्रश आइकन बाईं ओर टेक्स्ट के साथ दिखाई देगा।

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

    परिचय यह उदाहरण दर्शाता है कि Android में ScrollView को प्रोग्रामेटिक रूप से कैसे डिसेबल किया जाए। हम OnTouchListener का उपयोग करके टच इवेंट को इंटरसेप्ट करेंगे। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project के माध्यम से नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML में ScrollView res/l

  1. Android ऐप को प्रोग्रामेटिकली कैसे बंद करें? Android ऐप को प्रोग्रामेटिकली कैसे बंद करें?

    यह उदाहरण दर्शाता है कि Android ऐप को बटन क्लिक पर प्रोग्रामेटिकली कैसे बंद किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=https://schemas

  1. Android पर VPN कैसे सेट अप करें Android पर VPN कैसे सेट अप करें

    क्या आप अपने मोबाइल डिवाइस पर ब्राउज़ करते समय अधिक सुरक्षित रहना चाहते हैं? या सिर्फ Netflix पर क्षेत्र प्रतिबंधों को बायपास करना चाहते हैं? एक VPN (वर्चुअल प्राइवेट नेटवर्क) एक बढ़िया विकल्प है। लेकिन क्या आप Android पर VPN सेट अप कर सकते हैं? यहां बताया गया है कि अपने Android डिवाइस पर मोबाइल VPN