Search code examples
androidonclicktabitem

android TabItem onClick doesn't work


I have function cityClick, if I call this function from a textView its working ok, but if I call cityClick from TabItem it doesn't work, what is going on?

Java

public class Kategorie extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

protected void cityClick(View view) {
    Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
}

Layout

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:clickable="true"
        android:onClick="cityClick"
        app:tabMode="fixed">

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDAŃSK" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="SOPOT" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDYNIA" />

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

I was searching on this forum, but no found anything to help me.


Solution

  • Try this,

      tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    Toast.makeText(mActivity, "hai", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });