Trying to understand Views

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Trying to understand Views

Postby mangaluve » Mon Mar 23, 2009 9:17 pm

I've been programming J2ME for some time and now I'd like to learn android. I've done some things with a SurfaceView and a Canvas, which is pretty similar to J2ME. However, now I'm starting to learn the XML-stuff and about Views.

As far as I've understood, an Activity contains a set of Views, where the "current" view is set with setContentView. This View can then contain other views itself. Is this correct? What I don't understand is, what's the difference between a layout, for instance LinearLayout, and a View? Or is a LinearLayout just a subclass of View? Secondly, I can't really get my head around this XML thing. Suppose I have a XML file like this

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent" android:orientation="vertical">
  8.  
  9.    
  10.  
  11. </LinearLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


When this is "compiled" what is the result. Does the XML-file represents a View (with views in it)? In that case, what kind of View?
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top

Postby wiederke » Tue Mar 24, 2009 2:12 pm

Hi,

I suggest reading the DevGuide on User Interfaces.

http://developer.android.com/guide/topics/ui/index.html


Cheers

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Re: Trying to understand Views

Postby WarrenFaith » Wed Mar 25, 2009 8:56 am

mangaluve wrote:What I don't understand is, what's the difference between a layout, for instance LinearLayout, and a View? Or is a LinearLayout just a subclass of View?

Well, I would say that the View is simply the JPanel representation. The LinearLayout is your Layout and everything like TextView is Jwhatever.
mangaluve wrote:When this is "compiled" what is the result. Does the XML-file represents a View (with views in it)? In that case, what kind of View?

If you understand the thing I said above you should now be able to answer yourself :)
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby mangaluve » Wed Mar 25, 2009 9:26 am

Okay, I've never worked with UI in regular Java (just J2ME) so I cannot really relate to Swing. But I've read some more and I think I understand it better now
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby kanzlr » Wed Mar 25, 2009 10:17 am

the XMLs aren't really compiled into anything.
The R class is generated out of them, so you can reference the elements you describe there in your code by means of the R class.

In your XML you can define your GUI. You can put there simple Views like a text view or a table row, or you can define a complete layout. Where you have a LinearLayout a top, for example, and put all your buttons, textviews, etc in there and arrange them.
In your onCreate method you just reference the layout you created in the XML and Android creates the views on the fly from the XML.

So if you create a main.xml in your layout subfolder, you can put the following line in your onCreate method to load the layout:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

and later on find elements there by means of the findViewById method:

if you have a button defined in your main.xml like that:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <Button android:layout_height="wrap_content" android:id="@+id/startScreenNewBlindset" ...
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

you can reference it in your code by means of the R class with R.id.startScreenNewBlindset:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button newBlindButton = (Button) findViewById(R.id.startScreenNewBlindset);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

if you need to interact with it.
kanzlr
Developer
Developer
 
Posts: 25
Joined: Sun Mar 15, 2009 4:44 pm
Location: Vienna/Austria/EU

Postby mangaluve » Wed Mar 25, 2009 10:22 am

Thanks for the replies! I think I know how to use XML to build GUI, it's more that I want to understand it better. Like when I do setContentView(R.layout.xxx) what happens then? Is the XML represented by R.layout.xxx inflated into an instance? And when I do findViewById, where does this method looks for the View with a certain ID? In the current contentview? Is it recursive? And so on..
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top

Postby kanzlr » Wed Mar 25, 2009 10:29 am

R.layout.xxx is just an integer, it is not an instance. There is really no instance of the xml, it just gets interpreted and the views are instantiated according to the xml.

findViewById is a method provided by the View class, it searches this view, and afaik it is recursive.
but if you have
Code: Select all
view1
  |
  ---view2
         |
          ---view3

and do a view2.findViewbyId(<stuff-thats-in-view1>) you won't find it.
kanzlr
Developer
Developer
 
Posts: 25
Joined: Sun Mar 15, 2009 4:44 pm
Location: Vienna/Austria/EU

Postby mangaluve » Wed Mar 25, 2009 11:09 am

Thanks a lot, it's really helping me to understand!

I know that the R.layout.xxx is just an integer, what I mean was that when I do setContentView(R.layout..) the system takes the XML and creates an instance from it. My english isn't the best, but what I tried to say was that when the contents of the XML is "translated" into a View, it's just like any other view right? And the "type" of View is is determined by the "first" tag in the XML document? So if my XML document starts with <LinearLayout> then setContentView(..) will create a LinearLayout-instance and use it?

And when I do findViewById on my activity instead of a View, does it search the "current" view then? Can an Activty have multiples Views? (I know an Activity can have a ViewGroup which has multiple views but can the Activity itself have more than one view?)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby kanzlr » Wed Mar 25, 2009 11:11 am

pretty much.

LinearLayout is also just a view

http://developer.android.com/reference/ ... ayout.html
kanzlr
Developer
Developer
 
Posts: 25
Joined: Sun Mar 15, 2009 4:44 pm
Location: Vienna/Austria/EU

Postby mangaluve » Wed Mar 25, 2009 11:26 am

Thanks!

What about an Activity having multiple views? If I call Activity.addView, will that view be shown, aswell with the others? Or is just one view the "current"?

Im a little confused though because as far as I've understood, every parent-View calls measure(..) on its children, where the arguments is the "maximum" rectangle in which the children must fit. But I created a own button by inheriting Button and override the onMeasure-method and printed the arguments. I put the button in a LinearLayout. The arguments to the button's onMeasure was -2147483648, -2147483648. What does this mean?
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby kanzlr » Wed Mar 25, 2009 11:31 am

hm. I was under the impression that there is just a single content view. hm. not sure, really :)
and as I've never implemented my own views, I have not overridden the measure method yet, sorry.
kanzlr
Developer
Developer
 
Posts: 25
Joined: Sun Mar 15, 2009 4:44 pm
Location: Vienna/Austria/EU

Postby mangaluve » Wed Mar 25, 2009 11:40 am

Okay thanks!

It also says that setContentView(id) addsAll the top-level views in the id-resource to the Activity. Can I have more than one top-level View in an XML file? If I try that, I get an error in Eclipse, saying something about the formatting
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby kanzlr » Wed Mar 25, 2009 11:41 am

no, you can't :)
kanzlr
Developer
Developer
 
Posts: 25
Joined: Sun Mar 15, 2009 4:44 pm
Location: Vienna/Austria/EU

Postby mangaluve » Wed Mar 25, 2009 11:47 am

Okey thanks! Because the documentation says "Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity." about Activity.setContentView.
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests