0

I have tried some solutions but they don't seem to work. The color and boldness take effect but not the centering, even though it displays right in AndroidStudio. Also I tried out of I had enough space and if the title is long enough, then it goes all the way to the edge of the screen, but if it's short it stays at the beginning.

Here is my 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:orientation="vertical" >

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="my Title"
        android:textColor="@color/barTextColor"
        android:textSize="18sp" />

</LinearLayout>

And onCreate():

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        setContentView(R.layout.activity_homescreen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.bar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_open){

            @Override
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                super.onDrawerSlide(drawerView, 0);
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, 0); // this disables the animation
            }
        };

        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
newToEverything
  • 309
  • 4
  • 13
  • Don't bother with a custom `View` layout. Just put your `` directly in the `` in your layout, set the ``'s `layout_width` to `wrap_content`, and its `layout_gravity` to `center`. I would also mention that those `ActionBarDrawerToggle` overrides are unnecessary. Just don't set the toggle as a `DrawerListener` if you don't want the animation. – Mike M. Apr 16 '17 at 10:14
  • @MikeM. I removed the LinearLayout and it still doesn't center. – newToEverything Apr 16 '17 at 10:34
  • I don't know what you mean. I meant, get rid of the `bar` layout and `setCustomView()` call altogether. Put the `` directly in the ``, and make sure the `` has `wrap_content` for the `layout_width`, and `center` for the `layout_gravity`. Look here: http://stackoverflow.com/a/26548766 – Mike M. Apr 16 '17 at 10:39
  • Tried copying the XML too but it still was stuck at the left :/ – newToEverything Apr 16 '17 at 11:02
  • Are you sure your `Toolbar` is full-width? That is, it has `match_parent` for its `layout_width`? Also, if you've gotten rid of the custom `View`, you need to remove that `setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)` call, too. – Mike M. Apr 16 '17 at 11:11
  • Thanks, found the problem. My TextView was outside of the Appbar! – newToEverything Apr 16 '17 at 11:30

0 Answers0