Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCaleb Seeling
1,189 PointsTrying to use Tabitems to navigate! Isnt working.
I am making an App where you need to swipe with tabs. I am trying to do this but if I press on any tab in the emulator it doesnt go to the selected page and crashes if I press it to often.
HomePage
package com.example.calebseeling.myapplication;
import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabItem; import android.support.design.widget.TabLayout; import android.support.v7.app.AppCompatActivity;
/**
- Created by calebseeling on 27.12.17. */
public class HomePage extends AppCompatActivity { private TabItem main_tab_item_1; private TabItem main_tab_item_2; private TabItem main_tab_item_3; private TabLayout tablayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_home);
final TabItem main_tab_item_1 = (TabItem) findViewById(R.id.main_tab_item_1);
TabItem main_tab_item_2 = (TabItem) findViewById(R.id.main_tab_item_2);
TabItem main_tab_item_3 = (TabItem) findViewById(R.id.main_tab_item_3);
final TabLayout tablayout = (TabLayout) findViewById(R.id.tabLayout);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab main_tab_item_1) {
getNextHomePage();
}
@Override
public void onTabUnselected(TabLayout.Tab main__tab_item_2) {
getNextModePage();
}
@Override
public void onTabReselected(TabLayout.Tab main_tab_item_3) {
main_tab_item_1.setSelected(true);
getNextPlayPage();
}
});
}
boolean settrue = true;
private void getNextHomePage() {
Intent intent = new Intent(this, HomePage.class);
startActivity(intent);
main_tab_item_1.setSelected(settrue);
}
private void getNextModePage() {
Intent nextModePage = new Intent(this, ModePage.class );
startActivity(nextModePage);
main_tab_item_2.setSelected(settrue);
}
private void getNextPlayPage() {
Intent nextPlayPage = new Intent(this, PlayPage.class);
startActivity(nextPlayPage);
main_tab_item_3.setSelected(settrue);
}
}
ModePage
package com.example.calebseeling.myapplication;
import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabItem; import android.support.design.widget.TabLayout; import android.support.v7.app.AppCompatActivity;
/**
- Created by calebseeling on 31.12.17. */
public class ModePage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_mode);
TabItem main_tab_item_1 = (TabItem) findViewById(R.id.main_tab_item_1);
TabItem main_tab_item_2 = (TabItem) findViewById(R.id.main_tab_item_2);
TabItem main_tab_item_3 = (TabItem) findViewById(R.id.main_tab_item_3);
TabLayout tablayout = (TabLayout) findViewById(R.id.tabLayout);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabUnselected(TabLayout.Tab main_tab_item_1) {
getNextHomePage();
}
@Override
public void onTabSelected(TabLayout.Tab main__tab_item_2) {
getNextModePage();
}
@Override
public void onTabReselected (TabLayout.Tab main_tab_item_3) {
getNextPlayPage();
}
});
}
private void getNextHomePage() {
Intent intent = new Intent(this, HomePage.class);
startActivity(intent);
}
private void getNextModePage() {
Intent nextModePage = new Intent(this, ModePage.class );
startActivity(nextModePage);
}
private void getNextPlayPage() {
Intent nextPlayPage = new Intent(this, PlayPage.class);
startActivity(nextPlayPage);
}
}
PlayPage
package com.example.calebseeling.myapplication;
import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabItem; import android.support.design.widget.TabLayout; import android.support.v7.app.AppCompatActivity;
/**
- Created by calebseeling on 30.12.17. */
public class PlayPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_play);
TabItem main_tab_item_1 = (TabItem) findViewById(R.id.main_tab_item_1);
TabItem main_tab_item_2 = (TabItem) findViewById(R.id.main_tab_item_2);
TabItem main_tab_item_3 = (TabItem) findViewById(R.id.main_tab_item_3);
TabLayout tablayout = (TabLayout) findViewById(R.id.tabLayout);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabReselected(TabLayout.Tab main_tab_item_1) {
getNextHomePage();
}
@Override
public void onTabUnselected(TabLayout.Tab main__tab_item_2) {
getNextModePage();
}
@Override
public void onTabSelected(TabLayout.Tab main_tab_item_3) {
getNextPlayPage();
}
});
}
private void getNextHomePage() {
Intent intent = new Intent(this, HomePage.class);
startActivity(intent);
}
private void getNextModePage() {
Intent nextModePage = new Intent(this, ModePage.class );
startActivity(nextModePage);
}
private void getNextPlayPage() {
Intent nextPlayPage = new Intent(this, PlayPage.class);
startActivity(nextPlayPage);
}
}