How to Add Gradient Background in LinearLayout

In this android programming source code example, we going to explore how to add gradient background in LinearLayout.

Below is a step by step source code to add gradient background in LinearLayout.

activity_layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@drawable/background_gradient"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".layout.Layout1">

</LinearLayout>

drawable/background_gradient.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:endColor="#ff0012"
                android:startColor="#38068f"
                android:type="linear" />
        </shape>
    </item>
</selector>

Layout1.java

package com.inducesmile.inducesmile_button.layout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Layout1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout1);
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment