12

I have a RelativeLayout that inflates just fine. I would like to add a solid color rectangle spanning the width of the layout at the top. I tried putting the following into my xml:

<view android:id="@+id/top_alert"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_above="@+id/orders_gridview"
    android:layout_alignParentTop="true"
    android:background="@color/table_request_assistance"
    android:visibility="visible"/>

Now, when I try to inflate my Layout I get a NullPointerException at LayoutInflater.createViewFromTag (line 715):

if (name.equals(TAG_1995)) {

name is set earlier thusly:

if (name.equals("view")) {
    name = attrs.getAttributeValue(null, "class");
}

Evidently there is no "class" attribute. How do I add that? I can't find anything close in http://schemas.android.com/apk/res/android. Should I add it? Is this the standard way to do this? It seems like it should be the simplest thing in the world.

2 Answers2

11

For the noobs here is some more general markup. This will print a 10-pixel high grey rectangle spanning the top of its parent view at the top.

<View android:id="@+id/rectangle_at_the_top"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_alignParentTop="true"
    android:background="#DDDDDD"
    android:visibility="visible"/>

Explanation:

This is the rectangle's id:

android:id="@+id/rectangle_at_the_top"

This says make the View as wide as the parent:

android:layout_width="match_parent"

Note you'll sometimes see "fill_parent". That has been deprecated in favor of "match_parent".

This says make the height 10 "ensity-independent pixels high:

android:layout_height="10dp"

What is a "density-independent pixel" you ask? I'm not 100% sure, but these guys know: What's the difference between px, dp, dip, and sp in Android?

This says align the rectangle with the top of the parent View:

android:layout_alignParentTop="true"

More accurately it makes the top edge of the View the same as the top edge of the parent. Want to put something at the bottom? Yup, you guessed it: use layout_alignParentTop.

This says set the background color to a grey-ish color:

android:background="#DDDDDD"

DDDDDD is a color value. You can find examples of other color values and how Google suggests to use them here: Google's Android Color Guide

Finally, this says to make this View visible:

android:visibility="visible"

This is mostly redundant as they are visible by default. Other options include "invisible" and "gone" which sound similar but are crucially different. For more info see this discussion: What is the difference between "invisible" and "gone?"

Community
  • 1
  • 1
3

Classes are case sensitive - in this case, you are using the View class and therefore it needs to be exactly View in your XML.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443