Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Rebuilding from Scratch Handling a Lack of Data

Sante Kotturi
Sante Kotturi
7,434 Points

How does the android:id="@android:id/empty" TextView know you're asking if the ListView is empty?

It sort of seems like magic that this textview understands we're asking if the listview is empty...

I would have expected us to have to references the ListView and check to see if its empty before displaying the placeholder TextView. something like:

android:hide:="@android:id/list".isEmpty() (because you can totally method chain java in xml....)

What if we had two ListViews and two TextViews (one to placehold for each ListView). If it's corresponding ListView didn't yet have any data to present, how would each TextView know which ListView to check if empty?

The code I'm referencing:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="No items to display, yet" /> 

Thanks in advance, Sante

1 Answer

Hi Sante,

Firstly never did see an XML being chained to Java class members. You could parse an xml using DOM, SAX, JDOM etc. but this is new. If there is any resource I could I see for this would be great to learn something new.

What if we had two ListViews and two TextViews (one to placehold for each ListView)
Where exactly would these two elements be? Inside the same layout? As far as I know one cannot have the same id within one given layout. I could have two relative layouts and use @android:id/empty and @android:id/list for each layout structure. So in that case the point of which TextView would know which ListView to check if empty, is ruled out.

I hope I made sense :)

Surely other experienced Android users could add more to this.

Hope this helps.

Sante Kotturi
Sante Kotturi
7,434 Points

I understand what you're saying about these being unique id's but I still don't see how the TextView id:

<TextView
    android:id="@android:id/empty" /> 

References the ListView id:

<ListView
    android:id="@android:id/list" /> 

What if we had an email textfield in the same Relative Layout:

<EditText
    android:inputType="textEmailAddress"
    android:id="@+id/editText" />

What if, when this email field was empty, I wanted to display a textfield that said "Dont forget to enter your email". This is silly of course because I would just populate the email field with this text and then highlight red if the user tried to advance without the required field but regardless, imagine I wanted to do it this way...

I guess the bigger picture here is that: 1) Why is my view template doing my view logic? I'm coming from the world of backbone.js and angular.js where I would just write my html template and then my controller would handle all of the logic, using jquery .hide(), .show(), .toggle() etc as data is updated in the DOM. It doesn't make sense for the XML to be doing the logic, isn't it just suppose to be a dumb little template? (no offense XML) 2) If I accept that my view logic is being handled by my view itself (and not the controller, although we just watched the MVC video before this video....), it still seems like magic to me that android:id="@android:id/empty" references android:id="@android:id/list". Is this because @android:id gets compiled into an actual value that point to each other? Does this become something like 837410485a33b which then links the two elements together? And then my email field has its own id: android:id="@+id/editText".

EDIT: after reading this SO post, my idea of @android:id/<param> being compiled into some uniqueID isnt' correct: http://stackoverflow.com/questions/4355614/what-is-difference-between-id-androidlist-and-id-list

I'm guessing the answer to my question lies within the belly of this @android:id/<param> which I don't fully understand yet. And how this is different from the android:id="@+id/<param>.

Does this clarify my question at all?

PS. My desire to chain java methods comes from my javascript/jquery time where something like:

$("someDomElement).isEmpty().replace("otherDomElement");

Would work (this one wouldnt actually work but thats the idea....)HA.