Android में मैं स्क्रीन के निचले भाग में एक टोस्ट संदेश प्रदर्शित करना चाहता हूं, मैंने यह कोशिश की:
Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG).show();
यह काम नहीं करता है, मैं इसे सही तरीके से कैसे करूं?
Android में मैं स्क्रीन के निचले भाग में एक टोस्ट संदेश प्रदर्शित करना चाहता हूं, मैंने यह कोशिश की:
Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG).show();
यह काम नहीं करता है, मैं इसे सही तरीके से कैसे करूं?
जवाबों:
स्क्रीन के केंद्र में टोस्ट प्रदर्शित करने के लिए।
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
अपने टोस्ट की स्थिति
क्षैतिज रूप से केंद्रित स्क्रीन के नीचे एक मानक टोस्ट अधिसूचना दिखाई देती है। आप इस स्थिति को setGravity(int, int, int)
विधि के साथ बदल सकते हैं । यह तीन मापदंडों को स्वीकार करता है: एक Gravity
स्थिर, एक x-position
ऑफसेट और एक y-position
ऑफसेट।
उदाहरण के लिए, यदि आप तय करते हैं कि टोस्ट ऊपरी-बाएँ कोने में दिखाई दे, तो आप इस तरह गुरुत्वाकर्षण सेट कर सकते हैं:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
यदि आप स्थिति को दाईं ओर करना चाहते हैं, तो दूसरे पैरामीटर का मान बढ़ाएं। इसे नीचे करने के लिए, अंतिम पैरामीटर का मान बढ़ाएँ।
कस्टम टोस्ट के लिए लेआउट फ़ाइल
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
बटन के क्लिक इवेंट पर कस्टम टोस्ट के लिए .Java फ़ाइल
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Button is clicked!");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}
कोल्टिन में केंद्र में (क्षैतिज रूप से) ग्रैविटी दिखाना / सेट करना
fun Context.longToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_LONG)
.apply {
view.findViewById<TextView>(android.R.id.message)?.gravity = Gravity.CENTER
}
.show()
}
टोस्ट संदेश प्रदर्शित करने के लिए निम्न कोड का उपयोग किया जा सकता है
Toast tt = Toast.makeText(MainActivity.this,"Your text displayed here", Toast.LENGTH_LONG);
tt.setGravity(Gravity.CENTER, 0, 0);
tt.show();