Tuesday, September 22, 2015

android - Change ActionBar title position, color, size, font, style programmatically

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#d0d1be"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change ActionBar Title Style"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.ActionBar;
import android.widget.TextView;


public class MainActivity extends Activity {


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

        // Get the widgets reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the ActionBar
                ActionBar ab = getActionBar();

                // Set the ActionBar background color
                ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF4500")));

                // Create a TextView programmatically.
                TextView tv = new TextView(getApplicationContext());

                // Create a LayoutParams for TextView
                LayoutParams lp = new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, // Width of TextView
                        LayoutParams.WRAP_CONTENT); // Height of TextView

                // Apply the layout parameters to TextView widget
                tv.setLayoutParams(lp);

                // Set text to display in TextView
                // This will set the ActionBar title text
                tv.setText("Android Example : ActionBar Title Style");

                // Set the text color of TextView
                // This will change the ActionBar title text color
                tv.setTextColor(Color.parseColor("#FFF5EE"));

                // Center align the ActionBar title
                tv.setGravity(Gravity.CENTER);

                // Set the serif font for TextView text
                // This will change ActionBar title text font
                tv.setTypeface(Typeface.SERIF, Typeface.ITALIC);

                // Underline the ActionBar title text
                tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

                // Set the ActionBar title font size
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);

                // Display a shadow around ActionBar title text
                tv.setShadowLayer(
                        1.f, // radius
                        2.0f, // dx
                        2.0f, // dy
                        Color.parseColor("#FF8C00") // shadow color
                );

                // Set the ActionBar display option
                ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

                // Finally, set the newly created TextView as ActionBar custom view
                ab.setCustomView(tv);
            }
        });
    }
}
More android examples