0

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);
Vasko Vasilev
  • 554
  • 1
  • 10
  • 25

3 Answers3

2

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

Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
  • I have a custom toolbar. My issue currently is getting the toolbar to inflate. – Vasko Vasilev Jul 09 '19 at 08:34
  • 1
    I don't recommend using `ActionBar`. While using custom Toolbar you can still do the same and even more. Further, if you want you can add custom click listeners to the `TextViews` – Kartik Shandilya Jul 09 '19 at 08:39
1

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.

Juke
  • 38
  • 5
0

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

Vasko Vasilev
  • 554
  • 1
  • 10
  • 25