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.
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.
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
Wrap_Content View Inside a Constraintlayout Stretches Outside the Screen
Disable Edittext Blinking Cursor
Android Background Image Memory Usage
Android Intent for Playing Video
Appcompatactivity.Oncreate Can Only Be Called from Within the Same Library Group
Answer Incoming Call Using Android.Telecom and Incallservice
How Does the Getview() Method Work When Creating Your Own Custom Adapter
Handle Button Click Inside a Row in Recyclerview
Get All Messages from Whatsapp
How to Create Colorstatelist Programmatically
Difference Between APK (.Apk) and App Bundle (.Aab)
Lazy Load Images on Listview in Android(Beginner Level)
Simulate Low Battery & Low Memory in Android
How to Use the Same Keystore File to Sign Two Different Applications
Login Failed Invalid Key Error with Facebook Sdk
Passing String Array Between Two Class in Android Application
How to Change the Color of a Switchcompat from Appcompat Library