Android LinearLayout Gradient Background
Ok I have managed to solve this using a selector. See code below:
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/main_header_selector">
</LinearLayout>
main_header_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
Hopefully this helps someone who has the same problem.
How to make gradient background in android
You can create this 'half-gradient' look by using an xml Layer-List to combine the top and bottom 'bands' into one file. Each band is an xml shape.
See this previous answer on SO for a detailed tutorial: Multi-gradient shapes.
adding color gradient between 2 linear layouts
Try this:
Gradient drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#5b5cb8df"
android:centerColor="#c501a0e4"
android:endColor="#009FE3"
android:angle="270"/>
</shape>
In xml use like :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/company_bg"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/background_blur" />
</RelativeLayout>
How to make a gradient background for a LinearLayout?
Besides xml you can also use GradientDrawable it has corresponding methods for all xml attributes
Android linear layout with gradient transparency background
Since you are manipulating Bitmap
to generate the blurred image, you can use a LinearGradient
shader to add transparency to it as in this answer.
public Bitmap addGradient(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap overlay = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, 0, 0, h, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.REPEAT);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawRect(0, h - h, w, h, paint);
return overlay;
}
How to create android layout gradient background from middle to bottom of the screen?
Use android:centerColor
in your my_bg.xml
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);
Gradient shape blending position in LinearLayout
Try this:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffff"
android:centerColor="#ffffff"
android:endColor="#ff0000"
android:angle="270"
/>
</shape>
Related Topics
How to Use Std::Stoul and Std::Stoull in Android
How to Detect If Device Support Notch Display
How to Use Firebase Functions to Send Fcm to User
Couldn't Get Connection Factory Client - Fighting with Google Maps
Webview with Asynctask on Android
Push Notification Enable and Disable by Using Switch
How to Stack Firebase Cloud Messaging Notifications When the Application Is Not Running
How Is an Intent Service Declared in the Android Manifest
Android 4.0.1 Breaks Webview HTML 5 Local Storage
React Native Android Duplicate File Error When Generating APK
Getting Error "Gradle Dsl Method Not Found: 'Compile()'" When Syncing Build.Gradle
Set Rowspan or Colspan of a Child of a Gridlayout Programmatically
How to Load Image into Imageview from Url Using Glide V4.0.0Rc1
How to Create a Release Android Library Package (Aar) in Android Studio (Not Debug)
How to Make a Listview Transparent in Android
Read Logs from All Apps on Android from Within an App for Android 4.2+