In Android, what you see is activity. You interact with Android application through activity. Usually application has more than one activity that are connected each other. In other words, what you see in an Android device is an activity
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
In this tutorial we will show you how to play with activity. We will create 2 activity in which it navigates to each other when a button is clicked.
1. Create Project
We assume you understand creating Android project using eclipse, including create an AVD (Android Virtual Device) and running it. If you never know before you can learn it here
2. Create Layout
Create two xml layout files in “res/layout”
1. activity_first.xml –> Represents Screen 1
2. activity_second.xml –> Represents Screen 2
file : activity_first.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FirstActivity" > <TextView android:id="@+id/textViewFirst" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SCREEN 1" /> <Button android:id="@+id/buttonFirst" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go To Second Screen" /> </LinearLayout> |
file : activity_second.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:id="@+id/textViewSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SCREEN 2" /> <Button android:id="@+id/buttonSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textViewSecond" android:layout_below="@+id/textViewSecond" android:text="Go To First Screen" /> </RelativeLayout> |
2. Create Activity
Create two activity classes. Each activity has one layout we have created before
1. FirstActivity.java –> activity_first.xml
2. SecondActivity.java –> activity_second.xml
file : FirstActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.agung.navigateactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FirstActivity extends Activity implements OnClickListener { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); button=(Button) findViewById(R.id.buttonFirst); button.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.first, menu); return true; } @Override public void onClick(View arg0) { startActivity(new Intent(FirstActivity. this,SecondActivity.class)); } } |
file : SecondActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.agung.navigateactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity implements OnClickListener { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); button=(Button) findViewById(R.id.buttonSecond); button.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.second, menu); return true; } @Override public void onClick(View v) { startActivity(new Intent(SecondActivity.this,FirstActivity.class)); } } |
3. Android Manifest
Declare two activity above in AndroidManifest.xml
file : AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.agung.navigateactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="9" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.agung.navigateactivity.FirstActivity" 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.agung.navigateactivity.SecondActivity" android:label="@string/title_activity_second" > </activity> </application> </manifest> |
gan, itu kalo pake button
kalo di swipe gmana ya ?
trus itu berarti masuk ke xml babru ?
dalam 1 xml, bisa dinavigate ke 3 cml / lebih ga ?
jadi misal xml 1. isinya ada 3 button yg ngarah ke xml lain. bsa kan ?