Skip to content Skip to sidebar Skip to footer

How Can I Make The Items On Actionbar To Be One On The Left, One In The Center And One On The Right?

I'm using actionbarsherlock to do it. Example of what I want in the actionbar: [LOGIN]     [COMPANY LOGO]     [FILTER] Example of what I get in the actionbar:      

Solution 1:

You should create custom view for that. For example (layout/ab_custom.xml):

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="match_parent"android:layout_gravity="fill_horizontal" ><ImageButtonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:src="@drawable/left" /><ImageButtonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/center" /><ImageButtonandroid:id="@+id/btn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/right" /></RelativeLayout>

Then in your Activity's onCreate call this method:

privatevoidshowActionBar() {
        LayoutInflaterinflator= (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Viewv= inflator.inflate(R.layout.ab_custom, null);
    ActionBaractionBar= getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled (false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setCustomView(v);
}

To take control on your items use this:

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.btn1:
            //left button, do somethingreturntrue;
        case R.id.btn2:
            //center buttonreturntrue;
        case R.id.btn3:
            // right buttonreturntrue;
        default:
            returnsuper.onOptionsItemSelected(item);
        }
    }

EDIT: revised my answer. It's better to use RelativeLayout as parent.

Solution 2:

Wrap the items inside a RelativeLayout. Then use:

android:layout_alignParentLeftandroid:layout_alignParentCenterandroid:layout_alignParentRight

Solution 3:

To add on to Sabre's answer and address snadandpepper's comments. In order to set onClickListeners to the button, you can do the following.

privatevoidshowActionBar(){
        getSupportActionBar().setCustomView(R.layout.my_toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled (false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.getCustomView().findViewById(R.id.btn1).setOnClickListener(v -> {
            Log.d(TAG, "showActionBar: btn1 clicked");
        });
}
'''

Post a Comment for "How Can I Make The Items On Actionbar To Be One On The Left, One In The Center And One On The Right?"