Show toast widget underneath a view
From what I learned, this is the best I came up with for the program. Doing this puts the Toast message in the center of the phone.
if(input > 100) {
Toast toast = Toast.makeText(MainActivity.this,
R.string.over_guess,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
else if(input < comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_low, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(input > comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_high, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.correct, comp, guesses),
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
How to display a Toast message in the top of the page in my android application
You need add this as programmatically like this in your code:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
How to display Toast at center of screen
To display the Toast in center of the screen.
Toast toast = Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
How to change position of Toast in Android?
From the documentation,
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, anx-position
offset, and ay-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.
how to present a toast when a widget is clicked?
Just call Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
in widget's onClick()
method of View.OnClickListener
Update:
If you use AppWidgetProvider
so check this and this one posts
How to Display a Toast Widget in A Fragment?
You need a context to show toast, here is the code :
Toast.makeText(this@HomeFragment.requireActivity(), "saving", Toast.LENGTH_SHORT).show()
Thanks
Android - Create a Toast anchored to a View
I think this Tutorial will help you to achieve what you want:
public void onClick(View v) {
int xOffset = 0;
int yOffset = 0;
Rect gvr = new Rect();
View parent = (View) v.getParent();// v is the image,
//parent is the rectangle holding it.
if (parent.getGlobalVisibleRect(gvr)) {
Log.v("image left", Integer.toString(gvr.left));
Log.v("image right", Integer.toString(gvr.right));
Log.v("image top", Integer.toString(gvr.top));
Log.v("image bottom", Integer.toString(gvr.bottom));
View root = v.getRootView();
int halfwayWidth = root.getRight() / 2;
int halfwayHeight = root.getBottom() / 2;
//get the horizontal center
int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;
//get the vertical center
int parentCenterY = (gvr.bottom - gvr.top) / 2 + gvr.top;
if (parentCenterY <= halfwayHeight) {
yOffset = -(halfwayHeight - parentCenterY);//this image is above the center of gravity, i.e. the halfwayHeight
} else {
yOffset = parentCenterY - halfwayHeight;
}
if (parentCenterX < halfwayWidth) { //this view is left of center xOffset = -(halfwayWidth - parentCenterX); } if (parentCenterX >= halfwayWidth) {
//this view is right of center
xOffset = parentCenterX - halfwayWidth;
}
}
Toast toast = Toast.makeText(activity, altText, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, xOffset, yOffset);
toast.show();
}
});
Related Topics
Can't Find Toolbar in an Included Layout
Android Xml Layout Parameters Do Not Function as Expected
Understanding Canvas and Surface Concepts
Change Package Name for Android in React Native
Set Live Wallpaper Programmatically on Rooted Device Android
Android Debug Monitor Hierarchy View Not Showing
Android: How to Programmatically Set the Size of a Layout
Trying to Add Adb to Path Variable Osx
Missing Contentdescription Attribute on Image' in Xml
Runonuithread VS Looper.Getmainlooper().Post in Android
How to Add -Xlint:Unchecked to My Android Gradle Based Project
Glide Image Loading with Application Context
Put Buttons at Bottom of Screen with Linearlayout
Android - Multi-Line Linear Layout