Headings in Java – tablayout are not visible

Headings in Java – tablayout are not visible … here is a solution to the problem.

Headings in Java – tablayout are not visible

The problem with TabLayout is that the title is not visible I overwrote getPageTitle in the adapter I also returned a string that would be the title, but that string was not displayed. I’ve debugged the app to get the title from the server.

Yes, I followed but it doesn’t work either.

Here are the relevant Java code and XML files.

 private void loadCategories() {
        StringRequest request = new StringRequest(Constants.URL_GET_CATEGORIES, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                categoriesDTO = Constants.gson.fromJson(response,CategoriesDTO.class);
                ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(adapter);
                setTabLayout();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

}
        });

ApplicationController.getmInstance().addToRequestQueue(request);
    }

private void setTabLayout(){
        tabLayout.setupWithViewPager(viewPager);
    }

ViewPager’s adapter.

private class ViewPagerAdapter extends FragmentStatePagerAdapter{

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

@Override
    public Fragment getItem(int position) {
        return new ContentFragment();
    }

@Override
    public int getCount() {
        return categoriesDTO.getCategories().size();
    }

@Override
    public CharSequence getPageTitle(int position) {
        String title =categoriesDTO.getCategories().get(position).getTitle();
        return title;
    }
}

This is XML.

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill">

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

<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/home_drawer_menu">

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

This is the output

enter image description here

Here is the gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.softoven.ultron"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'

compile 'org.jsoup:jsoup:1.6.1'
compile 'com.mikhaellopez:circularimageview:3.0.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.5'

How to make it work?

Solution

In my case, I solved the problem by substitution

tabLayout.setupWithViewPager(viewPager);

Pass,

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
                        tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

@Override
            public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
            public void onTabReselected(TabLayout.Tab tab) {

}
});

Hope this helps.

Related Problems and Solutions