I added some tab buttons in the action bar of my app. But I don't know how to add the coding to the each tab button. I want to open a new layouts from each tab button. I can't find the ids for these buttons.(I'm a beginner for android) Thank you!
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
I got tabs by using this this code in the onCreate method.
/** Creating Android Tab */
Tab tab = actionabar.newTab().setText("My Tabs").setIcon(R.drawable.myfriends).setTabListener(tabListener);
Tab tab = actionabar.newTab().setText("Tab1").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Tab2").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Tab3").setTabListener(tabListener);
actionabar.addTab(tab);
Use switch case to shift to tabs
switch (position) {
case 0:
tabLayout = R.layout.tab1;
break;
case 1:
tabLayout = R.layout.tab2;
break;
case 2:
tabLayout = R.layout.tab3;
break;
}
Related
I want to create an activity that can display any number of tabs, without creating an activity for each tab.
Is it possible?
The only way I saw creating tabs included creating an activity for each one.
You can try as follows ,
Extend your activity from ActionBarActivity and add required number of tabs programmatically ,
ActionBar.TabListener tabListener;
mactionBar = getActionBar();
mactionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabListener=new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
displayTabs(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
displayTabs(tab.getPosition());
}
};
mactionBar.addTab(mactionBar.newTab().setText(<any text>).setTabListener(tabListener));
//call the same method for required number of tabs
Call the same fragment for each tab click
private void displayTabs(int position)
{
Fragment fragment = null;
switch (position) {
case 0:
fragment = new YourFragment();
break;
case 1:
fragment = new YourFragment();
break;
// same for required numder of cases
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(<container id>, fragment).commit();
}
}
In making the swipe tabbed layout as below prior to Android 5.0:
Simply required the code as:
#Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
...
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
}
But given the methods for navigation on the ActionBar are deprecated, How can I implement this type of view to be compatible withe Android 5.0 and previous versions?
You need to update to Toolbar here is the documentation.
http://android-developers.blogspot.mx/2014/10/appcompat-v21-material-design-for-pre.html
In my onCreate() method I try to manually select one of my tabs to force it to pre-load, then switch back to the first tab manually. However, when I start, the first tab initally displays a blank screen and only loads after selecting another and then reselecting. This is my code in onCreate:
//set up tabs
actionBar.addTab(actionBar.newTab()
.setText("Set Current Location")
.setTabListener(new CustomTabListener<SetCurrentLocationFragment>(currLocFragment,this,SetCurrentLocationFragment.class)),true);
actionBar.addTab(actionBar.newTab()
.setText("Input Trip")
.setTabListener(new CustomTabListener<InputNewTripFragment>(inputNewTripFragment,this,InputNewTripFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("View Trip")
.setTabListener(new CustomTabListener<FoodMilesFragment>(calcMilesFragment,this,FoodMilesFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("Past Trips")
.setTabListener(new CustomTabListener<TripHistoryFragment>(tripHistoryFragment,this,TripHistoryFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("Trip Map")
.setTabListener(new CustomTabListener<ResultsMapFragment>(resultsMapFragment,this,ResultsMapFragment.class)));
//quickly pre-load the View Trip
actionBar.setSelectedNavigationItem(2);
actionBar.setSelectedNavigationItem(0);
And this is my TabListener
private class CustomTabListener<T extends Fragment> implements TabListener {
private Fragment fragment;
private final Activity mBaseActivity;//activity to attach fragment to
private final Class<T> mFragmentClass;
public CustomTabListener(Fragment fragment, Activity activity, Class<T> fragClass)
{
this.fragment = fragment;
mBaseActivity = activity;
mFragmentClass = fragClass;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null)
{
throw new IllegalArgumentException("Fragment must already be instantiated.");
}
//if the fragment has not yet been added to the activity, add it now
if(fragment.getActivity() == null || !fragment.isAdded())
ft.add(R.id.tabFragmentFrame, fragment);
ft.show(fragment);
fragment.setUserVisibleHint(true);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(fragment != null)
{
ft.remove(fragment);
fragment.setUserVisibleHint(false);
}
}
}
Something worth mentioning as well is that this version of my TabListener, with ft.hide(fragment) substituted for ft.remove(fragment) led to IllegalStateExceptions - Fragment already added.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null)
{
throw new IllegalArgumentException("Fragment must already be instantiated.");
}
//if the fragment has not yet been added to the activity, add it now
if(fragment.getActivity() == null || !fragment.isAdded())
ft.add(R.id.tabFragmentFrame, fragment);
ft.show(fragment);
fragment.setUserVisibleHint(true);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(fragment != null)
{
ft.hide(fragment);
fragment.setUserVisibleHint(false);
}
}
While I still do not know why my code before was not working - I did find that adding the tab into the specified position with setSelected = true would indeed make it appear.
//set up tabs
actionBar.addTab(actionBar.newTab()
.setText("Input Trip")
.setTabListener(new CustomTabListener<InputNewTripFragment>(inputNewTripFragment,this,InputNewTripFragment.class)),0);
actionBar.addTab(actionBar.newTab()
.setText("View Trip Footprint")
.setTabListener(new CustomTabListener<FoodMilesFragment>(calcFoodMilesFragment,this,FoodMilesFragment.class)),1);
actionBar.addTab(actionBar.newTab()
.setText("Past Trips")
.setTabListener(new CustomTabListener<TripHistoryFragment>(tripHistoryFragment,this,TripHistoryFragment.class)),2);
actionBar.addTab(actionBar.newTab()
.setText("Trip Map")
.setTabListener(new CustomTabListener<ResultsMapFragment>(resultsMapFragment,this,ResultsMapFragment.class)),3);
//quickly pre-load the Food Miles Trip Footprint
actionBar.setSelectedNavigationItem(1);
actionBar.addTab(actionBar.newTab()
.setText("Set Current Location")
.setTabListener(new CustomTabListener<SetCurrentLocationFragment>(currLocFragment,this,SetCurrentLocationFragment.class)),0,true);
This question already has answers here:
how to show text in lower case in android?
(6 answers)
Closed 3 months ago.
I created a tab application using Sherlock ActionBar Tab Views. I like to change the Tab title to lowercase. see screen shot
I want to change TAB1 to tab1
Any solution is greatly appreciated
my code as follow
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Activate Navigation Mode Tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getSupportFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
thanks
Please add this in TabLayout tag of xml file
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
this will help you :)
try this cod
text-transform: capitalize;
Try This :-
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x1 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x1.setAllCaps(false);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x2 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x2.setAllCaps(false);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
TextView x3 = (TextView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
x3.setAllCaps(false);
Is my following Action bar Tab implementation method is efficient or not? Because whenever i switch between tabs the Tab content fragments are replaced and load again in frame layout.
I want to add fragments at first time itself and show the fragments smoothly(without reloading) when switch between tabs.
Note: i tried to add all fragments on onCreate method.But the fragments are overlapped with each other and display all fragments in frame layout at same time.
My Code:
public class ManageActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
private Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab();
tab1.setText("Tab1");
tab1.setTabListener(this);
getSupportActionBar().addTab(tab1);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText("Tab2");
tab2.setTabListener(this);
getSupportActionBar().addTab(tab2);
ActionBar.Tab tab3 = getSupportActionBar().newTab();
tab3.setText("Tab3");
tab3.setTabListener(this);
getSupportActionBar().addTab(tab3);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
String selectedtab = tab.getText().toString();
if(selectedtab.equalsIgnoreCase("Tab1"))
{
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab1Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab2")) {
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab2Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab3")){
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab3Fragment()).commit();
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_manage.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tabfragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManageActivity" >
</FrameLayout>
Note: My Tab contents are Listfragments which query data from Sqlite using loader manager.
I use a ViewPager that contains all my fragments. Every time a tab is selected I only switch the position in the Viewpager.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = adapter.findItemPosition(this.id);
ViewPager viewPager = (YourActivity)getActivity().getViewPager(); // Convenience method
if (viewPager != null) {
viewPager.setCurrentItem(position, this.shouldScroll);
} else {
Log.d(getClass().getSimpleName(), "No pager available");
}
}
To make this code working you need a Viewpager and have to add all tabs into the ViewPager through an instance of PagerAdapter. I use a FragmentPagerAdapter as there are not that many tabs most of the time.
This also enables your user to swipe through the tabs. If the user changes the selected tab through swiping you need to update the selected tab. I use ActionBarSherlock in all of my Apps. Therefore the next Snippet uses a supportActionBar. This code example is a simple listener that can be set on the ViewPager to update tab changes through swiping.
private final class TabPageChangedListener extends ViewPager.SimpleOnPageChangeListener {
#Override
public void onPageSelected(int position) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSelectedNavigationItem(position);
} else {
Log.e(getClass().getSimpleName(),
"No actionbar available to change selected tab.");
}
}
}
There is also the possibility to disable swiping by overwriting the ViewPager and intercepting the swiping touch motions if you do not want the tabs to be swipeable.