Gradient dividers in Android

Many of Android’s screens use attractive dividers with gradients that fade from black to white to black.  Here’s how to create one of your own.

1. Create a file called “black_white_gradient.xml” in /res/drawable. Paste this into it.

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

2. Create a view in the target layout and apply the gradient as the background.  The “black_white_gradient” in “@drawable/black_white_gradient” refers to the file name of the shape above.

<View android:id="@+id/divider"
    android:background="@drawable/black_white_gradient"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@id/someOtherThing" />

  1. amina says:

    slt,
    dans mon code tout les ‘R’ sont en rouge je sais pas comment les corriger ;svp comment je peux les corriger?? cemmecet ‘R’ svp
    inflater.inflate(R.menu.mainmenu, menu);

  2. Love android says:

    Its awesome, thanks for sharing.. it works very good in my emulator and phone.. really helpful.. thanks lot

  3. Love android says:

    Thanks for sharing, awesome.. 🙂

  4. Sid says:

    i liked it very much

  5. itzco says:

    Great info thanks!

    An extra note, I tried to use as a divider for a listview but doesn’t show up well (it mirrors)
    To use for a listview just change the drawable’s gradient section to:

    (Removing centerColor)

  6. PV says:

    we cant add into an widget, so facing problem. My widget is having three , so I want to add an divider after 1st & 2nd button. pls help me

    • PV says:

      Ignore first….
      we cant add a View into an widget, so facing problem. My widget is having three image buttons , so I want to add an divider after 1st & 2nd button. pls help me

  7. Anonymous says:

    how can u make a xml file in a drwable its funny !! ?

  8. sWozzie says:

    Awesome!

  9. punzz says:

    Thx for the info, really help me out

  10. C says:

    Thanks man, simple, quick, and it works 🙂

  11. Kevin says:

    Will this also work for a LinearLayout?

  12. Stephan Wiesner says:

    Cool. Simple and easy – and nice 🙂

    Stephan

  13. Ross says:

    this is cool ! instead of adding it to a regular View, add the background and layout_height attributes to a regular text view, change the angle on the gradient to 90 and you can fade vertical with text behind it – and for added style, add the gravity attribute (android:gravity=”center_vertical|center_horizontal”) to center the text.

  14. Matt Langston says:

    In your UI layout XML.

    In your drawable folder. Name is list_divider.xml.

  15. Binary Sheep says:

    You can also just add it to a ListView as a divider

  16. jetti says:

    Hi, i want the more information on the above given example.If anyone know please respond.
    Thanx in advance

  17. Michael says:

    Fantastic. Exactly what I’ve been looking for, I was about to create an image by hand. Thanks.

  18. MrSqueezles says:

    layout_below puts the divider below some other component. someOtherThing is the ID of whatever component underneath which the divider will appear. You’ll probably want to insert a little space between the two components.

  19. Sikus says:

    Hello, what is “someOtherThing” in layout_below?

Leave a Reply