Petr Karel
Petr Karel

Reputation: 53

action bar align buttons

I have an application with actionbar with a custom style. In action bar, I only have buttons. So I want to have one button in actionbar on the left, one in center of the action bar, and one on the right on action bar. How can I align them?

Upvotes: 1

Views: 1022

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

this may help you...

design one layout like this...

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/actionBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/leftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Left" />

    <Button
        android:id="@+id/centerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Center" />

    <Button
        android:id="@+id/rightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Right" />

</RelativeLayout>

and add this layout to ActionBar like this...

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);

    View view = LayoutInflater.from(actionBar.getThemedContext()).inflate(R.layout.activity_main, null);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(layoutParams);
    actionBar.setCustomView(view);

Upvotes: 3

Related Questions