कस्टम 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 दिशानिर्देशों का पालन करता है।