I don't know how it's called, but it's a small, gray, transparent box which shows information. I want to create it for my app, and write different information in it.
2 Answers
Toast.makeText(this, "your message", Toast.LENGTH_SHORT).show();
Here this
is context like in activity you can pass YourActivity.this
or in Fragment class you can pass getActivity()
.
Toast.LENGTH_SHORT
or Toast.LENGTH_LONG
are two duration which you can use to show Toast.
Update :
Positioning your Toast
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.
Create custom Toast with your color and layout like
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_custom_layout, null);
toast.setView(view);
toast.show();
One textview you can put inside the layout file and give the background and textcolor as you want.
Also you can do the following which won't need the extra custom layout file :
Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_backgrround);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
toast.show();
If you don't want do all this stuff here is library for fancy toast. Which allow you custom toast with many inbuilt themes.

- 57,232
- 27
- 203
- 212
-
Is there an easy way to edit it's position and background color because now it's overlapping with a button I have at the bottom and is with a white background? – Kazhiunea Apr 29 '18 at 17:22
-
Updated answer @Kazhiunea – Khemraj Sharma Apr 29 '18 at 17:29