SlideShare a Scribd company logo
Tutorial on Activity Life Cycle and Activities
1. Create a newprojectinandroidand name it“ActivityLifeCycleAndActivities”andplease
remembername yourxml file as“activitylifecycleandactivities.xml”.ItisyourfirstActivity.
2. Please addthe followingcode in“activitylifecycleandactivities.xml”.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="10dp"
android:text="Hello World, MainActivity!">
</TextView>
<Button
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Activity Two"
android:id="@+id/btnActTwo">
</Button>
<Button
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact View"
android:id="@+id/buttonContact" />
<Button
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ChangeColor"
android:id="@+id/buttonColor" />
</LinearLayout>
3. Addthe followingcode in onCreate functionof yourActivity“ActivityLifeCycleAndActivities”
// intilization
btnActTwo = (Button) findViewById(R.id.btnActTwo);
btnContact= (Button) findViewById(R.id.buttonContact);
btnColor= (Button) findViewById(R.id.buttonColor);
container= (LinearLayout) findViewById(R.id.container);
// link UI elements to action the code
btnContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnActTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
4. Run and Install the app and you should see the similar output as shown below.
5. Type the following line in the onCreatefunction of your current Activity.
6. Write the following code after the function of “OnCreate () as follows
And then press the key “ctrl+o” ( Note, this only works in Android Sudio) and choose the method OnRestart
as shown in the snapshots below.
Lecture exercise on activities
After step 6, your code will look like this.
7. After that add all functions of Activity Life cycle using the step 6. As shown below
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MainActivity: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "MainActivity: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "MainActivity: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "MainActivity: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainActivity: onDestroy()");
}
8. Add the following line in the global space of your activity (it is the space write after your activity)
9. Now at least run the emulator and device first and click the “logcat” and choose “edit filter configuration”.
10. Add the following in the filter “states”.
11. You shouldsee the followinglinesinthe logcat afterrunningyourapplication
Part 2. Creating Second Activity
1. Right click on your package folder as shown below and add a new Activity and name it
as “ActivityTwo”.
2. Also add the xml file for “ActivityTwo” and name it “activitytwo” by choosing the
following actions.
3. Add the following code in the “activitytwo.xml”.
4. <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Activity Two">
</TextView>
</LinearLayout>
5. Go to the source folder of your Activity and add the following code.
public class ActivityTwo extends Activity {
final String TAG = "States";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Log.d(TAG, "ActivityTwo: onCreate()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "ActivityTwo: onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "ActivityTwo: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "ActivityTwo: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "ActivityTwo: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ActivityTwo: onStop()");
}
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "ActivityTwo: onDestroy()");
}
}
6. Remove the errors if any
7. Now Go to the activity “ActivityLifeCycleAndActivities”andaddthe followingcode inthe
listener.
8. //Button Listener for Activity Two
btnActTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
ActivityTwo.class);
startActivity(intent);
}
});
9. Now Run the Application and press the button “Go To Activity Two”. And you should
see the following. ( If not, remove the logical errors by debugging)
In the log cat.
Part 3. Using onActivityFor Result
1. Add new activity as “ActivityThree” ( see step 1 on Part 2)
2. Add xml file in the layout folder of your project as “activitythree” and add the following
code.
<?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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
3. Add the following code in the “ActivityThree”.
4. public class ActivityThree extends AppCompatActivity {
ListView listV;
ArrayList <String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitythree);
listV= (ListView) findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listV.setAdapter(adapter);
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
}
});
}
}
5. Create an ArrayList and add it after the ListView As shown in the ActivityThree
6. Add the following code in the listener.
7. Now go to the Activity “ActivityLifeCycleAndActivities” andaddthe followingcode inthe
listener.
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityThree.class);
startActivityForResult(intent, PICK_COLOR );
}
});
8. Add the “PICK_COLOR” variable in the global space of this Activity.
9. Add the following function after the On Destroy function. Of this activity.
public void changeBackground(String color) {
// depending the extra value String, choose a background color
if (color.equals("red")) {
container.setBackgroundColor(Color.RED);
} else if (color.equals("green")) {
container.setBackgroundColor(Color.GREEN);
} else if (color.equals("blue")) {
container.setBackgroundColor(Color.BLUE);
}
}
10. Add the following line of code in the same Activity. ( Just write few onActi)
11. Remove the Error if any.Run your application and pressthe button“CHANGECOLOR”andselect
Redfrom the ActivityThree andyoushouldsee yourbackgroundredinthe firstactivity.
Lecture exercise on activities
Part 4 showing the Contacts of your
smartphones
1. Add the following code in the listener of your activity “ActivityLifeCycleAndActivities”
2. add the pickContact as shown.
3. copy and paste the following code in the “onActivityResult” Method.
if ( requestCode==PICK_COLOR)
{
String clr=data.getStringExtra("COLOR");
changeBackground(clr);
}
// Check which request it is that we're responding to
else if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in
the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the
given URI)
// CAUTION: The query() method should be called from a separate thread to
avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't
do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
4. Run the application , you should see the output like this. After pressing the
“contactView” Button.

