set the actionBar tab's icon and text vertical to each other - android

Tab footballTab = actionBar.newTab();
footballTab.setText("Football");
footballTab.setIcon(android.R.drawable.btn_radio); //whatever
footballTab.setTabListener(this);
Tab basketBallTab = actionBar.newTab();
basketBallTab.setText("Basketball");
basketBallTab.setIcon(android.R.drawable.btn_star_big_off); //whatever
basketBallTab.setTabListener(this);
Tab tennisTab = actionBar.newTab();
tennisTab.setText("Tennis");
tennisTab.setIcon(android.R.drawable.btn_minus); //whatever
tennisTab.setTabListener(this);
actionBar.addTab(footballTab, false);
actionBar.addTab(basketBallTab, true);
actionBar.addTab(tennisTab, false);
The result shows the icon and the text next to each other in a horizontal manner.
Is there a way I can show the icon and set the text below it ?
Moreover, is it possible to pass in custom views for the tab icon ?

Related

applying a background image for tabs in android

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);

Android Fragment create duplicate tabs every time when I come back to same fragment

I have create the sample activity using frame layout and fragment with tabs. However, when I switch to other activity/fragment and than comes back to same activity/fragment, it always create duplicate entry or view for tabs. for example, I have tab1 and tab2, when I view activity for first time it display two tabs, but when i switch to other activity and comes back to tabs activity it display four tabs, 'tab1, tab2, tab1, tab2'.
This is my code
public View onCreateView(LayoutInflater Inflater, ViewGroup Container,Bundle savedInstanceState) {
if(savedInstanceState==null) {
rootView = Inflater.inflate(R.layout.loanapplicationview, Container, false);
actionBar = getActivity().getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(true);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
return rootView;
}
}
You should check if the tabs exist before adding them.
if (actionBar.getTabCount() == 0) {
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
ActionBar().removeAllTabs() will remove all the tabs attached to your ActionBar.
So before addinf new tabs clear the previous ones with this method
actionBar.removeAllTabs();
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);

Change android actionbar tab text color on orientation change programaticly?

I want to change color of text in tabs. How can I reference the tab layout in which I want to change color property inside of the function:
public void onConfigurationChanged(Configuration newConfig)
{
findViewById(R.id.tab_textview); // returns null
}
Since this returns null. tab_textview is the template for the tab. In the onCreate I just put tabs inside the actionbar and everything works. I just need to change color when orientation is changed so the text is white and visible. Find many similar problems but I cant get it to work. I am very new to android programming.
At the onCreate method, we initial the ActionBar like this:
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar
.newTab()
.setCustomView(R.layout.tab_textview) // use our TextView
.setTabListener(
new Chapter1TabListener<FragmentA>(this, "fragmentA",
FragmentA.class));
TextView tabview = (TextView) tab.getCustomView();
tabview.setText("First Tab");
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setCustomView(R.layout.tab_textview)
.setTabListener(
new Chapter1TabListener<FragmentB>(this, "fragmentB",
FragmentB.class));
tabview = (TextView) tab.getCustomView();
tabview.setText("Second Tab");
actionBar.addTab(tab);
Override onConfigurationChanged, try as following:
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
ActionBar actionBar = getActionBar();
for(int i=0; i<actionBar.getTabCount(); i++ ) {
Tab tab = actionBar.getTabAt(i);
TextView tv = (TextView) tab.getCustomView();
tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
}
}

Changing between action bar tabs by clicking a common button in android

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);

Fetching tab title in viewpager

I have implemented ViewPageIndicator and it is running fine. I have 3 tabs with names "Tab1", "Tab2", and "Tab3". What I want is the tab name when I scroll the tab.
Could anyone let me know how to fetch the tab title of the current tab that is being displayed?
You can get the title for the current page from your PagerAdapter:
int currentPage = viewPager.getCurrentItem();
CharSequence pageTitle = pagerAdapter.getPageTitle(currentPage);

Categories

Resources