Reputation: 1219
Hello I am noob in Android.
I am using appcompat
support library to add tabs in ActionBarActivity
. I have written the code to add but tabs is not showing see it in the screenshot. I could not catch the problem.
Any idea why it so ?
private ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the action bar
mActionBar = getSupportActionBar();
// First Tab of the Activity
ActionBar.Tab mTab = mActionBar.newTab().setText("First Tab").setTabListener(this);
mActionBar.addTab(mTab);
mActionBar.selectTab(mTab);
// Second Tab of the Activity
mTab = mActionBar.newTab().setText("Second Tab").setTabListener(this);
mActionBar.addTab(mTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTrasaction) {
if(tab.getPosition() == 0) {
fragmentTrasaction.replace(R.id.container, new FirstFragment());
} else {
fragmentTrasaction.replace(R.id.container, new SecondFragment());
}
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
Screenshot
Upvotes: 0
Views: 380
Reputation: 7376
you can add setNavigationMode like this:
actionBar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);
Upvotes: 1