Zeeshan Shabbir Zeeshan Shabbir - 10 months ago 37
Java Question

Titles in tablayout aren't visible

Working with

TabLayout
Problem is Titles aren't visible i overide
getPageTitle
in adapter also i returned a string which would be title but that string doesn't show up. I have debugged the app i am getting titles from server.

And yes i followed this but it wasn't working too.

Here is 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);
}


Adapter for the viewpager.

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;
}
}


Here is the 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 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?

Answer

I made following changes in tablayout xml then titles were visible

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