8

I have been searching for possibilities to define different shapes inside a single shapes.xml and refer to each one on some specific events.

At last I've found a solution to my question. And the answer is using level-list.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
          
        <gradient android:startColor="#aaa" 
            android:endColor="#eee" android:angle="270" />
           
        <corners android:bottomRightRadius="7dp"
            android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    </shape>

</item>
<item android:maxLevel="1">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
          
        <gradient android:startColor="#eee" android:centerColor="#ddd"
            android:endColor="#00fff2" android:angle="270" />
           
        <corners android:bottomRightRadius="7dp"
            android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    </shape>
</item>
</level-list>

Apply this to the background attribute in the style. the Interchanging of differents shapes can be achieved by setting the level to that element.

Eg: findViewById(R.id.mybutton).getBackground().setLevel(1);

In the above code I'm setting the second shape to the button with id mybutton.

Ash
  • 1,701
  • 13
  • 18
Dinash
  • 3,027
  • 4
  • 32
  • 45

1 Answers1

-1
<?xml version="1.0" encoding="UTF-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dip" android:color="#FF8000" />
    <solid 
        android:color="#00FFFFFF"
        android:paddingLeft="10dip"
        android:paddingTop="10dip"/>
    <corners android:radius="10px"/>

    <padding 
        android:left="10dip" 
        android:top="10dip" 
        android:right="10dip" 
        android:bottom="10dip" /> 
</shape>

you can use this for Boarder and any shape ..its for reference...'' If it is useful so accept the answer and vote up the answer

sstn
  • 3,050
  • 19
  • 32
Hardik Gajjar
  • 5,038
  • 8
  • 33
  • 51
  • Can you explain how to use the above one. – Dinash Apr 18 '11 at 11:40
  • Hi...you can put this file as " borderframe.xml " in Drawable folder and you can call this by android:background="@drawable/borderframe" – Hardik Gajjar Apr 19 '11 at 05:59
  • 1
    That right Hardik i have already tried it. But still i wanted to have multiple shape definition in an single file and want to change to that shape on some events. For that i went through level-list and have also posted the sample in my question itself. Thanks... – Dinash Apr 19 '11 at 06:08
  • What question is this answer for? Does it solve the problem? – Andrew Sep 03 '15 at 07:23