Android provides two types of UI construction model i.e. programmatic UI layout and XML-based layout. In this article I am going to explain how to create an Android application in Android Emulator by using XML layout files.
Expand the Project in the Package Explorer à Expand res folder à Layout folder à Select main.xml.
· Here’s an XML layout file (main.xml) that is identical in behavior to the programmatically constructed file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="bold"
android:typeface="serif"
android:text="@string/hello"/>
· Inside the res/values/ folder open strings.xml, modify the strings.xml as shown below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Welcome! This is my first Android Application</string>
<string name="app_name">FirstAndroidApp</string>
</resources>
· Now open and modify your HelloAndroid class and use the XML layout. Edit the file as shown below:
package com.example.firstApp;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
After creating the above changes run your application:
Right click on the project (FirstAndroidApp) which is shown in the Package Explorer à Select Run As à Android Application.
Output as shown below:
After performing the above steps you can easily create and run an Android application by using XML layout files.
Leave Comment