More Related Content

PPTX
Create an other activity lesson 3
PDF
Aspnet mvc tutorial_01_cs
PDF
AI: Mobile Apps That Understands Your Intention When You Typed
DOCX
LearningMVCWithLINQToSQL
PPTX
Android Tutorials - Powering with Selection Widget
DOCX
Android App To Display Employee Details
PPTX
Point and Click App Building Workshop
PDF
Meteor.js Workshop by Dopravo
Create an other activity lesson 3
Aspnet mvc tutorial_01_cs
AI: Mobile Apps That Understands Your Intention When You Typed
LearningMVCWithLINQToSQL
Android Tutorials - Powering with Selection Widget
Android App To Display Employee Details
Point and Click App Building Workshop
Meteor.js Workshop by Dopravo

What's hot (20)

PPT
IMPACT/myGrid Hackathon - Introduction to Taverna
PPTX
Android Intent.pptx
PDF
Android appwidget
PPTX
Google analytics for iOS
PDF
AI: Integrate Search Function into Your App Using Bing Search API.
PDF
Ios actions and outlets
PPTX
Flex Building User Interface Components
PPT
OpenSocial Intro
PPTX
Lightning Components Workshop v2
PPTX
Grails Advanced
PPT
Android tutorial (2)
PDF
React native app with type script tutorial
PDF
Creating web api and consuming- part 1
PPTX
CloudBrew: Windows Azure Mobile Services - Next stage
PPT
Gaad01 job scheduler
PDF
Cómo tener analíticas en tu app y no volverte loco
PDF
iOS_Presentation
PDF
Salesforce Lightning Components Workshop
PDF
How to build integrated, professional enterprise-grade cross-platform mobile ...
PDF
Test Case Creation in Katalon Studio
IMPACT/myGrid Hackathon - Introduction to Taverna
Android Intent.pptx
Android appwidget
Google analytics for iOS
AI: Integrate Search Function into Your App Using Bing Search API.
Ios actions and outlets
Flex Building User Interface Components
OpenSocial Intro
Lightning Components Workshop v2
Grails Advanced
Android tutorial (2)
React native app with type script tutorial
Creating web api and consuming- part 1
CloudBrew: Windows Azure Mobile Services - Next stage
Gaad01 job scheduler
Cómo tener analíticas en tu app y no volverte loco
iOS_Presentation
Salesforce Lightning Components Workshop
How to build integrated, professional enterprise-grade cross-platform mobile ...
Test Case Creation in Katalon Studio
Ad

Similar to Lecture exercise on activities (20)

