In tab it looks like below.
But in device it looks fine
..
I am looking like below what the google play contained in phones and tabs as same
this is my oncreate method. I had done scroll tabs with view pager .In devices it fits good but in tabs ?.I know this is action bar design pattern .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_directory_activity);
final ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
myDirectoryPagerAdapter = new MyDirectoryAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(myDirectoryPagerAdapter);
mViewPager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < myDirectoryPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(myDirectoryPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
It will look below the actionbar in the device because the device does not have much space to show all the tabs so the actionbar will manage on its own to show the tabs and adjust it according to the device size. As you have also displayed a logo in actionbar so it will always switch to below.
That is by default for Tabs from Action bar. It is good thing bcoz it saves some screen space for you.
If you dont want it to be present in the action bar. You need to implement it separately. Use PagerTitleStrip.
Two implementations here:
http://developer.android.com/training/implementing-navigation/lateral.html#PagerTitleStrip
https://github.com/astuetz/PagerSlidingTabStrip
Related
I want to set a background image for each tab in my application. I tried applying a icon but it wont fill the tabs. Then I tried putting a layout. It covers the all height but width isn't enough.
Below is my code Please help to fill a tab with a background image.
viewpager = (ViewPager) findViewById(R.id.pager);
FragmentPageAdapter ft = new FragmentPageAdapter(getSupportFragmentManager(), getApplicationContext());
actionBar = getActionBar();
viewpager.setAdapter(ft);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setCustomView(R.layout.a_selected).setTabListener(this)); // trying to apply a layout as background
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon_1).setTabListener(this));//applying a icon as background
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon_2).setTabListener(this));//applying a icon as background
Try to use
actionBar = getSupportActionBar();
instead of
actionBar = getActionBar();
ActionBar
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionBar.setBackgroundDrawable(background);
For setting background color in Tab Host Actionbar
public static void setTabColor(TabHost tabhost) {
for(int index=0;index<tabhost.getTabWidget().getChildCount();index++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#477a47")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
For setting image Tab Host Actionbar
But if you register for TabHost.OnTabChanged events and call mTabHost.getCurrentTabView() to get the View, then use view.setBackgroundResource().
tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
I am trying to create a tabbed activity, and I'm using ViewPager for that (actually, Android Studio created it from template, I didn't even know what ViewPager was). I want to style the tab-bar below (indicated with red "circle"):
I've tried setting styles.xml tried almost everything, but still, nothing applies to that. Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Ironically, the generated code (by Android Studio's latest version) has many deprecated method calls, but I don't want to touch them unless I know what exactly I'm doing. Here is the layout file (activity_main.xml), again, generated by Android Studio:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="MY_APP_IDENTIFIER"/>
How can I change the colors of the tab bar?
Try using onPageSelected it's probably the simplest way I think. Here is a link.
I have these 5 tabs and in the first tab/fragment I have a button, I want to be able to switch to another tab by clicking this button. Here's my code which contains the tabs:
actBar = getSupportActionBar();
actBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
secPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
vPager = (ViewPager) findViewById(R.id.pager);
vPager.setAdapter(secPagerAdapter);
vPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actBar.setSelectedNavigationItem(position);
}
});
Tab tab = actBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
actBar.addTab(tab, true);
tab = actBar.newTab()
.setIcon(R.drawable.cart)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.users)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.products)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.settings)
.setTabListener(this);
actBar.addTab(tab);
This creates me a pretty nice action bar with tabs and all, and as you see the one with the home drawable has the below code:
actBar.addTab(tab, true);
since it is true, when this activity is opened it starts with this tab. So... I have a button within this tab. When I tap this button, I want it to scroll right through to the third tab which has the users drawable as an icon. I've seen things about tabhost around here and well, if that's the 'only' case, I gotta say, I don't know about tabhost. I tried to change that true boolean to be able to switch between tabs onClick but it didn't work.
Thanks in advance. I'd really appreciate it.
use this inside button click listener
actionBar.setSelectedNavigationItem(tab_position);
I couldn't manage to change the action bar title for every swipe action, there's always a problem. Either they get mixed or just some of them does not show up. Here are my tab codes:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
ViewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPager.setAdapter(mSectionsPagerAdapter);
ViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab tab = actionBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
.
.
.
see answer below before going further into the question.
You can try using this in your Fragment's onResume method :
if(getUserVisibleHint()){
getActionBar().setTitle(title);
}
defining String title as a variable inside your fragment.
You should read the documentation on it: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
and
http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
Download the source code and use it. It worked for me. I had the same problem.
I am fairly good in fragments API, I know how to create master/detail pattern on phone/tablet.
But how would I create a different style of navigation in phones and tablets?
The most obvious example would be swiping tabs on the phone and somekind tablet layout with all the fragments layed out inside, no swiping tabs.
Is there some elegant way of doing this other than giant if statement?
//EDIT
So what would you do, as I presumed, is to create create two layouts (phone - view pager / tablet - three static fragments) and in activity's onCreate check for something in the layout to figure out wether youre in phone or tablet. Then just initialize the tabs navigation / or not if youre in tablet. That should work.
CODE SNIPPETS FROM IO 2012 APP
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FragmentManager fm = getSupportFragmentManager();
mViewPager = (ViewPager) findViewById(R.id.pager);
if (mViewPager != null) {
// Phone setup
mViewPager.setAdapter(new HomePagerAdapter(getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_my_schedule)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_explore)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_stream)
.setTabListener(this));
homeScreenLabel = getString(R.string.title_my_schedule);
} else {
//Tablet setup
mExploreFragment = (ExploreFragment) fm.findFragmentById(R.id.fragment_tracks);
mMyScheduleFragment = (MyScheduleFragment) fm.findFragmentById(R.id.fragment_my_schedule);
mSocialStreamFragment = (SocialStreamFragment) fm.findFragmentById(R.id.fragment_stream);
}
}
Any of you guys that already have tabs navigation on phone, please consider implementing this on the tablet, if it works for you design-wise. You already have fragments api ready, so there's no excuse, really.
You could take a look at the 2012 I/O app from Roman Nurik, which switches navigation/layout pretty much with every possible combination of size and orientation;
http://code.google.com/p/iosched/