i'm used support action bar in a application and test in 2 devices Nexus S and Nexus 7 and i found different results regarding tab bar width ,Nexus S tab bar fill width while nexus 7 tab bar leave some space in left side. i used default theme provided by support action bar , applying custom themes does not effect action bar because parent style must be Theme.AppCompat.Light
Activity in Manifest
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Class:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
ActionBar bar;
ViewPager pager;
TabsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
bar = getSupportActionBar();
adapter = new TabsAdapter(getSupportFragmentManager());
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setHomeButtonEnabled(false);
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the
// corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have
// a reference to the
// Tab.
bar.setSelectedNavigationItem(position);
}
});
Tab hometab = bar.newTab();
hometab.setText("Home");
hometab.setTabListener(this);
bar.addTab(hometab);
Tab cameratab = bar.newTab();
cameratab.setText("Camera");
cameratab.setTabListener(this);
bar.addTab(cameratab);
Tab exploretab = bar.newTab();
exploretab.setText("Explore");
exploretab.setTabListener(this);
bar.addTab(exploretab);
bar.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
pager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
here is screen shots
Use High Resolution images for tablets.Put that inside drawable-xxhdpi folder .If this solution doesn't works.Then
Alternate Method:
Steps
1:Create 3 Separate layouts --one for Home tab,one for camera tab and one for explore tab.
2:Using Layout Inflater ,You can add this layout separately to these tabs.
Sample Code:
public class MyActivities extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.customActionBarTabStyle);
super.onCreate(savedInstanceState);
}
public static class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ignoredFt) {
FragmentManager fragMgr = ((FragmentActivity) mActivity)
.getSupportFragmentManager();
FragmentTransaction ft = fragMgr.beginTransaction();
mFragment = Fragment.instantiate(mActivity, mClass.getName());
// ft.add(android.R.id.content, mFragment, mTag);
ft.add(R.id.relMiddle, mFragment, mTag);
ft.addToBackStack(mTag);
ft.commit();
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
FragmentManager man = ((FragmentActivity) mActivity)
.getSupportFragmentManager();
if (man.getBackStackEntryCount() > 0)
man.popBackStack(man.getBackStackEntryAt(0).getName(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.detach(mFragment);
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
public void settings() {
actionbar = getSupportActionBar();
actionbar.setDisplayShowTitleEnabled(false);
getSupportActionBar().setCustomView(R.layout.myactivities_header);
getSupportActionBar().setDisplayShowCustomEnabled(true);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/*
* For showing custom top layout in actionbar
*
* Build verson is checking for Api <3.0
*/
View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home
: R.id.abs__home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
mTabsAdapter = new TabsAdapter(this, actionbar, mViewPager);
/*
* Use Customview for TABS IN FULL WIDTH
*
* 1: Note: use imageview with full width and full height.
*
* 2: Note : If we use seticon instead setContentview,Tabs may not show
* in full width /height
*/
mTabsAdapter.addTab(
actionbar.newTab().setText(" ")
.setCustomView(R.layout.cus_home_tab),
Home.class, null);
mTabsAdapter.addTab(
actionbar.newTab().setText("")
.setCustomView(R.layout.cus_camera_tab),
Camera.class, null);
mTabsAdapter.addTab(
actionbar.newTab().setText("")
.setCustomView(R.layout.cus_explore_tab),
Rxplore.class, null);
}
public static class TabsAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ActionBar bar,
ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
System.out.println("inside tab adapter");
}
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss,
Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
//System.out.println("Tabs Size is " + mTabs.size());
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With