Sairavi Kumar Sairavi Kumar - 2 months ago 22
Android Question

How to apply gradient for below kitkat devices in android

My app consists of a background color gradient, i have tested the app on both lollipop and kitkat devices, where i found gradient is working for above lollipop devices, but not working below lollipop devices.

Here is my gradient.xml look like

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="#4477C7"
android:centerY="0"
android:endColor="#303F9F"
android:gradientRadius="775dp"
android:startColor="#59C6FF"
android:type="radial" />
<corners android:radius="0dp" /></shape>


Here is my MainActivity look like

public class MainActivity extends AppCompatActivity {

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


Here is my activity_main look like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gradient"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:id="@+id/relativeLayout"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ravi.gradientexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello World!"
android:textColor="@android:color/white" />
</RelativeLayout>


And here is the output that i'm getting.

output in both lollipop and kitkat devices

Answer

I have done some modifications in your code,

public class MainActivity extends AppCompatActivity {

private RelativeLayout relativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    GradientDrawable gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[]{0xFF59C6FF, 0xFF4477C7, 0xFF303F9F}); // Gradient Color Codes
    gradientDrawable.setCornerRadius(0f);                   // Setting Corner Radius
    gradientDrawable.setGradientRadius(775f);               // Setting Graidnet Radius
    gradientDrawable.setGradientCenter(0.5f, 0);
    gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); // Gradient Type
    relativeLayout.setBackgroundDrawable(gradientDrawable);         //Setting Gradient To Layout
}}

Its working well in my kitkat device, here is the output enter image description here

Comments