Você está na página 1de 8

Android Application Development Training Tutorial

For more info visit http://www.zybotech.in

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Android Sample Programs


Explicit Intent
ActivityOne.java
import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Button;

public class Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button next = (Button) findViewById(R.id.Button01); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent myIntent = new Intent(view.getContext(), Activity2.class); // startActivityForResult(myIntent, 0); startActivity(myIntent); } }); } }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

ActivityOne.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" android:background="#ffffff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="This is Activity 1" /> <Button android:text="Next" android:id="@+id/Button01" android:layout_width="250px" android:textSize="18px" android:layout_height="55px"> </Button> </LinearLayout>

ActivityTwo.java
import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Button;

public class Activity2 extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main1); Button next = (Button) findViewById(R.id.Button02); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {

Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } }); } }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

ActivityTwo.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" android:background="#ffffff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="This is Activity 2" /> <Button android:text="Previous" android:id="@+id/Button02" android:layout_width="250px" android:textSize="18px" android:layout_height="55px"> </Button> </LinearLayout>

AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity1" 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=".Activity2"></activity> </application>

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Another Example
ActivityOne.java
import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.EditText;

public class ActivityOne extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bundle extras = getIntent().getExtras(); if (extras== null){ return; } String value1 = extras.getString("Value1"); String value2 = extras.getString("Value2"); if (value1 != null && value2 !=null){ EditText text1= (EditText) findViewById(R.id.EditText01); EditText text2 = (EditText) findViewById(R.id.EditText02); text1.setText(value1); text2.setText(value2); } } public void onClick(View view){ Intent i = new Intent(this, ActivityTwo.class); i.putExtra("Value1", "This value one for ActivityTwo "); i.putExtra("Value2", "This value two ActivityTwo"); startActivity(i); }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

ActivityOne.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" > <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text1:"></TextView> <EditText android:text="values" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText></LinearLayout> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text2:"></TextView> <EditText android:text="value1" android:id="@+id/EditText02" android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText></LinearLayout> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Calling an intent"> </Button> </LinearLayout>

ActivityTwo.java
import android.app.Activity; import android.content.Intent; import android.os.Bundle;

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

import android.view.View; import android.widget.EditText; public class ActivityTwo extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle bundle ) { super.onCreate(bundle); setContentView(R.layout.main); Bundle extras = getIntent().getExtras(); if (extras== null){ return; } String value1 = extras.getString("Value1"); String value2 = extras.getString("Value2"); if (value1 != null && value2 !=null){ EditText text1= (EditText) findViewById(R.id.EditText01); EditText text2 = (EditText) findViewById(R.id.EditText02); text1.setText(value1); text2.setText(value2); } } public void onClick(View view){ Intent i = new Intent(this, ActivityOne.class); i.putExtra("Value1", "Swinging on a star. "); i.putExtra("Value2", "You could be bettern then you are. "); startActivity(i); } }

AlertDialog
AlertDialog1.java
import import import import android.app.Activity; android.app.AlertDialog; android.content.DialogInterface; android.os.Bundle;

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

import android.widget.Toast; public class AlertDialog1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setMessage("Do you want to close this window ?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); dialog.cancel(); } }); AlertDialog alert = alt_bld.create(); // Title for AlertDialog alert.setTitle("Title"); // Icon for AlertDialog alert.setIcon(R.drawable.icon); alert.show(); } }

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi