4

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.

Sławek Lis
  • 101
  • 1
  • 7
  • Check if this works. https://stackoverflow.com/questions/4337514/android-tabwidget-detect-click-on-current-tab – Ramakanth Putta Jul 04 '17 at 07:42
  • The solutions from there are little to complicated, is there a simple way to call one little function without several dozens of extra lines of code? – Sławek Lis Jul 04 '17 at 07:47
  • 1
    I think the downvoters should give a solution...for me, it´s a valid question that adheres to the SO Guidelines...no reason to downvote. – Opiatefuchs Jul 04 '17 at 07:49
  • @slawek: have you used exact the same `cityClick` in your textView? – Opiatefuchs Jul 04 '17 at 07:59
  • yes this is the same function and its work for textView – Sławek Lis Jul 04 '17 at 08:10
  • I am not sure, but try two things seperately: 1. remove `onClick`and `clickable` from `android.support.design.widget.TabLayout` . Mabye it gets the Focus and no other view can touched inside. and 2. Change `protected void cityClick` to `public void cityClick` as in the docs of `onClick` XML Attribute a `public` modifier is suggested. Just try it...... – Opiatefuchs Jul 04 '17 at 08:20

3 Answers3

7

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

            }
        });
Rashmi Bhandari
  • 448
  • 4
  • 8
3

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

    }
})
Luke Redmore
  • 449
  • 3
  • 10
3

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

            }
        });
agatha
  • 11
  • 6
elyar abad
  • 771
  • 1
  • 8
  • 27