PPT
Beginning Native Android Apps
PPTX
Android apps development
DOCX
Creation of simple application using - step by step
PPTX
AndroidLab_IT.pptx
PPTX
04 activities - Android
PPTX
Android Tutorials : Basic widgets
KEY
Android Workshop
DOCX
Exercises
DOCX
2. file internal memory
PDF
Android tutorial
PDF
android level 3
DOCX
4.preference management
PPT
Android Bootcamp Tanzania:understanding ui in_android
DOCX
Activity
DOCX
Activity
DOCX
Activity
DOCX
Activity
PDF
IT3681 - MOBILE_APPLICATIONS_DEVELOPMENT_LABORATORY (1).pdf
PDF
Basics and different xml files used in android
PPT
21 android2 updated
Beginning Native Android Apps
Android apps development
Creation of simple application using - step by step
AndroidLab_IT.pptx
04 activities - Android
Android Tutorials : Basic widgets
Android Workshop
Exercises
2. file internal memory
Android tutorial
android level 3
4.preference management
Android Bootcamp Tanzania:understanding ui in_android
Activity
Activity
Activity
Activity
IT3681 - MOBILE_APPLICATIONS_DEVELOPMENT_LABORATORY (1).pdf
Basics and different xml files used in android
21 android2 updated
Ad

More from maamir farooq (20)

DOCX
Ooad lab1
PPT
Lesson 03
PPT
Lesson 02
PDF
Php client libray
PDF
Swiftmailer
PDF
PDF
PPTX
PDF
PDF
J query 1.7 cheat sheet
PDF
Assignment
PDF
Java script summary
PDF
PDF
PPTX
PPTX
Css summary
DOCX
Manual of image processing lab
PDF
Session management
PDF
Data management
PPTX
Content provider
Ooad lab1
Lesson 03
Lesson 02
Php client libray
Swiftmailer
J query 1.7 cheat sheet
Assignment
Java script summary
Css summary
Manual of image processing lab
Session management
Data management
Content provider

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
Pharma ospi slides which help in ospi learning
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cardiovascular Pharmacology for pharmacy students.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
Week 4 Term 3 Study Techniques revisited.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

