0

Below is my XML code. I want to display toast just above linearlayout (id: inside_linear) by leaving some margin. I know that we can use setGravity() method of toast. But how can I calculate yoffset?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/relative">

    <LinearLayout
        android:id="@+id/outside_linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:weightSum="10">

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8"/>

        <LinearLayout
            android:id="@+id/inside_linear"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            .....

       </LinearLayout>

</RelativeLayout>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 4
    Does this answer your question? [How to change position of Toast in Android?](https://stackoverflow.com/questions/2506876/how-to-change-position-of-toast-in-android) – Mohammed Abid Nafi Aug 14 '21 at 17:36

1 Answers1

0

Have you checked this answer? https://stackoverflow.com/a/2507069

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

With the last parameter you change Y axis position.

Also try setMargin(), it works with percentages

toast.setMargin(horizontalMargin, verticalMargin);
MTM
  • 15
  • 6