Search code examples
android-actionbarandroid-custom-viewfill-parent

don't fill parent custom layout for action bar


I have three layouts: 1. calculate_actionbar_layout.xml 2. purchasingmanager_actionbar_layout.xml 3. usermanagement_layout.xml

When I select a tab from action bar the relate layout for this tab with item show in action bar.but not fill custom layout in action bar.

Image of my app

Code of mainactivity:

ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
View cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);
actionbar.setCustomView(cView);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

ImageView i=(ImageView) cView.findViewById(R.id.imageviewadduser);
i.setOnClickListener(this);

On selected tab change action bar:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
final ActionBar actionbar = getActionBar();
View cView=null;
switch (tab.getPosition()) {
case 0:
cView = getLayoutInflater().inflate(R.layout.calculate_actionbar_layout, null);
 actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.calculate_actionbar_layout);

break;
case 1:
cView = getLayoutInflater().inflate(R.layout.purchasingmanager_actionbar_layout, null);
actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
break;
case 2:
cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);

actionbar.setCustomView(cView);

//actionbar.setCustomView(R.layout.usermanagement_layout);
break;
default:
break;
}
viewpager.setCurrentItem(tab.getPosition());
}

custom layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#0d93d2"
android:weightSum="4"
android:gravity="center"
android:layout_gravity="center"
 >
<ImageView
android:id="@+id/imageviewusermanagment"
android:layout_width="0sp"
android:layout_weight="2.5"
android:layout_height="wrap_content"
android:src="@drawable/usermanagment"
android:scaleType="fitStart"
android:paddingLeft="20sp"

 />
 <ImageView
android:id="@+id/imageviewdeleteuser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/deleteuser"

 />



<ImageView
android:id="@+id/imageviewedituser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/edituser"/>

<ImageView
    android:id="@+id/imageviewadduser"
android:layout_width="0sp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:src="@drawable/adduser"
android:onClick="onClick"
     />

</LinearLayout>

Solution

  • change onTabSelected to this: you should define LinearLayout.LayoutParams to your custom layout actionbar

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        final ActionBar actionbar = getActionBar();
        View cView=null;
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        switch (tab.getPosition()) {
        case 0:
    
             cView = getLayoutInflater().inflate(R.layout.calculate_actionbar_layout, null);
             cView.setLayoutParams(param);
             actionbar.setCustomView(cView);
    
            //actionbar.setCustomView(R.layout.calculate_actionbar_layout);
    
            break;
        case 1:
    
            cView = getLayoutInflater().inflate(R.layout.purchasingmanager_actionbar_layout, null);
            cView.setLayoutParams(param);
            actionbar.setCustomView(cView);
    
            //actionbar.setCustomView(R.layout.purchasingmanager_actionbar_layout);
            break;
        case 2:
             cView = getLayoutInflater().inflate(R.layout.usermanagement_layout, null);
             cView.setLayoutParams(param);
            actionbar.setCustomView(cView);
    
            //actionbar.setCustomView(R.layout.usermanagement_layout);
            break;
        default:
            break;
        }
        viewpager.setCurrentItem(tab.getPosition());
    }