Vasko Vasilev
Vasko Vasilev

Reputation: 574

How can I place two buttons on the action bar? One at the beginning and one at the end

I want to edit my menu bar in such a way that it has only two buttons and they are placed at the beginning and the end of the action bar. By default they appear to be automatically placed at the end of the action bar. Searching their attributes I was not able to find any that position the item elements. How can I get from where I am to what I need?

My XML:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/Menu"
    android:orderInCategory="0"
    android:title="@string/Menu"
    app:showAsAction="always" />

<item
    android:id="@+id/Bucket"
    android:title="@string/Bucket"
    app:showAsAction="always"
    android:orderInCategory="1"/>

What I currently have:

two muppets

What I want to achieve:

two muppets

EDIT: created a custom action bar and am attempting to inflate it, however I receive a null pointer exception and run time exception at the fourth and fifth line of the newly added code:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pogolemotoproektce, PID: 8377
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pogolemotoproektce/com.example.pogolemotoproektce.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null object reference

This is the added code:

    final ActionBar actionBar = getActionBar();
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_action_bar, null);
    actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Upvotes: 0

Views: 173

Answers (3)

Vasko Vasilev
Vasko Vasilev

Reputation: 574

Problem solved by creating a custom action bar and inflating it. The null pointer error and run time error were solved by replacing the erroneous code mentioned in the edit with the following snippet:

    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_action_bar,null);
    getSupportActionBar().setCustomView(view);
    getSupportActionBar().setDisplayShowCustomEnabled(true); 

Code copied and edited from the answer of the following question: Android action bar with two stretched buttons

Upvotes: 0

Kartik Shandilya
Kartik Shandilya

Reputation: 3924

You can create a custom Toolbar like this -

<androidx.appcompat.widget.Toolbar
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <LinearLayout
                android:layout_marginEnd="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="start"
                    android:text="Menu" />

            <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="end"
                    android:text="Bucket" />
        </LinearLayout>

</androidx.appcompat.widget.Toolbar>

This results in -

Sample Toolbar

Upvotes: 2

Juke
Juke

Reputation: 38

May you have a look on these two links:

Android ActionBar custom layout styling

or

Android action bar with two stretched buttons

The best way to achieve something like this, would be, to implement your own custom view as action bar.

Upvotes: 1

Related Questions