Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 468
Sławek Lis Avatar asked Dec 19 '22 06:12

Sławek Lis


2 Answers

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) {

            }
        });
like image 94
Rashmi Bhandari Avatar answered Jan 13 '23 11:01

Rashmi Bhandari


In Kotlin,

tabs.addOnTabSelectedListener(object:TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab : TabLayout.Tab) {
        Toast.makeText(mActivity, tab.text, Toast.LENGTH_SHORT).show()
    }
    override fun onTabUnselected(p0: TabLayout.Tab?) {

    }
    override fun onTabReselected(p0: TabLayout.Tab?) {

    }
})
like image 37
Luke Redmore Avatar answered Jan 13 '23 12:01

Luke Redmore