Android Activity – From One Screen To Another Screen
In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens, for example, one activity to display a list of the application settings, another activity to display the application status.
In this tutorial, we show you how to interact with activity, when a button is clicked, navigate from current screen (current activity) to another screen (another activity).
1. XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml– Represent screen 1res/layout/main2.xml– Represent screen 2
File : res/layout/main.xml
<?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:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="This is First Screen" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<Button android:id="@+id/button1" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" android:layout_height="wrap_content" android:text="Click Me" android:layout_width="wrap_content"></Button>
</LinearLayout>
File : res/layout/main2.xml
<?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:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="This is Second Screen" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView> </LinearLayout>2. Activities
Create two activity classes :
- HelloAndroidActivity.java –> main.xml
- Button2.java –> main2.xml
To navigate from one screen to another screen, use following code :Intent intent = new Intent(context, anotherActivity.class); startActivity(intent);File : HelloAndroidActivity.javapackage com.helloexample; 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 HelloAndroidActivity extends Activity implements OnClickListener{ private Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(this); } public void onClick(View v) { Intent i; switch (v.getId()) { case R.id.button1: i = new Intent(HelloAndroidActivity.this, Button2.class); startActivity(i); break; default: break; } } }File : Button2.javapackage com.helloexample; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Button2 extends Activity { private Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); } }3. AndroidManifest.xml
Declares above two activity classes inAndroidManifest.xml.File : AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.helloexample" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloAndroidActivity" 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=".Button2" android:label="@string/app_name"> </activity> </application> </manifest>4. Demo
Run application.AppActivity.java(main.xml) screen is display.When above button is clicked, it will navigate to another screen (main2.xml).
No comments:
Post a Comment