applying a background image for tabs in android - 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);

Related

how to implement a action bar like facebook, by removing header from tab header

I have implemented action bar and ab Layout with Swipeable Views. I have put a image to each title in the tabs and title. when i am changing the tab, actionbar respectively shows the tab title name in the actionbar. now I want to leave the title only from the tab header, and leave actionbar title and image on the tab header. so it will be looks like facebook app
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(this)
.setIcon(R.drawable.ic_tab_pizza));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(this)
.setIcon(R.drawable.ic_tab_noodle));
actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(this)
.setIcon(R.drawable.ic_tab_slides));
actionBar.addTab(actionBar.newTab().setText("Tab4").setTabListener(this)
.setIcon(R.drawable.ic_tab_promotion));
actionBar.addTab(actionBar.newTab().setText("Tab5").setTabListener(this)
.setIcon(R.drawable.ic_tab_profile));
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// set background for action bar tab
bar.setStackedBackgroundDrawable(new ColorDrawable(Color
.parseColor("#ffffff")));
bar.show();
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
getActionBar().setTitle(tab.getText());
viewPager.setCurrentItem(tab.getPosition());
}

Change background color of a specific tab in android tabs swipe

This is my onCreate of my main activity
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private TabHost tabHost;
// Tab titles
private String[] tabs = { "1", "2", "3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
//actionBar.setStackedBackgroundDrawable(new ColorDrawable(0xFFFF0000));
tabHost = (TabHost) findViewById(android.R.id.tabhost);
Log.d("miau", String.valueOf(tabHost));
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
I need that every tab have a different color. Any pointer? thanks in advance.
Just take the ImageView for the particular Tab in the corresponding xml. And then give that image view Width and Height "MatchParent". And set the backgroung color for that Imageview.
You could maybe try to use the setCustomView(View v) method when you're adding a tab.
So like:
//...code
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this).setCustomView(myView);
}
//...more code...
and then make your custom view (myView) just be a plain view with whatever color you want. I'm assuming you don't have a bunch of tabs. If you do then you may want to make a view, then randomly assign it a color on the fly. So have a large array of colors and then in your for loop assign myView a color and then set that view to your tab.
I'm not sure how this will affect the text of the tab though, but if it does you could add a textview to you custom view and then add the titles that way.
A more complicated version would be to make your own custom tab class and override everything that way.

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

How to remove Action Bar and set the custom title of FragmentActivity of Sherlock

Hello I am using Sherlock library to achieve the Tabs. Here I am looking to remove the ActionBar at the top and set the custom title layout instead of it.
I achieved a functionality to add the Tabs but could not set the custom title over ActionBar. Any way to do this ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_stub);
// adding action bar tabs to the activity
ActionBar mActionBar = getSupportActionBar();
Tab firstTab = mActionBar.newTab().setText(getString(R.string.paystub_current_tab)).setTabListener(this);
Tab secondTab = mActionBar.newTab().setText(getString(R.string.paystub_all_tab)).setTabListener(this);
// add the tabs to the action bar
mActionBar.addTab(firstTab);
mActionBar.addTab(secondTab);
// set the navigation mode the Action Bar Tabs
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
Thanks in advance.
I resolved this by adding background color and icon of the ActionBar.
ActionBar actionBar = getSupportActionBar();
setTitle("");
actionBar.setIcon(R.drawable.headerlogo);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F4A401")));

How to fix scrollable tabs always below the action bar in android

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

Categories

Resources