Riwaj Ghimire
Riwaj Ghimire

Reputation: 43

Gradient Background color in Java

I am working on android Project, I was using Gradient Background that I link to drawable but in one case I have background from Java Main Activity. I want to use same Background there too.

This is code I use in XML layout.

<gradient
    android:angle="135"
    android:centerColor="#16a085"
    android:endColor="#2980b9"
    android:startColor="#2980b9" />

I want to work in java with same gradient which is now like the screenshot below and its single color. How Can I Use Gradient in this place ?

Upvotes: 1

Views: 1676

Answers (1)

Abhi
Abhi

Reputation: 3511

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);

Upvotes: 3

Related Questions