Android toolbar center title and custom font

Android toolbar center title and custom font

To center the title of the toolbar and apply a custom font to it in Android, you can achieve this by creating a custom layout for the toolbar and then setting it as the custom view of the toolbar. Here's how you can do it:

  1. Create a custom layout for the toolbar: You'll define a layout XML file where you can place the title TextView and apply your custom font.

  2. Set the custom layout to the toolbar: In your activity or fragment, you'll inflate the custom layout and set it as the custom view of the toolbar.

Here's an example:

  1. Create a layout file toolbar_custom_layout.xml for your custom toolbar layout:
<!-- toolbar_custom_layout.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@color/colorPrimary" android:layout_centerInParent="true"/> </RelativeLayout> 
  1. In your activity or fragment, inflate this layout and set it as the custom view of the toolbar:
import android.graphics.Typeface; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); // Inflate the custom layout for the toolbar TextView toolbarTitle = findViewById(R.id.toolbar_title); toolbarTitle.setText("Centered Title"); // Apply custom font to the toolbar title Typeface customFont = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf"); toolbarTitle.setTypeface(customFont); setSupportActionBar(toolbar); } } 
  1. Make sure to apply your custom font to the TextView toolbar_title as shown in the code above. Replace "your_custom_font.ttf" with the path to your custom font file in the assets folder.

  2. In your activity's XML layout file (activity_main.xml), include the toolbar:

<!-- activity_main.xml --> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/AppTheme.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> <!-- Your content goes here --> </androidx.coordinatorlayout.widget.CoordinatorLayout> 

This way, you can have a centered title with a custom font in your toolbar. Adjust the layout and styles according to your app's design.

Examples

  1. How to center title and apply custom font in Android Toolbar? Description: Learn how to center the title text and apply a custom font to the title in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center" app:titleTextColor="@color/colorPrimary" app:title="@string/app_name" /> 
  2. Android Toolbar center title and change font example Description: See an example of how to center the title and change its font in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  3. Android Toolbar title center and custom font tutorial Description: Follow a tutorial on how to center the title and set a custom font for the Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  4. Center title and apply custom font to Android Toolbar Description: Discover how to center the title and set a custom font for the text in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  5. How to set custom font for center title in Android Toolbar? Description: Learn the steps to set a custom font for the center title text in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  6. Android Toolbar center title and custom font implementation Description: Implement centering the title text and applying a custom font to an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  7. Tutorial: Center title and change font in Android Toolbar Description: Step-by-step guide on centering the title and applying a custom font to the text in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  8. Android Toolbar center title and change font code snippet Description: Get a code snippet demonstrating how to center the title and change its font in an Android Toolbar.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 
  9. How to customize Toolbar title font and center it in Android? Description: Learn how to customize the font of the Toolbar title and center it for better aesthetics in an Android app.

    <!-- layout/toolbar_layout.xml --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/colorPrimary" android:textSize="20sp" android:fontFamily="@font/custom_font" android:layout_gravity="center"/> </androidx.appcompat.widget.Toolbar> 

More Tags

automapper webview django-forms resnet array-column latitude-longitude uploadify optionmenu google-admin-sdk parse-platform

More Programming Questions

More Mortgage and Real Estate Calculators

More Fitness Calculators

More Transportation Calculators

More Stoichiometry Calculators