27

I have the following code:

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="line">
   <stroke android:width="1dp"/>
   <size android:height="1dp" />
   <solid  android:color="#FFF"/>
   </shape>


   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background ="@drawable/line"/>

I have two questions:

  1. Why the line is black instead white? I have tried putting it inside a ImageView but the result is the same.
  2. How can i set the opacity of the shape?
Janusz
  • 187,060
  • 113
  • 301
  • 369
Gerardo
  • 5,800
  • 11
  • 66
  • 94

3 Answers3

29

1) In order to set the color of the line, you need to define android:color on the <stroke>. The <solid> element is for the background.

2) You can set the opacity of the shape by using color values with an alpha layer, aka #ARGB. In your case, maybe #7FFF.

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
22

The simplest example of the line for 2 density pixels with black color. Just save the xml in drawable folder: res/drawable/shape_black_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke 
    android:width="1dp" 
    android:color="#000000" />
<size android:height="2dp" />
</shape>

Use LinearLayout and background to show the line.

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="@drawable/divider">
</LinearLayout>

I hope this helped.

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
0

A tricky way can be like this:

1) Add a View where you want have a line.

2) set android:background to a hex value that you can set both color and opacity.

For example:

<View
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:background="#77f27123"
    android:orientation="vertical" />
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36