Vishal A.
Vishal A.

Reputation: 1381

TextView inside fragment with layout_gravity set to center hides behind tabs in tabbed activity

I am trying to implement a scrolling fragment in the Tabbed activity. There's only a single TextView inside the fragment and it works fine as far as layout_gravity is not changed. But as soon as layout_gravity is set to center the content of the TextView hides behind the tabs.

Here's the code:

Fragment

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragment_1">

    <TextView
        android:id="@+id/frag1_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/text_margin"
        android:textSize="@dimen/text_medium"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:text="@string/buddha_vandana_mar" />

</androidx.core.widget.NestedScrollView>

And ViewPager:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".PrayerActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true"
        android:theme="@style/Theme.BuddhaPoojapathMarathi.AppBarOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Without layout_gravity:center looks like following:

enter image description here

With layout_gravity:center set looks like following: enter image description here

Upvotes: 0

Views: 197

Answers (1)

androidLearner
androidLearner

Reputation: 1702

Add android:fitsSystemWindows="true" in your root layout.

  <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragment_1"
    android:fitsSystemWindows="true"
>

Upvotes: 0

Related Questions