Skip to content Skip to sidebar Skip to footer

Center Align Right Aligned Button On Action Bar

I have added a button to the Action bar but now it is aligned to the far right like this I want to align that button to the center like this. How can I acheive this? Is there a w

Solution 1:

Replace actionbar by android.support.v7.widget.Toolbar where you can put Button with android:layout_gravity="center".

Add support library dependency compile 'com.android.support:support-v13:25.3.1'

Define toolbar with button in your layout:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    ...
    >
    <Button
        android:id="@+id/toolbar_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        ...
        />
</android.support.v7.widget.Toolbar>

Set toolbar in activity

publicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example);

    Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Buttonbutton= (Button) findViewBydI(R.id.toolbar_button);
    ...
}

Post a Comment for "Center Align Right Aligned Button On Action Bar"