Simple Android toasts not aligning properly
Asked Answered
M

2

20

I'm simply calling from my Activity:

Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show()

But the result is a text aligned on the top of the toast container, not centered inside as it should:

enter image description here

Any ideas on what could be wrong?

Monjan answered 12/2, 2014 at 23:4 Comment(11)
Do you test it on phone? Which Android is that?Biegel
Nexus 4 running Stock Android 4.4Monjan
It seems to be some bug in your system. There is no way to change Toast's gravity. Can you post your Activity's code? Have you tested is on any other devices?Biegel
Yes, it works fine on other two devices I have here right now. But they are not running 4.4. I'm asking here because this is happening on an ordinary Nexus 4 phone without any changes made to it.Monjan
Post the code I will test it on nexus 5 for you:).Biegel
Have you tried to reboot your nexus?Biegel
Yes, this has been like this for a while now on this phone. I'm not sure what part of the code you want because the above is all there is to it related to the toast. Thanks for the offer though. :)Monjan
Have you tried updating your targetSdk to 19?Cashier
Nice idea, @AdamS but unfortunately my target is already 19. :(Monjan
In that case, does dropping it to 18 help?Cashier
Well, I can't do that because I'm using code from 19 on my project. But I created an empty project just with the toast and it works fine with target 18 and 19. So it's definitely something on my project. Could it be the activity's theme or actionbarsherlock?Monjan
M
35

I managed to fix it. The problem lies in applying the attribute android:fitsSystemWindows to the theme of an activity. I found this answer that explains why that should not be done:

The android:fitsSystemWindows attribute is intended for usage on views in layout xml, not in themes.

What you're seeing is the effect of the way the styled attribute system works in Android. If no attribute is specified on the view element or in the explicit style given to the view, the framework checks to see if that attribute has been specified on the theme itself. If it is found there, that value is used. Since the views used by toasts use your activity's theme, the default value of false is overridden and you see this behavior.

You're not just changing the fitsSystemWindows default for your top-level views by specifying it in the theme, you're overriding it for all views with that theme, which isn't what you want. You should only specify fitsSystemWindows on views within your layouts or in styles that you explicitly apply to views within your layouts, not on themes.

Just apply the attribute to the topmost ViewGroup of the activity (or style it) instead of its theme and the toast will be formatted correctly.

Monjan answered 13/2, 2014 at 12:32 Comment(1)
I used getApplicationContext() in Toast to solve the issueWoodrow
G
0

This situation occurs because in the horizontal screen state, the width of the navigation bar will also be calculated, regardless of whether the navigation bar is hidden or not, so you can subtract half the width of the navigation bar.

toast.setGravity(Gravity.CENTER, getNavigationBarHeight() / 2, 0); 

    private int getNavigationBarHeight() {
        Resources resources = this.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }
Garv answered 29/11, 2020 at 2:44 Comment(1)
Hi a442509097. Thanks for your answer. Usually answers with an explanation are more welcomed there. Would you like to add an explanation to your answer?Odysseus

© 2022 - 2024 — McMap. All rights reserved.