Create Action Bar with Gradient Shaded background in android

How to set gradient effect background inside Action Title Bar in android layout activity dynamically.

In this tutorial we are going to completely modify the action bar because we are using gradient shade as action title bar’s background. So here is the complete step by step tutorial for Create Action Bar with Gradient Shaded background in android.

android-project-download-code-button

How to Create Action Bar with Gradient Shaded background in android.

Code for MainActivity.java file.

 package com.actionbarwithgradient_android_examples.com;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;

public class MainActivity extends Activity {

 GradientDrawable gradientdrawable;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 //Calling Action Bar Creating method.
 CreateActionBarWithGradient();
 
 }
 
 //Creating action bar with gradient method.
 public void CreateActionBarWithGradient()
 {
 
 ActionBar actionbar = getActionBar();

 gradientdrawable = new GradientDrawable();
 
 gradientdrawable.setColors(new int[]{
 Color.parseColor("#e8fbfc"),
 Color.parseColor("#01ebfa")
 });

 gradientdrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
 
 gradientdrawable.setShape(GradientDrawable.RECTANGLE);

 actionbar.setBackgroundDrawable(gradientdrawable);
 
 }
 

}

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

</RelativeLayout>

Screenshot:

Create Action Bar with Gradient Shaded background in android

Click here to download Create Action Bar with Gradient Shaded background in android project with source code.

Leave a Reply

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