Search code examples
androidandroid-tablayout

TabLayout tab can't click in Android


I can't click tab in TabLayout. I have two tabs(Genel and Ozel). When I open the app genel open I slither Ozel layout open in genel tab. I can't click in Ozel Tab. The codes are in below.

XML Codes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        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"
        tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
            android:id="@+id/fotogpsbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
    />

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

    >

        <android.support.design.widget.TabItem
                android:id="@+id/fotogpsgenel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Genel"
        />

        <android.support.design.widget.TabItem
            android:id="@+id/fotogpsozel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Özel"
        />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/ogesayfa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >

    </android.support.v4.view.ViewPager>

</RelativeLayout>

Java Codes (MainActivity):

Toolbar fototoolbar;
    TabLayout fototabl;
    TabItem genelit, ozelit;
    ViewPager fotogpspager;

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

        fototoolbar = findViewById(R.id.fotogpsbar);
        fototabl = findViewById(R.id.fotogpslayout);
        genelit = findViewById(R.id.fotogpsgenel);
        ozelit = findViewById(R.id.fotogpsozel);
        fotogpspager = findViewById(R.id.ogesayfa);

        sayfaadapter sayfaada = new sayfaadapter(getSupportFragmentManager(), fototabl.getTabCount());
        fotogpspager.setAdapter(sayfaada);

    }

Java codes (sayfaadapter - Adapter):

public class sayfaadapter extends FragmentPagerAdapter {

    private int tabsayi;

    sayfaadapter(FragmentManager fm, int tabsayi){
        super(fm);
        this.tabsayi = tabsayi;
    }

    @Override
    public Fragment getItem(int i) {

        switch(i){
            case 0:
                return new genel();
            case 1:
                return new ozel();
            default:
                return null;
        }

    }

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

How I can resolve this problem?

I need your help.

Not: I hope you can understand. I doesn't have a good English. You can ask where you doesn't understand.


Solution

  • you forgot to add layout_below into your views so viewpager overlay on tab because of this you cant click on tab, replace code as below in your xml.

    <RelativeLayout 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_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/fotogpsbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme" />
    
        <android.support.design.widget.TabLayout
            android:id="@+id/fotogpslayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fotogpsbar"
            app:tabGravity="fill"
            app:tabMode="fixed">
    
            <android.support.design.widget.TabItem
                android:id="@+id/fotogpsgenel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Genel" />
    
            <android.support.design.widget.TabItem
                android:id="@+id/fotogpsozel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Özel" />
    
        </android.support.design.widget.TabLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/ogesayfa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fotogpslayout" />
    </RelativeLayout>