How to align Title in center of Actionbar with back button in fragment
<p>I am trying to make a title bar of actionbar in the center ,
I tried the code to make it center but no success
my code inside fragment <code>((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(R.layout.toobar_center_withback); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
</code></p>

<p>my xml code</p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"&gt;

    &lt;TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center"
        android:text="SIGN IN"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textStyle="bold" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>it gives output <a href="https://i.stack.imgur.com/Adeao.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Adeao.png" alt="enter image description here"></a>
I also tried some margin to textview but how can I know the actual margin measurement for different screen</p>

<p>I followed these solutions but no success </p>

<p><a href="https://stackoverflow.com/questions/18418635/how-to-align-title-at-center-of-actionbar-in-default-themetheme-holo-light">How to align title at center of ActionBar in default theme(Theme.Holo.Light)</a></p>

<p>Another thing is that I dont want to use any toolbar or something else ,I want a simple solution for all fragments</p>

<p>I also written the code but no success </p>

<pre><code> public void setTitle(String title) {
    ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    TextView textView = new TextView(getActivity());
    textView.setText(title);
    textView.setTextSize(20);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textView.setGravity(Gravity.CENTER);
    textView.setTextColor(getResources().getColor(R.color.black));
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setCustomView(textView);
}
</code></pre>

<p>It works fine when I have no button or have backbutton and also have a button in menu with showaction always like this </p>

<p><a href="https://i.stack.imgur.com/A1WOA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A1WOA.png" alt="enter image description here"></a></p>