Java Source Code
Let's see the java source file created by the Eclipse IDE:
File: MainActivity.java
- package com.example.helloandroid;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.TextView;
- public class MainActivity extends Activity {//(1)
- @Override
- protected void onCreate(Bundle savedInstanceState) {//(2)
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);//(3)
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {//(4)
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
It provides life cycle methods for activity such as onCreate, onStop, OnResume etc.
(2) The onCreate method is called when Activity class is first created.
(3) The setContentView(R.layout.activity_main) gives information about our layout resource. Here, our layout resources are defined in activity_main.xml file.
File: activity_main.xml
- <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="@string/hello_world" />
- </RelativeLayout>
File: strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">helloandroid</string>
- <string name="hello_world">Hello world!</string>
- <string name="menu_settings">Settings</string>
- </resources>
No comments:
Post a Comment