MY mENU


Saturday 11 February 2012

Main.xml and strings.xml files in Android

main.xml:

 The layout contains the main.xml which is called when the application is started. If you are familiar with c or java programming you know the function main which is called when ever the programs first starts and in a similar way the main.xml draws its content as soon as the application starts.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>

It starts with the LinearLayout tag which implies that we want to put some components on the screen in a linear fashion. There are many other layouts also defined in Android. The orientation, width and height describe how the layout should look. We follow it with a “TextView” component which is used to display texts on the screen. In this example it is taking the text from the “hello” string defined in the “strings.xml”.

Load XML Resource:



When you compile your application, each XML layout file is compiled into a View resource. You should load the layout resource from your application code, in your Activity.onCreate() callback implementation. Do so by calling setContentView(), passing it the reference to your layout resource in the form of: R.layout.layout_file_name For example, if Your XML file saved as main.xml than you put the code like as setContentView(R.layout.main);

strings.xml:

The strings.xml contained in the values folder is used to define strings to be used within the applications.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid!</string>
<string name="app_name">This is SrinivasMahanti</string>
</resources>

From the above content you can easily make out that the hello string corresponds to the actual string “Hello World, HelloAndroid!.


No comments:

Post a Comment