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.
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) {
}
});
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?) {
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With