91

React-navigation docs are still young, and reading through the issues is not working quite much for me (changes on each version) does anyone have a working method to center title in Android using react-navigation in React Native?

jsdario
  • 6,477
  • 7
  • 41
  • 75
  • there may be some solution for this, but what I do in my projects I don't use header and tabs from react-navigation. For header I use custom header (******performance of react-navigation tabs isn't that great*********). – Manjeet Singh Apr 10 '17 at 15:51
  • Theres a `titleStyle`props that can be used for styling. Did you try it out ? Here's the [link](https://github.com/react-community/react-navigation/search?utf8=✓&q=titleStyle). Check the `StackNavigator.md` – Himanshu Soni Apr 10 '17 at 16:54
  • I did, i am trying to set default props to all screens, it did not work :/ – jsdario Apr 10 '17 at 17:32

36 Answers36

175

Warning: react-navigation changes a lot, the way to do title align, already changed for like 2-3 times from my first answer of it.

If my answer doesn't work for you, please see other answers.

Modified 2021/08/31:

In year of 2020, headerLayoutPreset was also deprecated. The new way is via defaultNavigationOptions: (@ThatFrenchComputerGuy's answer helped me)

const AppNavigator = createStackNavigator({
    Home: { screen: HomeScreen },
 }, 
 {
  defaultNavigationOptions: {
      title: 'Aligned Center',
      headerTitleAlign: 'center'
  }
});

Modified 2019/03/12:

In year of 2018, after react-navigation v2 release (7 Apr 2018), for some reason alignSelf was not working anymore. Here it is the new working way, using headerLayoutPreset. from @HasanSH:

const HomeActivity_StackNavigator = createStackNavigator({
    Home: {screen: Main},
}, {headerLayoutPreset: 'center'});

Original Answer 2017/07/11:

Use headerTitleStyle:

static navigationOptions = {
    headerTitleStyle: { alignSelf: 'center' },
    title: 'Center Title',
}

enter image description here

Val
  • 21,938
  • 10
  • 68
  • 86
130

As of 2021, you can use headerTitleAlign.

Although headerLayoutPreset technically works, it should display a message informing that it is deprecated for expo users. The implementation is as follows:

const AppNavigator = createStackNavigator({
    Home: HomeScreen,
 }, 
 {
  defaultNavigationOptions: {
      title: 'Centered',
      headerTitleAlign: 'center'
  }
})

React-Navigation v5.x Update: As per @Ewan ' s rectification, if you are using React-Navigation v5.x, you cannot pass parameters to createStackNavigator(). Therefore, you should implement it as follows:

<Stack.Navigator screenOptions={{headerTitleAlign: 'center'}}>

Here is an image of it working

cjmling
  • 6,896
  • 10
  • 43
  • 79
ThatFrenchComputerGuy
  • 1,486
  • 1
  • 5
  • 8
61

To center the header title, we need to have flex header by add flex property.

navigationOptions: {
    title: "Title center",
    headerTitleStyle: { 
        textAlign:"center", 
        flex:1 
    },
}
Cong Nguyen
  • 3,199
  • 1
  • 23
  • 22
50

For anyone searching in 2020, this is working for me:

<Stack.Navigator screenOptions={{headerTitleAlign: 'center'}}>

https://reactnavigation.org/docs/stack-navigator/#headertitlealign

Ewan
  • 721
  • 5
  • 8
31

The accepted answer only works for me if there isn't a back button present on the left-hand side. In that case, you need to set an empty View to the right-hand side to properly center it.

static navigationOptions = {
    headerTitleStyle: { alignSelf: 'center' },
    title: 'Center Title',
    headerRight: (<View />)
}
David Schumann
  • 13,380
  • 9
  • 75
  • 96
Rawa
  • 411
  • 5
  • 3
  • This is what I do most often, but I find this is a work-around and should not be treated as a proper answer. I have upvoted it anyway, because it's handy. – jsdario Feb 08 '18 at 23:28
  • I totally agree that it's a workaround and I wish it wasn't. But I don't think there's a proper solution yet. Hopefully, it will be fixed soon. – Rawa Feb 09 '18 at 16:59
  • 1
    I also added `flex: 1, textAlign: 'center',` to headerTitleStyle and `headerLeft: (),` AND worked – Nagibaba Aug 18 '18 at 10:45
  • This works for me if I have a button on both sides. `{headerTitleStyle: { alignSelf: 'center' }, title: 'Center Title', headerLeft: (), headerRight: () }` – Harry Moreno Feb 06 '19 at 17:56
24

The best way to do that is by implementing what is listed in the documentation: Inside the StackNavigatorConfig assign an optional property, as follows:

createStackNavigator({
   { ... // your screens},
   { ...,
     headerLayoutPreset: 'center' // default is 'left'
   })

headerLayoutPreset - Specifies how to lay out the header components.

By doing this you don't have to mess up with the styling at all. And it would be applied to all the nested screens in that stack.

Check the Source

Hasan Sh
  • 1,018
  • 9
  • 16
10

Insert and set the headerTitleAlign value in defaultNavigationOptions. Here is the examples:

const MainStack = createStackNavigator(
    {
        ...

        defaultNavigationOptions: {
            headerTitleAlign: 'center',
        }
    }
)
AndriyFM
  • 1,389
  • 14
  • 11
8

This worked for me :)

static navigationOptions = {
title: "SAMPLE",
headerTitleStyle: { flex: 1, textAlign: 'center'},

};

Community
  • 1
  • 1
Pavitha
  • 181
  • 2
  • 10
8

For React Navigation V6 Following is working fine for me:

<Stack.Navigator screenOptions={{ headerTitleAlign: "center" }}>
// Put all of your screens here <Stack.Screen ... />
</Stack.Navigator>
SaumyadipDev
  • 225
  • 2
  • 5
5

if you're using react-navigation v4

const stack = createStackNavigator({
  screen1: {screen: Screen},
},{
  defaultNavigationOptions: {
    headerTitleAlign: 'left | center',
  }
});

Docs:

https://reactnavigation.org/docs/en/stack-navigator.html#headertitlealign

or if you're using react-navigation v3

const stack = createStackNavigator({
  screen1: {screen: Screen},
},{
  headerLayoutPreset: 'left | center',
});

Docs:

https://reactnavigation.org/docs/en/3.x/stack-navigator.html

BlaShadow
  • 11,075
  • 7
  • 39
  • 60
5

I am using react navigation with below dev dependencies, with alignSelf and textAlign I was getting warnings enter image description here

  "dependencies": {
"@react-native-community/masked-view": "^0.1.6",
"@react-navigation/native": "^5.0.0",
"@react-navigation/stack": "^5.0.0",
"base64-js": "^1.3.1",
"lodash": "^4.17.15",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-base64": "0.0.2",
"react-native-ble-plx": "^1.1.1",
"react-native-gesture-handler": "^1.5.6",
"react-native-reanimated": "^1.7.0",
"react-native-safe-area-context": "^0.7.2",
"react-native-screens": "^2.0.0-beta.2"

},

none of above option worked for me, then I tried property headerTitleAlign: 'centre' and it worked.

below is my component code

const App = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator
            <Stack.Screen
              name="Home"
              component={HomeScreen}
              options={{
                title: 'Track Down',
                headerTitleAlign: 'center',
              }}
            />

          </Stack.Navigator>
        </NavigationContainer>
      );
    };
