Alexander Zhak Alexander Zhak - 5 months ago 62
Android Question

How to show custom toast below ActionBar

How can I show a custom Toast just below ActionBar (compatible with API7)?

A method which creates the Toast is as follows:

public void showToast(Activity context, String text) {
LayoutInflater inflater = context.getLayoutInflater();
View layout = inflater.inflate(R.layout.popup, null);
TextView tv = (TextView)layout.findViewById(;
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, Y);

Actually, in order to be able to call this method from any activity, I put it in my Application class. As I understand, I need to put Y (top offset) to toast.SetGravity(). But I have no idea how to get the correct top coordinate of activity's layout, which is "ActionBar bottom". Any help would be highly appreciated!


So Y would be the height of the ActionBar

Assuming you're using the appcompat library and you've not changed the ActionBar height this should work:

int Y = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height);