Can't move to another activity using tabItem

I have tab and want to move to different activites when i clicked on it. It was not working at all. Maybe, it has different configuration since its just a tabItem not a Button. But, i don't know about it.

Anybody could help me, please ?

1) activity_main.xml

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            app:tabMode="scrollable">

            <android.support.design.widget.TabItem
                android:id="@+id/hotel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/nav_hotel"
                android:text="Hotel" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/nav_flight"
                android:text="Pesawat" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/nav_train"
                android:text="Kereta" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/nav_event"
                android:text="Event" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/nav_car"
                android:text="Mobil" />
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

2) MainActivity.java

TabItem tabItem = (TabItem) findViewById(R.id.hotel);
tabItem.setOnClickListener(new View.OnClickListener() {         
        public void onClick(View v) {               
            Intent fp = new Intent(MainActivity.this, AnotherActivity.class);              
            startActivity(fp);              
        }
});

Try this

tabItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
               Intent fp = new Intent(MainActivity.this, AnotherActivity.class);              
               startActivity(fp); 
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image