By default the Title text in the Android Toolbar is right-aligned, If you want to change it to center, you can do so by creating your own custom ToolBar,
custom_action_bar.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/custom_action_bar"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Title in Center"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
Now in your Activity class, in onCreate() method add the below lines to use your own custom Toolbar.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar customActionBar = getSupportActionBar();
customActionBar.setDisplayShowCustomEnabled(true);
customActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
customActionBar.setCustomView(R.layout.custom_action_bar);
setContentView(R.layout.activity_main);
}
}
Output:
Have Questions? Post them here!
More Posts related to android,
- appcompat_v7 errors after updates to API level 21 Material Theme
- adb: The Android Debug Bridge and Commands
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- How to make TextView Text Transparent [Android]
- Android is starting optimizing... app 1 of 1
- How to make Text in TextView bold and italic in Android
- Android Development - How to switch between two Activities
- Android : IOException: Unable to open sync connection!
- Android Toolbar example with appcompat_v7 21
- How to Change Android Title Bar Color?
- How to Add Padding to Android TextView
- JavaScript : redirect page to other url
- Programmatically Send an Email from Android App using Intent
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- [Soluiton] You already have the latest version of Android Studio installed
- Detect swipes on Android Activity
- Add Buttons at the bottom of Android Layout xml file
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Hide Navigation Bar from Android Screen Activity
- Android Disable EditText from Auto Focus on Activity load
- Toast not getting displayed Android App
- How to Enable Developers Option in Android Phones Settings
- Android : Duplicate registration for activity com.example.abc
- Android ListView turns Black or Flickers while Scrolling
- Android Studio Ctrl Shift o auto import not working
More Posts:
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException - Java
- Add or remove users from sudo group - Ubuntu - Ubuntu
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- Python: If Else Statements in One Single Line - Python
- How to copy file name and path to clipboard in Notepad++ - NotepadPlusPlus
- Create Custom Toast Android Programming - Android
- Fix: Make makefile:31 Error 127 (GCC) - Ubuntu
- How to Create a Git Branch in 6 Different ways - Git