Tuesday, September 22, 2015

android - How to display custom view in ActionBar

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="#fcfff7"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply ActionBar Custom View"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>
actionbar_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/tv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:fontFamily="sans-serif-condensed"
    android:textSize="17dp"
    android:textStyle="italic"
    android:shadowRadius="25"
    android:shadowDx="15"
    android:shadowDy="15"
    android:shadowColor="#fdf8ff"
    />
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.app.ActionBar;
import android.widget.TextView;
import android.view.LayoutInflater;
import java.util.Random;


public class MainActivity extends Activity {
    public static Random rand = new Random();

    @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();

                // Get the custom layout
                LayoutInflater inflater = getLayoutInflater();
                View customView = inflater.inflate(R.layout.actionbar_custom_view,null);

                // Get the TextView reference from custom layout
                TextView tv = (TextView)customView.findViewById(R.id.tv);

                //tv.setText(ab.getTitle());
                // Set the ActionBar title text
                tv.setText("Android Example : ActionBar Custom View");

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

                /*
                    public abstract void setCustomView (View view)
                        Set the action bar into custom navigation mode, supplying a view
                        for custom navigation. Custom navigation views appear between
                        the application icon and any action buttons and
                        may use any space available there.

                        Parameters
                            view Custom navigation view to place in the ActionBar.
                */
                // Set the ActionBar custom layout
                ab.setCustomView(customView);

                /*
                    public abstract void setBackgroundDrawable (Drawable d)
                        Set the ActionBar's background. This will be used for the primary action bar.

                        Parameters
                            d Background drawable
                */
                // Set a dynamic gradient background for ActionBar
                ab.setBackgroundDrawable(createHomeDrawable());
           }
        });
    }

    // Method to generate a gradient drawable
    public static Drawable createHomeDrawable(){
        GradientDrawable gradientD = new GradientDrawable();
        //Set the shape type
        gradientD.setShape(GradientDrawable.RECTANGLE);
        //Set the gradient colors
        gradientD.setColors(new int[]{getRandomColor(),
                getRandomColor(),
                getRandomColor(),
                getRandomColor(),
                getRandomColor(),
                getRandomColor(),
                getRandomColor()
        });

        //Set the gradient type (i.e linear)
        gradientD.setGradientType(GradientDrawable.SWEEP_GRADIENT);

        //Set stroke width and color
        gradientD.setStroke(2,getRandomColor());

        //set gradient orientation (i.e top to bottom)
        gradientD.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);

        //Set a random value for gradient radius
        float gradientRadius = rand.nextFloat() *(360-0)+0;
        gradientD.setGradientRadius(gradientRadius);
        return gradientD;
    }

    // Method to generate a random color
    public static int getRandomColor(){
        //Random rand = new Random();
        int max = 255;
        int min = 0;
        int intR = rand.nextInt() * (max - min) + min;
        int intG = rand.nextInt() * (max - min) + min;
        int intB = rand.nextInt() * (max - min) + min;
        int color = Color.argb(255, intR, intG, intB);
        return  color;
    }
}
More android examples