Android is so easy to learn and you enjoy when learning it. Lets create a simple app, which includes the following:
It contains two pages
a)First page with Title, Username label, text to enter user name, password label, text to enter password, submit button.
b) On clicking submit button, it should take me to second page which shows the name entered by user in first page, saying "Hello xxx", a back button to go back to first activity.
Activity is a page which is associated with one .java file and one .xml file.
.xml file contains the components which are visible to user(we can add components dynamically from .java file as well)
.java file contains the life cycle methods, component listeners, etc.
So lets create our first page:
1. Creating First Activity: LoginActivity
By default, when an android app is created, eclipse creates one activity xml file: activity_main.xml and one java file: MainActivity.java. Delete both these files.
a) Creating layout file: login_layout.xml
Right click on res/layout folder and select New > Android XML File
Give the name of the file as "login_layout"
click on "Finish"
Now go to text representation of the file, instead of Graphical layout.
Add the below content, inside LinearLayout tag: (all the corresponding strings must be defined in res/values/strings.xml file)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="@+id/txtLoginTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/loginTitle" >
</TextView>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.4"
android:text="@string/userNameLabel" >
</TextView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text" android:layout_weight="0.6">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/passWordLabel" >
</TextView>
</LinearLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/submitButton"/>
</LinearLayout>
b) creating LoginActivity.java file:
package com.example.activitynavigationapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.activitynavigationapp.R;
public class LoginActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.login_layout);
super.onCreate(savedInstanceState);
Button submitBtn = (Button) findViewById(R.id.btnSubmit);
submitBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, UserActivity.class);
this.startActivity(intent);
finish();
}
@Override
public void onDestroy() {
System.out.println("onDestroy() is called...");
super.onDestroy();
}
}
2. Create second activity: UserActivity
a)user-layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="@+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/welcomeText" >
</TextView>
<Button
android:id="@+id/btnGoBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/goBack" />
</LinearLayout>
b) UserActivity.java
package com.example.activitynavigationapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class UserActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.user_layout);
super.onCreate(savedInstanceState);
Button goBackButton = (Button) findViewById(R.id.btnGoBack);
goBackButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, LoginActivity.class);
this.startActivity(intent);
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitynavigationapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activitynavigationapp.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.activitynavigationapp.UserActivity"
android:label="@string/app_name" />
</application>
</manifest>
4) strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActivityNavigationApp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="loginTitle">Please Login Here</string>
<string name="userNameLabel">Username</string>
<string name="passWordLabel">Password</string>
<string name="submitButton">Submit</string>
<string name="welcomeText">Welcome Guest</string>
<string name="goBack">Go Back</string>
</resources>
Output:
Activity is a page which is associated with one .java file and one .xml file.
.xml file contains the components which are visible to user(we can add components dynamically from .java file as well)
.java file contains the life cycle methods, component listeners, etc.
So lets create our first page:
1. Creating First Activity: LoginActivity
By default, when an android app is created, eclipse creates one activity xml file: activity_main.xml and one java file: MainActivity.java. Delete both these files.
a) Creating layout file: login_layout.xml
Right click on res/layout folder and select New > Android XML File
Give the name of the file as "login_layout"
click on "Finish"
Now go to text representation of the file, instead of Graphical layout.
Add the below content, inside LinearLayout tag: (all the corresponding strings must be defined in res/values/strings.xml file)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="@+id/txtLoginTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/loginTitle" >
</TextView>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.4"
android:text="@string/userNameLabel" >
</TextView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text" android:layout_weight="0.6">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/passWordLabel" >
</TextView>
</LinearLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/submitButton"/>
</LinearLayout>
b) creating LoginActivity.java file:
package com.example.activitynavigationapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.activitynavigationapp.R;
public class LoginActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.login_layout);
super.onCreate(savedInstanceState);
Button submitBtn = (Button) findViewById(R.id.btnSubmit);
submitBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, UserActivity.class);
this.startActivity(intent);
finish();
}
@Override
public void onDestroy() {
System.out.println("onDestroy() is called...");
super.onDestroy();
}
}
2. Create second activity: UserActivity
a)user-layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="@+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/welcomeText" >
</TextView>
<Button
android:id="@+id/btnGoBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/goBack" />
</LinearLayout>
b) UserActivity.java
package com.example.activitynavigationapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class UserActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.user_layout);
super.onCreate(savedInstanceState);
Button goBackButton = (Button) findViewById(R.id.btnGoBack);
goBackButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, LoginActivity.class);
this.startActivity(intent);
}
}
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitynavigationapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activitynavigationapp.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.activitynavigationapp.UserActivity"
android:label="@string/app_name" />
</application>
</manifest>
4) strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActivityNavigationApp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="loginTitle">Please Login Here</string>
<string name="userNameLabel">Username</string>
<string name="passWordLabel">Password</string>
<string name="submitButton">Submit</string>
<string name="welcomeText">Welcome Guest</string>
<string name="goBack">Go Back</string>
</resources>
Output:
