Reputation: 111
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.
Upvotes: 5
Views: 6143
Reputation: 788
Since several tabs may exist, this would be more complete:
TabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
// codes related to the first tab
break;
case 1:
// codes related to the second tab
break;
case 2:
// codes related to the third tab
break;
case 3:
// codes related to the fourth tab
break;
.
.
.
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Upvotes: 3
Reputation: 449
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?) {
}
})
Upvotes: 3
Reputation: 458
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) {
}
});
Upvotes: 7