Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabLayout text disappears when visibility goes from GONE to VISIBLE

I have a problem with the TabLayout in Android. I'm using AppCompat library because my min SDK is 10. The problem is that if the TabLayout has visibility GONE when the activity is first created, when I set the visibility to VISIBLE later, the tab tittles and tab indicator are missing.

Here is my MainActivity:

public class MainActivity extends AppCompatActivity {

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

    /**
     * Called when we press the button.
     */
    public void openTabActivity(View view) {
        Intent intent = new Intent(this, TabActivity.class);
        startActivity(intent);
    }
}

TabActivity is this:

public class TabActivity extends FragmentActivity {

    MyPagerAdapter mMyPagerAdapter;
    ViewPager mViewPager;
    TabLayout mTabLayout;

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

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mMyPagerAdapter =
                new MyPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.myViewPager);
        mViewPager.setAdapter(mMyPagerAdapter);

        // Link the TabLayout with the ViewPager.
        mTabLayout = (TabLayout) findViewById(R.id.myTab);
        mTabLayout.setupWithViewPager(mViewPager);

        // If I set visibility GONE it doesn't show titles 
        // when I set it to VISIBLE again.
        // If I remove this, it works fine.
        mTabLayout.setVisibility(View.GONE);

    }

    /**
     * If the tab is visible it turn it gone, if it's gone it turn it
     * visible.
     * @param view
     */
    public void toggleTab(View view) {
        Log.d(this.getClass().toString(), "ShowTab()");
        if (mTabLayout.getVisibility() == View.VISIBLE) {
            Log.d(this.getClass().toString(), "Turning GONE");
            mTabLayout.setVisibility(View.GONE);
        } else {
            Log.d(this.getClass().toString(), "Turning VISIBLE");
            mTabLayout.setVisibility(View.VISIBLE);
        }
    }    
}

Page adapter:

public class MyPagerAdapter extends FragmentStatePagerAdapter {

    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[]{"Tab 1", "Tab 2", "Tab 3"};

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new MyFragment();
        return fragment;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

Fragment:

public class MyFragment extends Fragment {

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);
        return view;
    }

}

Layouts are very simple too:

activity_main.xml

<RelativeLayout 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"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:onClick="openTabActivity"
        android:textColor="#55F"
        android:text="Press to go to Tabs"/>

</RelativeLayout>

activity_tab.xml

<LinearLayout 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"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              tools:context=".TabActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/myTab"
        style="@style/AppTheme.Tab.NavigationTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/lightPrimaryColor"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"/>

</LinearLayout>

my_fragment.xml

<FrameLayout 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"
             tools:context=".MyFragment">

    <Button
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:onClick="toggleTab"
        android:text="Press"/>

</FrameLayout>

If i set visibility GONE in TabActivity.onCreate it fails. If it's VISIBLE in TabActivity.onCreate it works.

I've tried to use .invalidate() but it doesn't work.

Can anybody help me?

Thanks in advance for your help.

like image 418
José Miguel Avatar asked Mar 15 '23 06:03

José Miguel


1 Answers

Confirmed. It's a bug in library com.android.support:design:22.2.1. If I use com.android.support:design:22.2.0 it works perfectly. It will be solved in future releases of the library.

Here is the issue in code.google.com

like image 103
José Miguel Avatar answered Apr 08 '23 10:04

José Miguel