Changing gradient background colors on Android at runtime
Yes! Found a way!
Had to forget about XML, but here's how I did it:
On my getView() overloaded function (ListAdapter) I just had to:
int h = v.getHeight();
ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, h, Color.parseColor("#330000FF"), Color.parseColor("#110000FF"), Shader.TileMode.REPEAT));
v.setBackgroundDrawable(mDrawable);
And that gave me the same result as the XML background above. Now I can programmatically set the background color.
Change gradient center dynamically
You can achieve your goal by using Shader
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(width/2, 0, width/2, height,
new int[]{0xff000000, 0xff42f448, 0xff000000},
new float[]{0, 0.3f, 1}, // start, center and end position
Shader.TileMode.CLAMP);
}
};
PaintDrawable pd = new PaintDrawable();
pd.setShape(new RectShape());
pd.setShaderFactory(sf);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(pd);
} else
layout.setBackgroundDrawable(pd);
Set the background colour of a Layout view to a gradient in Android?
create gradient.xml
in /res/drawable
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:endColor="#00000000"
android:angle="45"/>
</shape>
and in your main.xml
layout file in /res/layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient"
>
</LinearLayout>
you can specify the angle by replacing the android:angle
value and start/end colour by replacing android:startColor
and android:endColor
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);
Change background colour dynamically
Try creating three View
s underneath the RangeSeekBar
, and updating the widths of each as the sliders are moved.
Very roughly, this might look like:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<View
android:id="@+id/green"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#00FF00"/>
<View
android:id="@+id/orange"
android:layout_toRightOf="@id/green"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FFFF00"/>
<View
android:id="@+id/red"
android:layout_toRightOf="@id/orange"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FF0000"/>
<RangeSeekBar android:layout_width="300dp" android:layout_height="wrap_content">
</RelativeLayout>
In this example, you would update the widths of the three colour views so that they add up to 300dp, as the sliders are moved.
You can either use solid colours for the backgrounds of the three views, which would look like your second image, or create three gradients (green to green-yellow, yellow-orange, orange-red).
Gradient Background color in Java
Save the Gradient as an XML in your drawable.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="135"
android:centerColor="#16a085"
android:endColor="#2980b9"
android:startColor="#2980b9" />
</shape>
Replace
ab.setBackgroundDrawable (new ColorDrawable(Color.parseColor(#2c3e50')));
with
ab.setBackgroundResource(R.drawable.color_gradient);
Related Topics
How to Force a Cache Clearing Using Universal Image Loader Android
How to Detect Bottom Soft Navigation Bar Available in Android Programmatically
Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating
How to Draw Dashed Polyline with Android Google Map Sdk V2
Android Imageview's Onclicklistener Does Not Work
How to Search for a Value in Firebase Android
How to Store Large Blobs in an Android Content Provider
Android Can't Record Video with Front Facing Camera, Mediarecorder Start Failed: -19
Retrofit2: Modifying Request Body in Okhttp Interceptor
Saving Arraylists in SQLite Databases
Custom 'Keyboard' Built in an Application on Android
Error Inflating Class Collapsingtoolbarlayout
Practical Way to Find Out If Sms Has Been Sent
Dagger 2 Injecting Android Application Context
Android:How to Change Playback Rate of Music Using Opensl Es