Rajnikant
  • 2,176
  • 24
  • 23
5

While I was facing the same thing but the solution is pretty easy. I just added one line of code headerTitleAlign: 'center',

function HomeNavigator() {
  return (
    <TabOneStack.Navigator>
      <TabOneStack.Screen
        name="HomeScreen"
        component={TabOneScreen}
        options={{
          headerRightContainerStyle: {
            marginRight: 20,
          },
          headerTitleAlign: 'center',     <-------- here
          headerTitle: () => (
            <Ionicons
              name={'logo-twitter'}
              color={Colors.light.tint}
              size={30}
            />
          ),
        }}
      />
    </TabOneStack.Navigator>
  )
}
David Leuliette
  • 1,595
  • 18
  • 26
kaybrian
  • 61
  • 1
  • 6
4
headerTitleStyle: {
    color: 'white',
    textAlign: 'center',
               flex: 1
}
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
4

for @react-navigation/native@next, I can confirm { headerTitleAlign: 'center' } works for me on android. Example below:

<Stack.Navigator screenOptions={{ headerTitleAlign: 'center' }}>
    <Stack.Screen name="Promo" component={PromoScreen} />
    <Stack.Screen name="Settings" component={SettingsNavigator} />
</Stack.Navigator>
Mazin Luriahk
  • 828
  • 8
  • 9
