81

I'm making an activity to configure my app, and I have to divide the sections of my configuration window with a line. I used this: divider_horizontal_bright, from this example:

http://android.cryx.li/doku.php?id=know:settings:start

However it doesn't work! When I test on my android phone, it doesn't show a horizontal line. Why?

I am using Android 2.1

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

5 Answers5

144

Try this link.... horizontal rule

That should do the trick.

The code below is xml.

<View
    android:layout_width="fill_parent"
    android:layout_height="2dip"
    android:background="#FF00FF00" />
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
prolink007
  • 33,872
  • 24
  • 117
  • 185
137

If this didn't work:

  <ImageView
    android:layout_gravity="center_horizontal"
    android:paddingTop="10px"
    android:paddingBottom="5px"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:src="@android:drawable/divider_horizontal_bright" />

Try this raw View:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#000000" />
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 9
    +1 for providing the complete View example, not just the attributes, and for the ImageView example using Android's own divider drawable. The ImageView seems preferable since it uses the style provided by the system/theme. – spaaarky21 Jun 04 '13 at 15:38
  • Great solution for providing both. The image Version did not display the 'bar', so I went the the View version. – TheIcemanCometh May 10 '15 at 13:52
  • 2
    Great answer! Just a note: one probably needs to add `android:scaleType="fitXY"` to make the ImageView solution work (maybe is this only needed with new Android versions?) – Alberto M May 27 '15 at 10:34
  • 1
    You should replace `padding` with `margin`, otherwise neither of these will work. – Ali Bdeir Aug 01 '16 at 07:24
7

For only one line, you need

...
<View android:id="@+id/primerdivisor"
android:layout_height="2dp"
android:layout_width="fill_parent"
android:background="#ffffff" /> 
...
gangadhars
  • 2,584
  • 7
  • 41
  • 68
4

How about defining your own view? I have used the class below, using a LinearLayout around a view whose background color is set. This allows me to pre-define layout parameters for it. If you don't need that just extend View and set the background color instead.

public class HorizontalRulerView extends LinearLayout {

    static final int COLOR = Color.DKGRAY;
    static final int HEIGHT = 2;
    static final int VERTICAL_MARGIN = 10;
    static final int HORIZONTAL_MARGIN = 5;
    static final int TOP_MARGIN = VERTICAL_MARGIN;
    static final int BOTTOM_MARGIN = VERTICAL_MARGIN;
    static final int LEFT_MARGIN = HORIZONTAL_MARGIN;
    static final int RIGHT_MARGIN = HORIZONTAL_MARGIN;

    public HorizontalRulerView(Context context) {
        this(context, null);
    }

    public HorizontalRulerView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public HorizontalRulerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOrientation(VERTICAL);
        View v = new View(context);
        v.setBackgroundColor(COLOR);
        LayoutParams lp = new LayoutParams(
            LayoutParams.MATCH_PARENT,
            HEIGHT
        );
        lp.topMargin = TOP_MARGIN;
        lp.bottomMargin = BOTTOM_MARGIN;
        lp.leftMargin = LEFT_MARGIN;
        lp.rightMargin = RIGHT_MARGIN;
        addView(v, lp);
    }

}

Use it programmatically or in Eclipse (Custom & Library Views -- just pull it into your layout).

Chrissi
  • 1,188
  • 10
  • 9
1

Use This..... You will love it

 <TextView
    android:layout_width="fill_parent"
    android:layout_height="1px"
    android:text=" "
    android:background="#anycolor"
    android:id="@+id/textView"/>