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

Android में कस्टम Toast संदेश बनाना

कस्टम Toast क्या है?

डिफ़ॉल्ट Toast एक साधारण टेक्स्ट संदेश दिखाता है। कस्टम Toast आपको कस्टम लेआउट (इमेज, कलर, फॉन्ट, एनिमेशन) के साथ समृद्ध सूचनाएं बनाने की अनुमति देता है।

चरण-दर-चरण कार्यान्वयन

चरण 1: कस्टम टोस्ट लेआउट बनाएं (res/layout/custom_toast.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_background"
    android:orientation="horizontal"
    android:padding="12dp"
    android:gravity="center">

    <ImageView
        android:id="@+id/toast_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginEnd="12dp"
        android:src="@drawable/ic_success"
        android:contentDescription="@string/toast_icon_desc" />

    <TextView
        android:id="@+id/toast_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="कस्टम टोस्ट संदेश"
        android:textColor="#FFFFFF"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

चरण 2: बैकग्राउंड ड्रॉएबल (res/drawable/toast_background.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="12dp" />
    <gradient
        android:startColor="#2E7D32"
        android:endColor="#1B5E20"
        android:angle="45" />
    <padding
        android:left="16dp"
        android:top="12dp"
        android:right="16dp"
        android:bottom="12dp" />
    <stroke
        android:width="1dp"
        android:color="#4CAF50" />
</shape>

चरण 3: MainActivity.java में कस्टम टोस्ट दिखाएं

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        Button showToastBtn = findViewById(R.id.showToastBtn);
        showToastBtn.setOnClickListener(v -> showCustomToast("ऑपरेशन सफल!", R.drawable.ic_success, "#2E7D32"));

        Button showErrorToastBtn = findViewById(R.id.showErrorToastBtn);
        showErrorToastBtn.setOnClickListener(v -> showCustomToast("कुछ गलत हुआ!", R.drawable.ic_error, "#C62828"));
    }

    private void showCustomToast(String message, int iconRes, String bgColor) {
        // लेआउट इन्फ्लेट करें
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast, 
                                       (ViewGroup) findViewById(R.id.custom_toast_container));

        // व्यूज़ रेफरेंस प्राप्त करें
        ImageView icon = layout.findViewById(R.id.toast_icon);
        TextView text = layout.findViewById(R.id.toast_message);

        // कंटेंट सेट करें
        icon.setImageResource(iconRes);
        text.setText(message);

        // बैकग्राउंड रंग डायनामिकली बदलें (वैकल्पिक)
        // GradientDrawable drawable = (GradientDrawable) layout.getBackground();
        // drawable.setColor(Color.parseColor(bgColor));

        // Toast बनाएं और दिखाएं
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 100); // नीचे से 100dp ऑफसेट
        toast.setView(layout);
        toast.show();
    }
}

पुन: प्रयोज्य Toast हेल्पर क्लास

public class CustomToast {
    public static void showSuccess(Context context, String message) {
        show(context, message, R.drawable.ic_success, "#2E7D32");
    }

    public static void showError(Context context, String message) {
        show(context, message, R.drawable.ic_error, "#C62828");
    }

    public static void showWarning(Context context, String message) {
        show(context, message, R.drawable.ic_warning, "#F57F17");
    }

    public static void showInfo(Context context, String message) {
        show(context, message, R.drawable.ic_info, "#1565C0");
    }

    private static void show(Context context, String message, int iconRes, String bgColor) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.custom_toast, null);
        
        ImageView icon = layout.findViewById(R.id.toast_icon);
        TextView text = layout.findViewById(R.id.toast_message);
        
        icon.setImageResource(iconRes);
        text.setText(message);
        
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 100);
        toast.setView(layout);
        toast.show();
    }
}

// उपयोग: CustomToast.showSuccess(this, "डेटा सेव हो गया!");

महत्वपूर्ण विचार

  • लाइफसाइकिल: टोस्ट एप्लिकेशन कॉन्टेक्स्ट के साथ काम करता है, एक्टिविटी कॉन्टेक्स्ट के साथ नहीं (लीक से बचने के लिए)
  • थ्रेड सुरक्षा: हमेशा मेन थ्रेड पर show() कॉल करें
  • कतार: एकाधिक टोस्ट कतार में लगते हैं; लंबे संदेशों के लिए LENGTH_LONG उपयोग करें
  • Android 12+: कस्टम टोस्ट व्यूज़ कुछ प्रतिबंधों के अधीन हो सकते हैं

आधुनिक विकल्प: Snackbar

अधिक इंटरैक्टिव फीडबैक (एक्शन बटन के साथ) के लिए, Snackbar का उपयोग करने पर विचार करें जो Material Design दिशानिर्देशों का पालन करता है।

  1. एंड्रॉइड पर टेक्स्ट कैसे फॉरवर्ड करें एंड्रॉइड पर टेक्स्ट कैसे फॉरवर्ड करें

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

  1. एंड्रॉयड पर कस्टम जेस्चर कैसे जोड़ें एंड्रॉयड पर कस्टम जेस्चर कैसे जोड़ें

    अपने एंड्रॉयड डिवाइस पर कस्टम जेस्चर बनाना आपके लिए समय बचाने का एक शानदार तरीका है। इन कस्टम जेस्चर को बनाकर, आप अपने डिवाइस को उन मूवमेंट का उपयोग करने के लिए कस्टमाइज़ कर सकते हैं जिनके साथ आप सबसे अधिक सहज हैं, किसी विशेष कार्य को निष्पादित करने के लिए जैसे नया टैब खोलना। क्या यह बहुत अच्छा नही

  1. एंड्रॉइड पर कस्टम टेक्स्ट मैसेज रिंगटोन सेट करें: चरण-दर-चरण गाइड एंड्रॉइड पर कस्टम टेक्स्ट मैसेज रिंगटोन सेट करें: चरण-दर-चरण गाइड

    टेक्स्ट मैसेज के लिए एक कस्टम नोटिफिकेशन टोन या किसी विशिष्ट संपर्क के लिए एक कस्टम रिंगटोन एक सरल लेकिन बहुत उपयोगी सेटिंग है। यह आपको संदेशों या कॉल्स को प्राथमिकता देने और यह तय करने की अनुमति देता है कि किन पर तत्काल ध्यान देने की आवश्यकता है और किन्हें इंतजार करना पड़ सकता है। उदाहरण के लिए, आप