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

एंड्रॉइड में Snackbar का लेआउट कस्टमाइज़ कैसे करें

Snackbar को पूरी तरह से कस्टमाइज़ करना - रंग, फॉन्ट, एक्शन बटन

डिफ़ॉल्ट Snackbar कई बार ऐप की थीम से मेल नहीं खाता। यह उदाहरण दिखाता है कि Snackbar के टेक्स्ट रंग, बैकग्राउंड रंग, फॉन्ट और एक्शन बटन को कैसे कस्टमाइज़ किया जाए।

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

Android Studio में प्रोजेक्ट बनाएं और build.gradle में मटेरियल लाइब्रेरी सुनिश्चित करें:

dependencies {
    implementation 'com.google.android.material:material:1.9.0'
}

चरण 2: मुख्य लेआउट

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <Button
        android:id="@+id/btnCustomSnackbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:backgroundTint="?attr/colorPrimary"
        android:text="कस्टम Snackbar दिखाएं"
        android:textColor="@android:color/white" />

</LinearLayout>

चरण 3: MainActivity में कस्टमाइज़ेशन लॉजिक

package com.example.customsnackbar;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

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

        Button btnCustomSnackbar = findViewById(R.id.btnCustomSnackbar);
        btnCustomSnackbar.setOnClickListener(v -> showCustomSnackbar(v));
    }

    private void showCustomSnackbar(View view) {
        // Snackbar बनाएं
        Snackbar snackbar = Snackbar.make(view, "यह एक कस्टम Snackbar है!", Snackbar.LENGTH_LONG)
                .setAction("UNDO", v -> {
                    Snackbar.make(view, "एक्शन पर क्लिक किया गया!", Snackbar.LENGTH_SHORT).show();
                });

        // Snackbar व्यू प्राप्त करें और कस्टमाइज़ करें
        View snackbarView = snackbar.getView();

        // बैकग्राउंड रंग बदलें
        snackbarView.setBackgroundColor(Color.WHITE);

        // टेक्स्ट व्यू और एक्शन व्यू प्राप्त करें
        TextView textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
        TextView actionView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_action);

        // कस्टम फॉन्ट लागू करें (assets/fonts/ में फॉन्ट फ़ाइल रखें)
        try {
            Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/LatoRegular.ttf");
            if (textView != null) textView.setTypeface(customFont);
            if (actionView != null) actionView.setTypeface(customFont);
        } catch (Exception e) {
            // फॉन्ट नहीं मिला तो डिफ़ॉल्ट उपयोग करें
        }

        // टेक्स्ट रंग सेट करें
        if (textView != null) textView.setTextColor(Color.BLACK);
        if (actionView != null) actionView.setTextColor(Color.parseColor("#6200EE")); // प्राइमरी रंग

        // एक्शन टेक्स्ट को ऑल कैप्स से रोकें
        if (actionView != null) actionView.setAllCaps(false);

        snackbar.show();
    }
}

एडवांस्ड कस्टमाइज़ेशन: पूरी तरह से कस्टम लेआउट

अधिक नियंत्रण के लिए कस्टम लेआउट इन्फ्लेट करें:

private void showFullyCustomSnackbar(View view) {
    // कस्टम लेआउट इन्फ्लेट करें
    View customView = getLayoutInflater().inflate(R.layout.custom_snackbar_layout, null);

    TextView messageText = customView.findViewById(R.id.custom_message);
    TextView actionText = customView.findViewById(R.id.custom_action);
    ImageView iconView = customView.findViewById(R.id.custom_icon);

    messageText.setText("कस्टम लेआउट के साथ Snackbar");
    actionText.setText("RETRY");
    iconView.setImageResource(R.drawable.ic_warning);

    Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
    Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbar.getView();

    // डिफ़ॉल्ट पैडिंग हटाएं
    snackbarLayout.setPadding(0, 0, 0, 0);

    // कस्टम व्यू जोड़ें
    snackbarLayout.addView(customView, 0);

    snackbar.show();
}

res/layout/custom_snackbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp"
    android:background="#1E1E1E"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/custom_icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="12dp"
        android:src="@drawable/ic_info"
        android:contentDescription="आइकन" />

    <TextView
        android:id="@+id/custom_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/custom_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:textColor="#FFD600"
        android:textSize="14sp"
        android:textStyle="bold" />

</LinearLayout>

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.customsnackbar">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.MaterialComponents.DayNight">

        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

मुख्य कस्टमाइज़ेशन बिंदु

कस्टमाइज़ेशनविधि
बैकग्राउंड रंगsnackbarView.setBackgroundColor()
मैसेज टेक्स्ट रंगtextView.setTextColor()
एक्शन टेक्स्ट रंगactionView.setTextColor()
कस्टम फॉन्टTypeface.createFromAsset()
पूरा कस्टम लेआउटsnackbarLayout.addView()
नोट: Snackbar कंटेनर व्यू (CoordinatorLayout या ConstraintLayout) के सापेक्ष प्रदर्शित होता है। सुनिश्चित करें कि पैरेंट लेआउट Snackbar के लिए उपयुक्त हो।
  1. एंड्रॉइड नोटिफिकेशन साउंड कैसे चलाएं? एंड्रॉइड नोटिफिकेशन साउंड कैसे चलाएं?

    डिफ़ॉल्ट नोटिफिकेशन साउंड बजाना RingtoneManager और MediaPlayer का उपयोग करके सिस्टम के डिफ़ॉल्ट नोटिफिकेशन/रिंगटोन/अलार्म साउंड को प्रोग्रामेटिकली बजाएं। चरण 1-2: प्रोजेक्ट और लेआउट (मानक बटन लेआउट) चरण 3: MainActivity.java { // डिफ़ॉल्ट नोटिफिकेशन साउंड URI प्राप्त करें U

  1. Android में Snackbar का उपयोग कैसे करें? Android में Snackbar का उपयोग कैसे करें?

    परिचय Snackbar Material Design का एक घटक है जो स्क्रीन के निचले भाग में एक संक्षिप्त संदेश दिखाता है। यह उदाहरण Snackbar के बुनियादी उपयोग और अनुकूलन को दर्शाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.

  1. एंड्रॉइड में लाइन कैसे ड्रा करें? (Canvas/Paint) एंड्रॉइड में लाइन कैसे ड्रा करें? (Canvas/Paint)

    एंड्रॉइड में Canvas और Paint से लाइन (और शेप्स) ड्रा करना एंड्रॉइड में कस्टम ड्राइंग के लिए Canvas (ड्राइंग सतह) और Paint (स्टाइल: रंग, चौड़ाई, स्ट्रोक/फिल) उपयोग करते हैं। दो मुख्य दृष्टिकोण हैं: कस्टम View (onDraw() ओवरराइड) — **सिफारिश किया गया**, पुन: उपयोग योग्य, प्रदर्शन अच्छा। Bitmap पर ड्रा