Search code examples
androidandroid-layoutandroid-fragmentsandroid-tablayout

Android - TabLayout titles not displaying


I'm stuck on a problem, was wondering anyone could help me out! I've been trying to achieve this layout over here:

enter image description here

My problem is now the TabLayout titles are not displaying. Here's my code for MainActivity.java

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener, OnMapReadyCallback{

private MapboxMap mapboxMap;
private MapView mapView;
private TabLayout tabLayout;
private ViewPager viewPager;

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

    tabLayout = (TabLayout)findViewById(R.id.tablayout_id);
    viewPager = (ViewPager)findViewById(R.id.viewpager_id);

    tabLayout.setupWithViewPager(viewPager);

    //Creates the fragment (tab)
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FragmentFlood(),"Flood");
    adapter.addFragment(new FragmentWeather(), "Weather");
    adapter.addFragment(new FragmentAnalytics(),"Analytics");

    //pass view to view pager
    viewPager.setAdapter(adapter);

}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter{

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();

public void addFragment(Fragment fragments, String titles){
    this.fragmentList.add(fragments);
    this.FragmentListTitles.add(titles);
    Log.d("fragments", "fragg");

}

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

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

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

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

activity_main.xml

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

<ScrollView
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_height="match_parent"
android:layout_width="match_parent">

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="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=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_id"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:layout_marginTop="300dp"
        android:background="@color/colorPrimary"
        android:elevation="2dp"
        app:layout_constraintBottom_toTopOf="@+id/viewpager_id"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimary"
        app:tabTextColor="@color/tabTextColor" />

    <android.support.v4.view.ViewPager
        android:layout_width="377dp"
        android:layout_height="163dp"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="350dp"
        tools:ignore="MissingConstraints"
        android:id="@+id/viewpager_id"/>


</android.support.constraint.ConstraintLayout>

</ScrollView>

FragmentFlood.java

public class FragmentFlood extends Fragment{
View view;


public FragmentFlood() {
}

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

There's no logcat error, everything seems perfectly fine. But when I run it, the TabLayout doesn't display anything at all? I also have MapBox in the view but I ommitted it in the code I posted here. Someone help me out please.


Solution

  • viewPager.setAdapter(adapter);    
    tabLayout.setupWithViewPager(viewPager);//<-This line should be after setAdapter to view pager