Change action bar title text color in android programmatically

How to set action bar text color dynamically in android application.

Action bar is one of the most attached part of every android application because action bar shows at the top of every android app. So in this tutorial we are simply Change action bar title text color in android programmatically method by creating and setting TextView inside action bar.

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 Change action bar title text color in android programmatically.

Code for MainActivity.java file.

 package com.changeactionbartitlecolor_android_examples.com;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
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 {

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

 TextView textview = new TextView(MainActivity.this);

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

 textview.setLayoutParams(layoutparams);

 textview.setText("Action Bar Title Text");
 
 textview.setGravity(Gravity.CENTER);

 textview.setTextColor(Color.parseColor("#fc6902"));
 
 textview.setTextSize(25);
 

 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.changeactionbartitlecolor_android_examples.com.MainActivity" >

</RelativeLayout>

Screenshot:

Change action bar title text color in android programmaticallyClick here to download Change action bar title text color in android programmatically project with source code.

2 Comments

  1. This unfortunately stopped and fails for the latest version. Maybe missing more codes or files. The default new project runs but not the project with these codes.

  2. Please tell me what kind of error you are facing and I’ll make sure that your project runs perfectly.

Leave a Reply

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