3

Set react-navigation header title in the center. Using the headerTitleStyle CSS.

static navigationOptions = {
        title: 'Home',
        headerTintColor: '#fff',
        headerTitleStyle: {
            width: '90%',
            textAlign: 'center',
        },
    };
Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Asif vora
  • 3,163
  • 3
  • 15
  • 31
3
navigationOptions:({navigation}) =>({
                    gesturesEnabled :false,

                    headerTitleStyle : {
                            color:"white",
                            fontFamily:"OpenSans",
                            alignSelf: 'center' ,
                            textAlign: 'center',
                            flex:1
                    },
                  }),

here . => {flex:1 ,textAlign: 'center' and alignSelf: 'center'} works for me!

Akshita Agarwal
  • 235
  • 3
  • 4
3

You should add headerLayoutPreset: 'center' to your createeStackNavigator function.

This is the one true way:

const someStack = createStackNavigator({
ConfigurationScreen: ConfigurationScreen,
PreferencesScreen: PreferencesScreen},
{ headerLayoutPreset: 'center' });

Reference: https://github.com/react-navigation/react-navigation/pull/4588

Daniel S.
  • 730
  • 6
  • 14
3

On React Navigation v5 the only way I managed to center a View was to use it like this:

 <MainStack.Navigator
  screenOptions={{
    headerTitleAlign: 'center',
  }}>

 ...

  <MainStack.Screen
    ...
    options={({navigation, route}) => ({
      headerTitle: () => <ViewButton />,
    ...
    })}
Dihan
  • 229
  • 3
  • 7
3

In the year 2020, if anyone had a problem as I did, below piece of snippet worked for me.

<Stack.Screen options={({ route, navigation }) => ({
          title: "Register",
          headerTitleAlign: "center")
        })}
 />
David Buck
  • 3,752
  • 35
  • 31
  • 35
Vibin
  • 155
  • 1
  • 4
3

As per version 5.x of ReactNavigation, you can use option header attribute headerTitleAlign with value center. Here is example of the code:

<Stack.Screen name="ScreenRegister" component={ScreenRegister}
   options={{ 
       headerTitle: props => <LogoHeader {...props} />,
       headerTitleAlign: 'center'
    }} 
/>
azwar_akbar
  • 1,451
  • 18
  • 27
2

Use headerTitleContainerStyle

static navigationOptions = {
  headerTitleStyle: { justifyContent: 'center' },
}
2

This works for me on Android & iOS:

static navigationOptions = {
    headerTitleStyle: {
        textAlign: 'center',
        flexGrow: 1,
    },
    headerRight: (<View/>)
};
Dodi
  • 2,201
  • 4
  • 30
  • 39
2

As of 30th May 2020, you can't no more pass any parameters to createStackNavigator().

To center your title, you have to use the following (with the headerTitleAlign property):

<Stack.Screen
    name="Centered"
    component={Centered}
    options={{
      title: 'Centered title',
      headerShown: true,
      headerTitleAlign:'center'
    }}
/>
cocool97
  • 1,201
  • 1
  • 10
  • 22
1

This will definately work for android

      headerTitleStyle:{
         flex: 1, textAlign: 'center'
      },
M.Hassam Yahya
  • 382
  • 2
  • 7
1

headerTitleStyle: { color: 'white', textAlign: 'center', flex: 1 }

  • This just repeat existing answers. New answers should add new information and new insights into the topic. – AdrianHHH Oct 03 '19 at 12:10
1

add headerTitleAlign: 'center' in navigationOptions

