How to create action bar that shows underline title text inside it.
In this tutorial we are creating and modifying a action bar and setting up textview inside it. Now we are converting the textview text to underline using setPaintFlags(Paint.UNDERLINE_TEXT_FLAG) method. This method will underline the textview text and when we set the textview inside action bar then it will simply replace the action bar default text. So here is the complete step by step tutorial for Set Underline on ActionBar title text in android programmatically.
Note: If you are facing any type of NullPointerException error then ready my this post to solve this error.
How to Set Underline on ActionBar title text in android programmatically.
Code for MainActivity.java file.
package com.underlineactionbartitletext_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.RED); textview.setGravity(Gravity.CENTER); textview.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); 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.underlineactionbartitletext_android_examples.com.MainActivity" > </RelativeLayout>
Screenshot: