How to make a custom toast fill width?
How can you make a custom toast notification fill the width of the screen?
I 开发者_JAVA技巧have tried multiple things and can't get it to work.
Thanks.
This should work:
Toast t = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT);
t.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
You can create toast with custom layout and fill_horizontal. Below code is written in Adapter class and it is working fine.
LayoutInflater inflater =(LayoutInflater)mContext
.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
View layout =inflater.inflate(R.layout.custom_toast_layout,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(mContext);
//use both property in single function
toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
You can find here http://android-apps-blog.blogspot.com/2011/04/how-to-display-custom-toast-in-android.html a good tutorian on How to create a Custom Toast Notification.
Have a look at this example for Custom Toast with Border.
I'd say you should make a Dialog or an activity (with the dialog theme or something you've made that looks like the dialog theme), and show that.
Use a timer in the oncreate that calls finish()
after your time is up.
Try something like this...
wM = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
LayoutInflater inflate = (LayoutInflater) context
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = inflate.inflate(R.layout.custom_toast, null);
// Set the layout to be like a toast window!
mParams.gravity = Gravity.WHEREVER YOU WANT IT TO BE
mParams.height = 200;
mParams.width = WindowManager.LayoutParams.FILL_PARENT;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mParams.format = PixelFormat.OPAQUE;
mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(v);
<Timer or whatever>
wm.removeView(v);
精彩评论