How to Programmatically Set the Background Color Gradient on a Custom Title Bar

How do I programmatically set the background color gradient on a Custom Title Bar?

To do this in code, you create a GradientDrawable.

The only chance to set the angle and color is in the constructor.
If you want to change the color or angle, just create a new GradientDrawable and set it as the background

    View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

For this to work, I added an id to your main LinearLayout as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/title_bar_logo"
android:gravity="center_horizontal"
android:paddingTop="0dip"/>

</LinearLayout>

And to use this as for a custom title bar

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
View title = getWindow().findViewById(R.id.mainlayout);
title.setBackgroundDrawable(gd);

Android Gradient Application Title Bar

All people are trying to use Custom title for this requirement.

I achieved this without using custom title. Just use below code in your activity's onCreate() after setContentView().

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED,Color.GREEN});
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundDrawable(gd);

Enjoy with above code. Below image is screen shot of my app with gradient color as title bar background.

Gradient Color as Background of title bar

Setting a gradient background color for surfaceview

Found my answer in the form of paint's set shader, this works nicely for the moment

         Paint gradPaint = new Paint();
gradPaint.setShader(new LinearGradient(0,0,0,getHeight(),Color.BLUE,Color.CYAN,Shader.TileMode.CLAMP));
canvas.drawPaint(gradPaint);
//Where getHeight() is the height of the canvas

How to display gradient effect from top to bottom in android

make an xml file in your dawable folder name it like gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000"
android:endColor="#fff"
android:angle="90"
/>
</shape>

and set it as background to your View.

android:background="@drawable/gradient_bg"

or

setBackgroundResource(R.drawable.gradient_bg);

Gradient Background on Flutter AppBar

I don't believe you can pass a gradient to an AppBar as it expects a Color rather than a gradient.

You can, however, create your own widget that mimics an AppBar except by using a gradient.
Take a look at this example that I've pieced together from the Planets-Flutter tutorial along with the code below it.

enter image description here

import "package:flutter/material.dart";

class Page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(children : <Widget>[GradientAppBar("Custom Gradient App Bar"), Container()],);
}
}

class GradientAppBar extends StatelessWidget {

final String title;
final double barHeight = 50.0;

GradientAppBar(this.title);

@override
Widget build(BuildContext context) {
final double statusbarHeight = MediaQuery
.of(context)
.padding
.top;

return new Container(
padding: EdgeInsets.only(top: statusbarHeight),
height: statusbarHeight + barHeight,
child: Center(
child: Text(
title,
style: TextStyle(fontSize: 20.0, color: Colors.white, fontWeight: FontWeight.bold),
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.red, Colors.blue],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(0.5, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp
),
),
);
}
}

Hope this helps. Let me know if you have any questions.



Related Topics



Leave a reply



Submit