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.

5
  • Can you also post the exception Commented Jan 5, 2015 at 20:48
  • 1
    What happens when you replace <view with <View? Commented Jan 5, 2015 at 20:48
  • You can use a TextView with no text. I upvoted as I'd like to know the standard answer to this. Commented Jan 5, 2015 at 20:58
  • @ianhanniballake Well, now it inflates. Ok so "view" is nothing and "View" is a View? I wonder what that code is supposed to do. What is the expected situation where name would equal "view"? Post this as an answer and I'll mark it as correct. Commented Jan 5, 2015 at 21:01
  • @pbabcdefp I have a hunch this is the standard way to do this. I just had a typo. Commented Jan 5, 2015 at 21:02

2 Answers 2

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?"

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.