Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm working on Android application development. There is a question that always comes to my mind that: How XML interacts with Java code?. I mean Button inside XML is a view, but Button inside Java is a class.

For populating and XML layout we use setContentView(R.layout.name). What I want to know is how this XML code interacts with Java, what technique is used and what code is behind this. If any know please tell me in brief.

share|improve this question
    
For this Android internally uses Java XML parsers. For each XML property, there is a method in Android. –  user82310 Feb 23 '13 at 12:40
    
thanks lot @user82310 but can you elaborate this please –  Dilip Kumar Chaudhary Feb 25 '13 at 9:14
add comment

2 Answers

up vote 1 down vote accepted

To understand the relationship between the two, I need to first clarify one thing. You say that "Button inside XML is a view, but Button inside Java is a class" which is true but a bit narrow. A Button in XML is not just a View but it's also a class.

View is a Java class. Button is a subclass of View. The layout XML in Android is just a simple way to arrange a number of View objects to form a UI. Anything you do in a layout XML can be done in Java code and it can accomplish the exact same thing. Android has these layout XMLs to simplify the process.

Consider the following example. These two scenarios will create the exact same UI.

Create UI with layout XML:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <LinearLayout android:id="@+id/left_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Create UI in Java code:

MainActivity.java

public static final int LEFT_CONTAINER = 0x0710001B;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout parent = new LinearLayout(this);
    parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    parent.setBackgroundColor(getResources().getColor(android.R.color.black));

    LinearLayout child = new LinearLayout(this);
    child.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    child.setId(LEFT_CONTAINER);
    parent.addView(child);

    setContentView(parent);
}

Now this is a very simple layout so the required code for each method is relatively light. If we scale this up to a UI with multiple containers and multiple controls, the Java code method starts to get really cluttered and prone to error. You're also creating the possibility for multiple dependencies on your MainActivity class since it's holding the IDs of the layouts within it.

By using the layout XML, you confine that majority of your layout issues to one separate file and you reduce the amount of code that needs to be written to create your UI as the Android Activity parent class will handle all of that for you. Additionally, you can reduce cross dependencies by having all of your layout and view IDs confined to your R class that Android generates for identifiers.

share|improve this answer
    
Thanks for this wonderful guide –  Dilip Kumar Chaudhary Feb 27 '13 at 5:46
add comment

To summarize in short, an XML layout is a way of separating the construction of View hierarchy from Android/Java code. This helps you in modifying and editing the UI layout easily.

When you set an XML layout using setContentView(R.layout.mylayout) the Android will internally inflate the mylayout.xml and build a LinearLayout class and adds it to the View Root.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.