Search code examples
androidandroid-layoutandroid-fragmentsandroid-tablayoutandroid-appbarlayout

Tabs in TabLayout are not displaying


I need to display tabs in tab Layout which is in Container Activity but somehow tabs are not displaying. If it helps this project also has three activities which are Login Activity, Maps Activity, Register Activity.

Note: Map Fragment is different than Maps Activity.

Container.java

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(viewPager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
}

private  void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFragment(new MapFragment(), "Map");
    adapter.addFragment(new Chat(),"Chat");

    viewPager.setAdapter(adapter);

}

class ViewPagerAdapter extends FragmentPagerAdapter{

    private List<Fragment> mFragment = new ArrayList<>();
    private List<String> mFragmentTitle = new ArrayList<>();

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

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

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

    public void addFragment(Fragment fragment, String fragmentTitle){

        mFragment.add(fragment);
        mFragmentTitle.add(fragmentTitle);

    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitle.get(position);
    }
}

activity_container.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:background="@android:color/darker_gray"
        app:layout_scrollFlags="scroll|enterAlways"
        android:minHeight="22dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


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

</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="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

Chat Fragment Chat.java

public class Chat extends Fragment {

public Chat(){

}

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

}

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

}

activity_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downy.gpslocation.Chat">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hey there this is chat"
    android:layout_centerInParent="true"/>
</RelativeLayout>

Thanks for help in advance.


Solution

  • You should have:

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

    Because you are returning 0, saying that the adapter is empty.

    Also:

    @Override
    public Fragment getItem(int position) { 
        return mFragment.get(position); 
    }
    

    To return the correct fragment for the given position, not null.