Kunal Shah
Kunal Shah

Reputation: 1

Tab Not Changing on click of tab layout items

Fragments(tabs) are not changing on click of tab layout(tabs button)

I have tried the following code

MainActivity.java

package com.kunal.flightstatus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.net.Uri;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

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

        TabLayout tabLayout = findViewById(R.id.tablayout);
        tabLayout.addTab(tabLayout.newTab().setText("Home"));
        tabLayout.addTab(tabLayout.newTab().setText("Favourite"));
        tabLayout.addTab(tabLayout.newTab().setText("Search"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = findViewById(R.id.viewpager);
        PageAdapter adapter = new PageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

    }

}

PagerAdapter.java

package com.kunal.flightstatus;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;


public class PageAdapter extends FragmentPagerAdapter {


    public PageAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0 :
                Tab1 tab1 = new Tab1();
                position +=1;
                Bundle bundle = new Bundle();
                bundle.putString("message","fragment :"+position);
                tab1.setArguments(bundle);
                return tab1;
            case 1 :
                Tab2 tab2 = new Tab2();
                position +=1;
                Bundle bundl = new Bundle();
                bundl.putString("message","fragment :"+position);
                tab2.setArguments(bundl);
                return tab2;
            case 2 :
                Tab3 tab3 = new Tab3();
                position +=1;
                Bundle bundlee = new Bundle();
                bundlee.putString("message","fragment :"+position);
                tab3.setArguments(bundlee);
                return tab3;
            default:
                return null;
        }
    }

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewpager">

    </androidx.viewpager.widget.ViewPager>

</LinearLayout>

Tabs(fragments) Should change on click of tab layout item

Upvotes: 0

Views: 56

Answers (1)

Dmitry Safonov
Dmitry Safonov

Reputation: 11

ViewPager and TabLayout need to be linked for tab switching to work.

tabLayout.setupWithViewPager(viewPager);

should do the trick.

That way TabLayout will be connected to the ViewPager and will get filled automatically. Implement CharSequence getPageTitle (int position) in your FragmentPagerAdapter to fill in the titles instead of calling tabLayout.addTab() manually:

@NonNull
private List<String> titles = Arrays.asList("Home", "Favourite", "Search");

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

The whole setup will look like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabGravity="fill" />

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewpager" />

</LinearLayout>

activity:

package com.kunal.flightstatus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

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

        final ViewPager viewPager = findViewById(R.id.viewpager);
        PageAdapter adapter = new PageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = findViewById(R.id.tablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setupWithViewPager(viewPager);
    }

}

PagerAdapter:

package com.kunal.flightstatus;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.Arrays;
import java.util.List;


public class PageAdapter extends FragmentPagerAdapter {
    @NonNull
    private List<String> titles = Arrays.asList("Home", "Favourite", "Search");

    public PageAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

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

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0 :
                Tab1 tab1 = new Tab1();
                position +=1;
                Bundle bundle = new Bundle();
                bundle.putString("message","fragment :"+position);
                tab1.setArguments(bundle);
                return tab1;
            case 1 :
                Tab2 tab2 = new Tab2();
                position +=1;
                Bundle bundl = new Bundle();
                bundl.putString("message","fragment :"+position);
                tab2.setArguments(bundl);
                return tab2;
            case 2 :
                Tab3 tab3 = new Tab3();
                position +=1;
                Bundle bundlee = new Bundle();
                bundlee.putString("message","fragment :"+position);
                tab3.setArguments(bundlee);
                return tab3;
            default:
                throw new IllegalStateException();
        }
    }

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

Upvotes: 1

Related Questions