An SO User
An SO User

Reputation: 25028

Android UI can be created by code *AND* XML?

I am having trouble grasping a certain concept in Android UI design. The book I am referring to first uses the usual technique that Java programmers use to create UIs and that is to to create containers and add UI components to them and nest them as necessary.

Now, the book introduces a new concept where the entire UI was created using an XML file. The code is pasted below:

package com.oreilly.android.intro;
import android.app.Activity;
import android.os.Bundle;
/**
* Android UI demo program
*/
public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);
 }
}  

so basically, I can use any of them ?

Upvotes: 1

Views: 138

Answers (3)

codeMagic
codeMagic

Reputation: 44571

Simple answer,yes, you can use either approach. However, there are some limitations to each such as there are layout properties that must be set in xml if you want to use them. I can't think of what any are off-hand but I can look them up.

For the most part, creating the layouts is much simpler to do in xml but you do have the option of setting Views and layouts in Java if you need to such as creating an unknown number of Buttons depending on some user-defined variable.

When you create your UI in xml then you inflate it in your Java code. This is normally done in onCreate() using

setContentView(R.layout.main);

as you see in your example. But it can also be done with an inflater.

The thing to remember here is to inflate your layout, using either method, before trying to initialize any views in the layout or you will get a NPE when trying to call a method on a View defined before inflating the layout it is contained in.

A correct way

**Examples of inflating views/layouts correctly**
    Button mBtn;
    public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);

   btn = (Button) findViewById(R.id.buttonId); // Button is initialized after inflating the layout
 }
} 

Incorrect way

    public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   Button mBtn = (Button) findViewById(R.id.buttonId);  // Button is initialized before inflating layout which will return null
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);
}
}   

I added the above example because I have seen a lot of people make that mistake. So don't do it...you've been warned! :)

Upvotes: 4

Pozzo Apps
Pozzo Apps

Reputation: 1857

Yes. But is preferable to use xml, it is more powerful, easier and will separate layout from your code. Take a look at the docs: http://developer.android.com/guide/topics/ui/declaring-layout.html

Upvotes: 1

r2DoesInc
r2DoesInc

Reputation: 3771

Not entirely sure what you're asking, but the two are interchangeable. Most of the time your UI will be done via xml. In some cases though, the ui is heavily dependent of the data, so you may need to dynamically generate it.

It basically comes down to whichever is easiest for you at the time.

Upvotes: 2

Related Questions