Losing an ActionBar Header When Creating a Custom Layout

I am trying to create an ActionBar that has:

  • Home button with heading on (left)
  • TextView (centered)
  • Overflow menu items (right)

I followed this answer to an almost identical question. The difference is that I would like to show the name. I tried actionBar.setTitle("Title"), but it is cut off by LinearLayout and is not visible.

How to create a custom view in the center of my ActionBar with the above elements?

Can I do without overriding the entire ActionBar?

Edit:

I installed an ActionBar here:

final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setTitle("Title");
actionBar.setCustomView(R.layout.some_layout);

Here is the file some_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text" />

</LinearLayout>

Here is the result from the ActionBar that I get from this (I set the LinearLayout background to green and the text to white):

action bar

Here is what I am trying to get (without a green background):

enter image description here

+4
1

:

        ActionBar ab = getSupportActionBar();
    ab.setCustomView(R.layout.registration_actionbar);
    ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
0

Source: https://habr.com/ru/post/1542845/


All Articles