Android center name Android with home button

I was able to center the ABS title using a custom view, but the home button was gone. How can this be solved? Is this the only way to do this to put that home button in a custom view? (To be clear: home button should be left not center)

Custom view:

<?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="match_parent"
    android:gravity="center"
    android:background="@android:color/transparent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"/>

</LinearLayout>

      

Operation code:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.widget_actionbar_title);
getSupportActionBar().setHomeButtonEnabled(true);

      

+3


source to share


4 answers


I had to add this code to make it work:



getSupportActionBar().setDisplayShowHomeEnabled(true);

      

0


source


    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    View customView = LayoutInflater.from(this).inflate(
            R.layout.app_icon, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

    getSupportActionBar().setCustomView(customView, params);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.back_button));

      



+4


source


Add this:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  | ActionBar.DISPLAY_SHOW_HOME);

      

+1


source


very simple solution) Change layout

android:layout_width="match_parent"

      

:

android:layout_width="wrap_content"

      

this will help because your initial layout takes up all visible space and there is no room for a "home"

+1


source







All Articles