0

My app uses a tabLayout which is defined in the main_activity layout. I then have a separate layout file per tab to place all the views for that tab.

In the tab layout files, I’d like to constrain a view to the bottom edge of the tabs. I don’t know how to do this as the tabLayout is defined in another layout file, and it doesn’t have a fixed height (and I don't really want it to), so I can’t just use the same dimension in the tab layout file.

I’ve seen that there’s an tag that you can use to pull content in from other layouts, but this doesn’t feel right, I don’t need the whole layout to be duplicated within the tab, I essentially just need to know the height of it.

Here’s my main_activity layout which contains the tabLayout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:background="?attr/backgroundColour"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"     
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <android.support.design.widget.TabItem
            android:id="@+id/CostTab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cost" />

        <android.support.design.widget.TabItem
            android:id="@+id/costTab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distance" />

    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tabMenu"
        />

</android.support.constraint.ConstraintLayout>
Andrii Artamonov
  • 622
  • 8
  • 15
pledgeX
  • 443
  • 3
  • 11

1 Answers1

0

You should populate the adapter for ViewPager with Fragments for each tabLayout you have or use plain views as adapter items. You can find more info in developer guide here: https://developer.android.com/guide/navigation/navigation-swipe-view

  • I'm not sure I follow. I have fragments for each tab, and each fragment has a corresponding layout file. The tab functionality is working fine, it's just that in each of my fragment layout files I'm having to guess roughly where the tabs end and my 'usable space' begins. – pledgeX Jan 17 '20 at 12:16
  • 'usable space' begins at the top of fragment's layout file, so if you want to constrain a view to the bottom edge of the tabs, you just need to constraint it to the top of fragment's layout file. And if you need to know the height of layout in tab just use approach described in this answer: https://stackoverflow.com/questions/16937953/android-get-layout-height-and-width-in-a-fragment – Oleksii Yerastov Jan 17 '20 at 13:55