how to make custom actionbar to fill screen width when used with swipable tab view?

806 Views Asked by At

Hi I want to have custom action bar with swipable tab view. I have created custom action bar and im setting the custom view for actionbar. action bar works well when i use with other screens but when i use it with tabview, app icon appears on the left side of the action bar. Please help me to make my custom action bar to fill entire width over tabview. My code is like this,

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //  requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.tab_bar);
    final Animation animLogOut=AnimationUtils.loadAnimation(this, R.anim.animzoom);
    final Animation animBack=AnimationUtils.loadAnimation(this, R.anim.animzoom);
//  final Animation animHome=AnimationUtils.loadAnimation(this, R.anim.animzoom);

//2 ActionBar objects for custom action bar and Tab view
    mActionBar=getActionBar();
    actionBar = getActionBar();

    viewPager = (ViewPager)findViewById(R.id.pager);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.actionbar, null);

    mActionBar.setDisplayShowHomeEnabled(false); 
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setCustomView(mCustomView, lp1);

    imLogout = (ImageView)mCustomView.findViewById(R.id.imlogout);
    imBack = (ImageView)mCustomView.findViewById(R.id.imback);
    imHome = (ImageView)mCustomView.findViewById(R.id.home);
    imHome.setVisibility(View.INVISIBLE);

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

    /**
     * on swiping the viewPager make respective  tab selected
     */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position){
            //on changing the page
            //make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

}

EDIT1: here is my custom action bar layout which conatils 4 imageViews.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llheader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal"
    android:background="@color/headerbackground"
    android:orientation="horizontal" >

        <ImageView
             android:id="@+id/imlogout"
            android:layout_width="40dp"
            android:layout_height="40dp"
              android:padding="6dp"
             android:layout_alignParentRight="true"
             android:layout_centerVertical="true"
             android:layout_marginRight="10dp"
             android:src="@drawable/logout" />
  <ImageView
            android:id="@+id/home"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/imlogout"
            android:padding="6dp"
            android:src="@drawable/home" />
        <ImageView
            android:id="@+id/productlogo"
            android:layout_width="wrap_content"
           android:layout_height="25dp"
            android:layout_alignParentTop="true"
           android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/imback"
            android:paddingBottom="7dp"
            android:src="@drawable/harmaan" />


          <ImageView

             android:id="@+id/imback"
           android:layout_width="40dp"
            android:layout_height="40dp"
              android:padding="6dp"
             android:layout_alignParentLeft="true"
             android:layout_centerVertical="true"
             android:src="@drawable/back_icon" />

    </RelativeLayout>

and on the screen app logo appears on the left. because of low reputation i cannot post the screen schot. please help me to solve this issue.

1

There are 1 best solutions below

0
On

Change this code

LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

by

ActionBar.LayoutParams lp1 = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);

then check hope it will help you