23

I want to create vertical gradient separator in android

I am able to create for horizontal separator but in vertical gradient nothing appears

code for horizontal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
       <gradient       
                android:startColor="#DEDEDE"
                android:centerColor="#F52578"
                android:endColor=  "#DEDEDE"
      /> 
</shape>

this is how i use in my project

  <View 
            android:background="@drawable/divider_gradient"
            android:layout_width="fill_parent"
            android:layout_height="1.0px"

     />

i tried layout_width="1dp" and layout_height="fill_parent" for vertical separator but nothing appears

Hunt
  • 7,357
  • 26
  • 104
  • 238

2 Answers2

64

Use

android:angle="270"

as another attribute in your gradient

to get you start color - center color - end color as Top to Bottom

Rahul garg
  • 8,604
  • 5
  • 32
  • 67
  • garg i tried `android:startColor="#FFFFFF" android:centerColor="#F52578" android:endColor= "#FFFFFF" android:angle="270" ` but its not working – Hunt Mar 03 '12 at 19:06
  • the gradient you mentioned is correct,, how are you using it in the view... ` android:layout_height="1.0px"` not this right... that would make the height 1 px..and you won't see gradient... What is the current color of the view visible..? – Rahul garg Mar 03 '12 at 19:11
  • These are the attributes of view `android:background="@drawable/divider_gradient" android:layout_width="1dp" android:layout_height="fill_parent"` – Hunt Mar 03 '12 at 19:13
  • is view showing up at all..? try changing android:background="#FFF", and notice if the view is visible at all of complete white background.. if yes then you can change that to gradient.. otherwise problem is in you `view` position and size... – Rahul garg Mar 03 '12 at 19:21
  • yes, view is visible if i remove the gradient and pass the solid color , but when i change it to gradient it disappears – Hunt Mar 03 '12 at 19:24
  • quite strange!! just for debugging can you try to put `` and removing gradient at all from `divider_gradient` to see if the gradient is actually being applied on the view.. – Rahul garg Mar 03 '12 at 19:35
  • Yes a solid like appears , after i replace the gradient tag with a solid tag , n yes this seems pretty strange – Hunt Mar 03 '12 at 19:39
  • just for info..did you get it corrected?? and what was the defect/reason earlier? – Rahul garg Mar 05 '12 at 12:12
  • 1
    gard i tried your code only actually gradient wasn't visible in emulator , but was showing in a real device , so +1 for showing your interest – Hunt Mar 05 '12 at 15:33
  • Thanks it help,really easy answer – ashad Nov 20 '18 at 12:00
1

Make use of Angle attribute to achieve gradient in wanted direction. More at http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

the100rabh
  • 4,009
  • 4
  • 29
  • 39
  • what angle did u try working with ?? Also try changing that width to something like 4dp for trials first instead of 1.0px, and I would suggest u to use 1dp instead of 1px always – the100rabh Mar 04 '12 at 14:22