0

I'm new to Android, I have the following tab and I need to establish a divider between tabs enter image description here

I would like to get something like this

enter image description here

I was already reading this, but the solution comes to TabLayout and I am using FragmentTabHost, and it did not work

This is the solution that I try to implement

In my activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_flight);

        tabHost= (FragmentTabHost) findViewById(R.id.search_flight_tab_host);
        tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);

        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ROUND_TRIP).setIndicator(getString(R.string.round_trip)),
                SearchFlightRoundTripFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ONE_WAY).setIndicator(getString(R.string.one_way)),
                SearchFlightOneWayFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_MULTIPLE).setIndicator(getString(R.string.multiple)),
                SearchFlightMultipleFragment.class, null);

        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            View tabView = tabHost.getChildAt(i);
            if (tabView instanceof LinearLayout) {
                ((LinearLayout) tabView).setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                Drawable drawable = getDrawable(R.drawable.tab_divider);
                ((LinearLayout) tabView).setDividerDrawable(drawable);
            }
        }
    }

My drawable tab_divider

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size android:width="1dp" />
            <solid android:color="@color/colorBlack" />
        </shape>
    </item>

    <item
        android:bottom="16dp"
        android:top="12dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/colorBlack" />
            <size android:width="1dp" />
        </shape>
    </item>
</layer-list>

my activity.xml

<?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"
    tools:context=".SearchFlightActivity" >

    <FrameLayout
        android:gravity="fill_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <ProgressBar
            android:id="@+id/top_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:indeterminateTint="@color/colorAccent"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone" />

    </FrameLayout>

    <ScrollView
        android:layout_marginTop="11dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <android.support.v4.app.FragmentTabHost
                android:id="@+id/search_flight_tab_host"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginRight="6dp"
                    android:orientation="vertical">

                    <TabWidget
                        android:id="@android:id/tabs"
                        style="@style/lightGrayLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:background="@drawable/tab_border_round"
                        android:orientation="horizontal" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1" />
                </LinearLayout>
            </android.support.v4.app.FragmentTabHost>

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

I was also watching this and looked at a couple of places but please thank them if you could help me

  • Set your drawable as the `android:divider` attribute on the ``. – Mike M. Nov 29 '19 at 07:07
  • 1
    Add `android:divider="@drawable/tab_divider"` to the `` tag in your layout. – Mike M. Nov 29 '19 at 08:26
  • I did not solve the problem, still showing nothing. – Juan Pablo Moreno Martín Nov 29 '19 at 08:58
  • 1
    Well, this is kinda silly, but it looks like you have to add `android:showDividers="middle"`, too, even though it's not mentioned in the docs. At least, that's what I had to do to get it working. – Mike M. Nov 29 '19 at 09:34
  • Perfect, thank you very much, you can not imagine how many places I have looked for this solution, until finally you help me – Juan Pablo Moreno Martín Nov 29 '19 at 09:38
  • Yeah, that's bad documentation. `TabWidget` is a subclass of `LinearLayout`, and it just passes all of the divider stuff to `LinearLayout`. The `TabWidget` docs specifically mention only the `divider` attribute, so you'd think that `TabWidget` is handling the appropriate `showDividers` setting itself, but apparently not. Anyhoo, no problem. Glad you got it working. Cheers! – Mike M. Nov 29 '19 at 09:43

1 Answers1

0

Thanks to the comments of @MikeM I was able to solve the problem, the solution is the next:

In the activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_flight);

        tabHost= (FragmentTabHost) findViewById(R.id.search_flight_tab_host);
        tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);

        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ROUND_TRIP).setIndicator(getString(R.string.round_trip)),
                SearchFlightRoundTripFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ONE_WAY).setIndicator(getString(R.string.one_way)),
                SearchFlightOneWayFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_MULTIPLE).setIndicator(getString(R.string.multiple)),
                SearchFlightMultipleFragment.class, null);
    }

In my activity xml

<?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"
    tools:context=".SearchFlightActivity" >

    <FrameLayout
        android:gravity="fill_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <ProgressBar
            android:id="@+id/top_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:indeterminateTint="@color/colorAccent"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone" />

    </FrameLayout>

    <ScrollView
        android:id="@+id/mainLayout"
        android:layout_marginTop="11dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <android.support.v4.app.FragmentTabHost
                android:id="@+id/search_flight_tab_host"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginRight="6dp"
                    android:orientation="vertical">

                    <TabWidget
                        android:id="@android:id/tabs"
                        style="@style/lightGrayLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:background="@drawable/tab_border_round"
                        android:orientation="horizontal"
                        android:divider="@color/colorBlack"
                        android:showDividers="middle"/>

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1" />
                </LinearLayout>
            </android.support.v4.app.FragmentTabHost>

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

It should be noted that the property android:divider="@color/colorBlack" It is not obligatory to achieve what you want, with or without it, the dividing lines appear, but with this, it is possible to darken them a little more, I hope this helps someone else.