Lecture exercise on activities

  • 1. Tutorial on Activity Life Cycle and Activities 1. Create a newprojectinandroidand name it“ActivityLifeCycleAndActivities”andplease remembername yourxml file as“activitylifecycleandactivities.xml”.ItisyourfirstActivity. 2. Please addthe followingcode in“activitylifecycleandactivities.xml”. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="10dp" android:text="Hello World, MainActivity!"> </TextView> <Button android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Activity Two" android:id="@+id/btnActTwo"> </Button> <Button android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Contact View" android:id="@+id/buttonContact" /> <Button android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ChangeColor" android:id="@+id/buttonColor" /> </LinearLayout>
  • 2. 3. Addthe followingcode in onCreate functionof yourActivity“ActivityLifeCycleAndActivities” // intilization btnActTwo = (Button) findViewById(R.id.btnActTwo); btnContact= (Button) findViewById(R.id.buttonContact); btnColor= (Button) findViewById(R.id.buttonColor); container= (LinearLayout) findViewById(R.id.container); // link UI elements to action the code btnContact.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); btnColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); btnActTwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); 4. Run and Install the app and you should see the similar output as shown below.
  • 3. 5. Type the following line in the onCreatefunction of your current Activity. 6. Write the following code after the function of “OnCreate () as follows And then press the key “ctrl+o” ( Note, this only works in Android Sudio) and choose the method OnRestart as shown in the snapshots below.
  • 5. After step 6, your code will look like this. 7. After that add all functions of Activity Life cycle using the step 6. As shown below @Override protected void onRestart() { super.onRestart(); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "MainActivity: onStart()"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "MainActivity: onResume()");
  • 6. } @Override protected void onPause() { super.onPause(); Log.d(TAG, "MainActivity: onPause()"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "MainActivity: onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "MainActivity: onDestroy()"); } 8. Add the following line in the global space of your activity (it is the space write after your activity) 9. Now at least run the emulator and device first and click the “logcat” and choose “edit filter configuration”. 10. Add the following in the filter “states”.
  • 7. 11. You shouldsee the followinglinesinthe logcat afterrunningyourapplication Part 2. Creating Second Activity 1. Right click on your package folder as shown below and add a new Activity and name it as “ActivityTwo”.
  • 8. 2. Also add the xml file for “ActivityTwo” and name it “activitytwo” by choosing the following actions.
  • 9. 3. Add the following code in the “activitytwo.xml”. 4. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is Activity Two"> </TextView> </LinearLayout> 5. Go to the source folder of your Activity and add the following code. public class ActivityTwo extends Activity { final String TAG = "States"; @Override public void onCreate(Bundle savedInstanceState) {
  • 10. super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); Log.d(TAG, "ActivityTwo: onCreate()"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "ActivityTwo: onRestart()"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "ActivityTwo: onStart()"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "ActivityTwo: onResume()"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "ActivityTwo: onPause()"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "ActivityTwo: onStop()"); } protected void onDestroy() { super.onDestroy(); Log.d(TAG, "ActivityTwo: onDestroy()"); } } 6. Remove the errors if any 7. Now Go to the activity “ActivityLifeCycleAndActivities”andaddthe followingcode inthe listener. 8. //Button Listener for Activity Two btnActTwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ActivityTwo.class); startActivity(intent); } });
  • 11. 9. Now Run the Application and press the button “Go To Activity Two”. And you should see the following. ( If not, remove the logical errors by debugging) In the log cat. Part 3. Using onActivityFor Result 1. Add new activity as “ActivityThree” ( see step 1 on Part 2) 2. Add xml file in the layout folder of your project as “activitythree” and add the following code. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  • 12. android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_gravity="center_vertical" android:layout_weight="1" /> </LinearLayout> 3. Add the following code in the “ActivityThree”. 4. public class ActivityThree extends AppCompatActivity { ListView listV; ArrayList <String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activitythree); listV= (ListView) findViewById(R.id.listView); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list); listV.setAdapter(adapter); listV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } } 5. Create an ArrayList and add it after the ListView As shown in the ActivityThree 6. Add the following code in the listener.
  • 13. 7. Now go to the Activity “ActivityLifeCycleAndActivities” andaddthe followingcode inthe listener. btnColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ActivityThree.class); startActivityForResult(intent, PICK_COLOR ); } }); 8. Add the “PICK_COLOR” variable in the global space of this Activity. 9. Add the following function after the On Destroy function. Of this activity.
  • 14. public void changeBackground(String color) { // depending the extra value String, choose a background color if (color.equals("red")) { container.setBackgroundColor(Color.RED); } else if (color.equals("green")) { container.setBackgroundColor(Color.GREEN); } else if (color.equals("blue")) { container.setBackgroundColor(Color.BLUE); } } 10. Add the following line of code in the same Activity. ( Just write few onActi)
  • 15. 11. Remove the Error if any.Run your application and pressthe button“CHANGECOLOR”andselect Redfrom the ActivityThree andyoushouldsee yourbackgroundredinthe firstactivity.
  • 17. Part 4 showing the Contacts of your smartphones 1. Add the following code in the listener of your activity “ActivityLifeCycleAndActivities” 2. add the pickContact as shown. 3. copy and paste the following code in the “onActivityResult” Method. if ( requestCode==PICK_COLOR) { String clr=data.getStringExtra("COLOR"); changeBackground(clr); } // Check which request it is that we're responding to else if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER}; // Perform the query on the contact to get the NUMBER column // We don't need a selection or sort order (there's only one result for the given URI)
  • 18. // CAUTION: The query() method should be called from a separate thread to avoid blocking // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) // Consider using CursorLoader to perform the query. Cursor cursor = getContentResolver() .query(contactUri, projection, null, null, null); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String number = cursor.getString(column); // Do something with the phone number... } } 4. Run the application , you should see the output like this. After pressing the “contactView” Button.