-1

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 ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); ((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(R.layout.toobar_center_withback); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

my xml code

    <?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:orientation="vertical">

    <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" />

</LinearLayout>

it gives output enter image description here I also tried some margin to textview but how can I know the actual margin measurement for different screen

I followed these solutions but no success

How to align title at center of ActionBar in default theme(Theme.Holo.Light)

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

I also written the code but no success

 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);
}

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

enter image description here

Community
  • 1
  • 1
Umar Ata
  • 4,170
  • 3
  • 23
  • 35
  • i think, your textview is inflated after the back button. thats why it counts your toolbar after the default up button. So, basically you r using the gravity right and its working.. but its parent layout starts at just after the up button...thats why it is.. and when you add the button in toolbar it shifts the text to left(according to the button width). – SRB Bans Nov 05 '16 at 12:48
  • Actually I am using fragments and I am also changed the default behaviour of backbutton so for that I am calling ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); after settile – Umar Ata Nov 05 '16 at 12:52
  • doesn't matter if you are using fragment or not.. its not related.. its just about the views and its bounds. its not a problem.. up button is just taking its space.. and rest is for your text view title. – SRB Bans Nov 05 '16 at 13:00
  • so whats the solution – Umar Ata Nov 05 '16 at 13:01
  • it has nothing like solution.. it can be done by tricks.. just add the padding_right/margin_right to your textview (padding==up button size). – SRB Bans Nov 05 '16 at 13:05
  • well you rounded my mind , I already said in my question I applied some margin too but since backbutton different in different devices so how can I give textview a margin – Umar Ata Nov 05 '16 at 13:06
  • so how much margin should I provide to textview – Umar Ata Nov 05 '16 at 13:14
  • add a custom back drawable as back button(just one line of code). now you have a drawable and you can get the size... as i previously said.. its a trick. – SRB Bans Nov 05 '16 at 13:16

2 Answers2

2

I faced this problem too then I found the solution in:

1.Create custom toolbar custom_actionbar.xml

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorWhite"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo_action_bar" />
</RelativeLayout>

2.Disable default actionbar using

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

3.Then include this custom_actionbar in each layout

<include layout="@layout/custom_actionbar"/>
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
Hanzala
  • 1,965
  • 1
  • 16
  • 43
  • did you read my question correctly , I actually said I dont want to use any toolbar – Umar Ata Nov 05 '16 at 11:59
  • @UmarAta but you cannot do without using toolbar – Hanzala Nov 05 '16 at 12:01
  • 1
    My answer is quite relevant and work for your problem – Hanzala Nov 05 '16 at 13:34
  • I clearly mentioned in my question that I don't want to use any toolbar since my project is working and I can't change all the activities and fragments behavior , everthing is working fine even I am also getting fragment from backstack with proper text tile except of their center alignment problem so , I can't change the code of whole project – Umar Ata Nov 05 '16 at 13:37
2

So finally I found a trick to tackle this problem and it worked

setTitle("Sign In");
 ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setHasOptionsMenu(true);

here is my setTitle method

 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);
}

and here I created a menu item with transparent image

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.blank_menu,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

here is my blank_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
<item
    android:title="   "
    app:showAsAction="always|withText"
    android:icon="@drawable/transparent"
    >
</item>
</menu>

and now its giving output like this enter image description here

Umar Ata
  • 4,170
  • 3
  • 23
  • 35