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:
[LOGIN] [COMPANY LOGO] [FILTER]
I have created the login button, company logo and filter button (in the form of drawables) in the res/menu's activity_main.xml
. However, those buttons on the actionbar are unable to shift fully to the left, even though I have removed the default application logo and set those to false
:
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled (false);
here are my codes in the menu's activity_main.xml
:
<item android:id="@+id/login"
android:icon="@drawable/login_btn"
android:layout_gravity="left"
android:showAsAction="always"/>
<item android:id="@+id/logo"
android:icon="@drawable/logo_btn"
android:layout_gravity="center"
android:showAsAction="always"/>
<item android:id="@+id/filter"
android:icon="@drawable/filter_btn"
android:layout_gravity="right"
android:showAsAction="always"/>
You should create custom view for that. For example (layout/ab_custom.xml
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal" >
<ImageButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/left" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/center" />
<ImageButton
android: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:
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.ab_custom, null);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled (false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
To take control on your items use this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btn1:
//left button, do something
return true;
case R.id.btn2:
//center button
return true;
case R.id.btn3:
// right button
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT: revised my answer. It's better to use RelativeLayout as parent
.
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