Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to get gradient effect on setBackgroundColor

Is there a way to get a gradient effect or a semi-transparant effect when setting the background color of a view? For example, this code:

selView.setBackgroundColor(Color.rgb(240, 128, 128)); // light red

highlights a selected view in a list view. Either a gradient or a background would be cool.

like image 742
Jack BeNimble Avatar asked Nov 30 '22 16:11

Jack BeNimble


2 Answers

You would first need to create a gradient in xml.

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <gradient 
  android:startColor="#FFFF00FF" 
  android:endColor="#FFFFFFFF" 
  android:angle="270"/> 
</shape>

You should add this to one of your drawables.xml. You should then be able to apply this to your ListView in xml.

android:background=@drawable/yourdrawable

or

setBackgroundResource(R.drawable.yourdrawable);
like image 62
nicholas.hauschild Avatar answered Dec 10 '22 12:12

nicholas.hauschild


i will give you a sample code

  <item android:state_pressed="true" >
    <shape>
    <gradient
    android:startColor="#ff5500"
    android:endColor="#999999"
    android:angle="270" />
    <stroke
    android:width="3dp"
    android:color="#999999" />
    <corners
    android:radius="3dp" />
    <padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
    </shape>
    </item>

    <item android:state_focused="true" >
    <shape>
    <gradient
    android:endColor="#ff5500"
    android:startColor="#999999"
    android:angle="270" />
    <stroke
    android:width="3dp"
    android:color="#999999" />
    <corners
    android:radius="3dp" />
    <padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
    </shape>
    </item>

    <item> 
    <shape 
    android:shape="rectangle">

    <gradient android:type="radial" android:gradientRadius="50"
    android:startColor="#999999" android:endColor="#000000" />
    <!-- <corners-->
    <!-- android:radius="10dp" />-->
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
    android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
    </shape>
    </item>
    </selector>

apply this to your view from drawable

like image 44
Sreedev Avatar answered Dec 10 '22 11:12

Sreedev