example:

  static navigationOptions = ({navigation}) => ({
    headerTitle: (
      <Image
        style={{width: 100, height: 100}}
        resizeMode="contain"
        source={icons.logo}
      />
    ),
    headerTitleAlign: 'center',
  });
samad324
  • 199
  • 6
1

In react navigation V5

<AuthStack.Screen
  name="SignupScreen"
  component={SignupScreen}
  options={{ title: "Sign Up", animationEnabled: false, headerTitleAlign: 'center', }}
/>
M Mahmud Hasan
  • 1,303
  • 1
  • 13
  • 20
1

If the header has more than 1 item ie left , right , center as below :

  <TabOneStack.Screen
        name="HomeScreen"
        component={TabOneScreen}
        options={{
          headerLeftContainerStyle: { marginLeft: 10 },
          headerRightContainerStyle: { marginRight: 10 },
          headerTitleContainerStyle: { backgroundColor: "yellow", alignItems: "center" },
          headerLeft: () => <ProfilePicture image="https://pbs.twimg.com/profile_images/1383042648550739968/fS6W0TvY_200x200.jpg" size={40} />,
          headerTitle: () => (<Ionicons name="logo-amazon" size={30}  />),
          headerRight: () => (<MaterialCommunityIcons name="star-four-points-outline" size={30} />)
        }}
      />

then adding alignItems:center to headerTitleContainerStyle will center the title component enter image description here

Bhupendra
  • 1,196
  • 16
  • 39
1

just use options property additionally,

<Stack.Screen
      component={HomeScreen}
      name="Skin Cancer Prediction"
      options={{
          headerTitleAlign: "center",
      }}
/>

you're good to go

naimur978
  • 144
  • 8
0

headerTitleStyle :{textAlign: 'center', flex: 1}

worked for me

Hiten Sharma
  • 117
  • 1
  • 5
  • Please consider providing a little more context, and possibly an explanation about _why_ the code works. – ZGski Dec 19 '19 at 15:30
0
 headerTitleAlign: {
        alignItems: 'center',
        justifyContent: 'center'
      },
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • Thank you for this answer! To help other understand your solution, can you provide a quick description of why this code works? – RedBassett Feb 12 '20 at 19:14
0

Working with React Navigation v5 you can also use the following option:

headerTitleAlign:'center'

Like in the below example where I wanted to perfectly center the title.

<Stack.Screen
    name="Settings"
    component={SettingsScreen}
    options={{
      title: 'SMS Recipient Number',
      headerShown: true,
      headerTitleAlign:'center'
    }}
/>
Tadiwanashe
  • 1,246
  • 14
  • 15
0

Simply this works for me with latest version 16.9.0,

defaultNavigationOptions : {title: 'App', headerTitleAlign: 'center' }
Thiru
  • 33
  • 7
0

we can achieve this with

headerTitleStyle :{ alignSelf:"center" };

Acash
  • 9
  • 1
-3

Make sure to check the issues before resulting to stack overflow, normally more helpful.issue regarding your problem But as himanshu said in comments you need to access the title style property to adjust the title how you want.

static navigationOptions = {
    header: (navigation) => ({
      title: <Text style={{ 
            color: 'rgba(0, 0, 0, .9)', 
            fontWeight: Platform.OS === 'ios' ? '600' : '500',  
            fontSize: Platform.OS === 'ios' ? 17 : 18, 
            alignSelf: 'center' 
         }}>Filters</Text>,
      right: <SearchButton />,
      left: <CancelButton />,
    })
  };

As shown in the issue, i presume you've already managed to find a solution as this was a while ago. But for anyone else coming across this issue it may be helpful.

David Schumann
  • 13,380
  • 9
  • 75
  • 96
TomTom
  • 899
  • 8
  • 16
-4

You can set the header title center for android in stack navigator of react navigation using this file change:

node_modules\react-navigation\src\views\Header.js

Change This Code In Header.js file:-

title: {
bottom: 0,
left: TITLE_OFFSET,
right: TITLE_OFFSET,
top: 0,
position: 'absolute',
alignItems: Platform.OS === 'ios' ? 'center' : 'center',
},
David Schumann
  • 13,380
  • 9
  • 75
  • 96
Vishal Dhaduk
  • 492
  • 5
  • 13