Set/Change Action Bar title text size in android programmatically

How to increase-decrease action title bar title text font size dynamically in android.

In this tutorial we are simply changing the action bar title text font size using .setTextSize() method. Because we are setting up TextView as action bar title text so we can modify it as per user requirement. So here is the complete step by step tutorial for Set/Change Action Bar title text size in android programmatically.

android-project-download-code-button

Note: If you are facing any type of NullPointerException error then ready my this post to solve this error.

How to Set/Change Action Bar title text size in android programmatically.

Code for MainActivity.java file.

 package com.changeactionbartitletextsize_android_examples.com;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ActionBar actionbar = getActionBar();

 TextView textview = new TextView(MainActivity.this);

 LayoutParams layoutparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 
 textview.setLayoutParams(layoutparams);

 textview.setText("ActionBar Title");

 textview.setTextColor(Color.MAGENTA);
 
 textview.setGravity(Gravity.CENTER);

 textview.setTextSize(30);

 actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 actionbar.setCustomView(textview);
 
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.changeactionbartitletextsize_android_examples.com.MainActivity" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_world" />

</RelativeLayout>

Screenshot:

Set/Change Action Bar title text size in android programmatically

Click here to download Set/Change Action Bar title text size in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *