Building a Mobile Employee Directory – Step 4: Pass data to DetailActivity to display more data and provide other functionality (w/ intent.putExtra)
For the source code relating to this post, checkout this Github repository.
Continuing with the mobile employee directory example, I’ve added the detail activity.
MainActivity.java
package com.himebaugh.employeedirectory; import java.util.List; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.View; import android.widget.ListView; import android.widget.TextView; // GOAL: Build a native android Mobile Employee Directory // ** The result is similar to the sample with Flex and Flash Builder // see http://www.adobe.com/devnet/flex/articles/employee-directory-android-flex.html // PURPOSE: Learning how to build an Android App. // Step 4: Pass data to DetailActivity to display more data and provide other functionality (w/ intent.putExtra) // 1) Create DetailActivity // 2) Create activity_detail.xml (in res/layout) // 3) Add DetailActivity to AndroidManifest.xml // 4) Add uses-permissions to AndroidManifest.xml // 5) Modify strings.xml (in res/values) // 6) Create mail.png, phone.png, sms.png (in res/drawable) // 7) Create employee_photo.jpg (in assets/pics) public class MainActivity extends ListActivity { public List<Employee> employees = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Parse xml data in a non-ui thread new LoadEmployeesTask().execute(); } private class LoadEmployeesTask extends AsyncTask<String, Void, Cursor> { @Override protected Cursor doInBackground(String... args) { // query the database and return a cursor of employees. EmployeeDatabase employeeDatabase = new EmployeeDatabase(getApplicationContext()); Cursor cursor = employeeDatabase.getAllEmployeesCursor(); return cursor; } @Override protected void onPostExecute(Cursor cursor) { String[] dataColumns = { EmployeeDatabase.COLUMN_FIRSTNAME, EmployeeDatabase.COLUMN_TITLE, EmployeeDatabase.COLUMN_DEPARTMENT, EmployeeDatabase.COLUMN_CITY, EmployeeDatabase.COLUMN_OFFICE_PHONE, EmployeeDatabase.COLUMN_MOBILE_PHONE, EmployeeDatabase.COLUMN_EMAIL, EmployeeDatabase.COLUMN_PICTURE }; int[] viewIDs = { R.id.list_item_name, R.id.list_item_title, R.id.list_item_department, R.id.list_item_city, R.id.list_item_office_phone, R.id.list_item_mobile_phone, R.id.list_item_email, R.id.list_item_picture }; SimpleCursorAdapter records = new SimpleCursorAdapter(getBaseContext(), R.layout.list_item, cursor, dataColumns, viewIDs, 0); setListAdapter(records); } } @Override protected void onListItemClick(ListView l, View view, int position, long id) { // get values from selected ListItem String empID = ((TextView) view.findViewById(R.id.list_item_emp_id)).getText().toString(); String name = ((TextView) view.findViewById(R.id.list_item_name)).getText().toString(); String title = ((TextView) view.findViewById(R.id.list_item_title)).getText().toString(); String department = ((TextView) view.findViewById(R.id.list_item_department)).getText().toString(); String city = ((TextView) view.findViewById(R.id.list_item_city)).getText().toString(); String officePhone = ((TextView) view.findViewById(R.id.list_item_office_phone)).getText().toString(); String mobilePhone = ((TextView) view.findViewById(R.id.list_item_mobile_phone)).getText().toString(); String email = ((TextView) view.findViewById(R.id.list_item_email)).getText().toString(); String picture = ((TextView) view.findViewById(R.id.list_item_picture)).getText().toString(); // Start new intent Intent intent = new Intent(getApplicationContext(), DetailActivity.class); intent.putExtra("empID", empID); intent.putExtra("name", name); intent.putExtra("title", title); intent.putExtra("department", department); intent.putExtra("city", city); intent.putExtra("officePhone", officePhone); intent.putExtra("mobilePhone", mobilePhone); intent.putExtra("email", email); intent.putExtra("picture", picture); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
DetailActivity.java
package com.himebaugh.employeedirectory; import java.io.IOException; import java.io.InputStream; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class DetailActivity extends Activity implements OnClickListener { public TextView mName; public TextView mTitle; public TextView mDepartment; public TextView mCity; private TextView mOfficePhone; private TextView mMobilePhone; private TextView mSms; private TextView mEmail; private ImageView mPicture; // XML node keys static final String KEY_ID = "empID"; static final String KEY_NAME = "name"; static final String KEY_TITLE = "title"; static final String KEY_DEPARTMENT = "department"; static final String KEY_CITY = "city"; static final String KEY_OFFICEPHONE = "officePhone"; static final String KEY_MOBILEPHONE = "mobilePhone"; static final String KEY_EMAIL = "email"; static final String KEY_PICTURE = "picture"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); // implement Up Navigation with caret in front of App icon in the Action Bar getActionBar().setDisplayHomeAsUpEnabled(true); // getting intent data Intent intent = getIntent(); // Update values on the screen with XML values from previous intent mName = (TextView) findViewById(R.id.activity_detail_name); mTitle = (TextView) findViewById(R.id.activity_detail_title); mDepartment = (TextView) findViewById(R.id.activity_detail_department); mCity = (TextView) findViewById(R.id.activity_detail_city); mOfficePhone = (TextView) findViewById(R.id.activity_detail_office_phone); mMobilePhone = (TextView) findViewById(R.id.activity_detail_mobile_phone); mSms = (TextView) findViewById(R.id.activity_detail_sms); mEmail = (TextView) findViewById(R.id.activity_detail_email); mPicture = (ImageView) findViewById(R.id.activity_detail_picture); mName.setText(intent.getStringExtra(KEY_NAME)); mTitle.setText(intent.getStringExtra(KEY_TITLE)); mDepartment.setText(intent.getStringExtra(KEY_DEPARTMENT)); mCity.setText(intent.getStringExtra(KEY_CITY)); mOfficePhone.setText(intent.getStringExtra(KEY_OFFICEPHONE)); mMobilePhone.setText(intent.getStringExtra(KEY_MOBILEPHONE)); mSms.setText(intent.getStringExtra(KEY_MOBILEPHONE)); mEmail.setText(intent.getStringExtra(KEY_EMAIL)); // listen for button clicks findViewById(R.id.activity_detail_call_office_button).setOnClickListener(this); findViewById(R.id.activity_detail_office_phone).setOnClickListener(this); findViewById(R.id.activity_detail_call_mobile_button).setOnClickListener(this); findViewById(R.id.activity_detail_mobile_phone).setOnClickListener(this); findViewById(R.id.activity_detail_send_sms_button).setOnClickListener(this); findViewById(R.id.activity_detail_sms).setOnClickListener(this); findViewById(R.id.activity_detail_send_email_button).setOnClickListener(this); findViewById(R.id.activity_detail_email).setOnClickListener(this); // add PhoneStateListener FROM http://www.mkyong.com/android/how-to-make-a-phone-call-in-android/ PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); InputStream is; try { is = getAssets().open("pics/" + intent.getStringExtra(KEY_PICTURE)); Bitmap bit = BitmapFactory.decodeStream(is); mPicture.setImageBitmap(bit); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the options menu from XML MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { // This is called when the Home (Up) button is pressed in the Action Bar. Intent parentActivityIntent = new Intent(this, MainActivity.class); parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(parentActivityIntent); finish(); return true; } return super.onOptionsItemSelected(item); } // @Override public void onClick(View view) { switch (view.getId()) { case R.id.activity_detail_call_office_button: case R.id.activity_detail_office_phone: // clicking on textView will have same action as the button Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:" + mOfficePhone.getText().toString())); startActivity(callIntent); break; case R.id.activity_detail_call_mobile_button: case R.id.activity_detail_mobile_phone: // clicking on textView will have same action as the button Intent callIntent2 = new Intent(Intent.ACTION_CALL); callIntent2.setData(Uri.parse("tel:" + mMobilePhone.getText().toString())); startActivity(callIntent2); break; case R.id.activity_detail_send_sms_button: case R.id.activity_detail_sms: // clicking on textView will have same action as the button Intent smsIntent = new Intent(Intent.ACTION_SENDTO); smsIntent.setData(Uri.parse("smsto:" + mMobilePhone.getText().toString())); // smsIntent.putExtra("sms_body", "Hello " + mName.getText().toString() ); startActivity(smsIntent); break; case R.id.activity_detail_send_email_button: case R.id.activity_detail_email: // clicking on textView will have same action as the button String to = mEmail.getText().toString(); String subject = "textSubject"; String message = "textMessage"; Intent email = new Intent(Intent.ACTION_SEND); email.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); email.putExtra(Intent.EXTRA_SUBJECT, subject); email.putExtra(Intent.EXTRA_TEXT, message); // prompts email apps email.setType("message/rfc822"); startActivity(Intent.createChooser(email, "Choose an Email client :")); break; } } // monitor phone call activities private class PhoneCallListener extends PhoneStateListener { private boolean isPhoneCalling = false; String LOG_TAG = "LANGDON"; @Override public void onCallStateChanged(int state, String incomingNumber) { if (TelephonyManager.CALL_STATE_RINGING == state) { // phone ringing Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); } if (TelephonyManager.CALL_STATE_OFFHOOK == state) { // active Log.i(LOG_TAG, "OFFHOOK"); isPhoneCalling = true; } if (TelephonyManager.CALL_STATE_IDLE == state) { // run when class initial and phone call ended, // need detect flag from CALL_STATE_OFFHOOK Log.i(LOG_TAG, "IDLE"); if (isPhoneCalling) { Log.i(LOG_TAG, "restart app"); // restart app Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); isPhoneCalling = false; } } } } }
activity_detail.xml (in res/layout)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.himebaugh.employeedirectory" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="top|left" > <ImageView android:id="@+id/activity_detail_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:contentDescription="@string/employee_photo" android:minHeight="150dp" android:minWidth="150dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/activity_detail_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:layout_toRightOf="@+id/activity_detail_picture" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/activity_detail_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_name" android:layout_below="@+id/activity_detail_name" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/activity_detail_department" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_name" android:layout_below="@+id/activity_detail_title" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/activity_detail_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_name" android:layout_below="@+id/activity_detail_department" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/activity_detail_picture" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text="@string/detail_header1" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/activity_detail_office_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView5" android:layout_below="@+id/textView5" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_office_phone" android:layout_below="@+id/activity_detail_office_phone" android:layout_marginTop="25dp" android:text="@string/detail_header2" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/activity_detail_mobile_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView7" android:layout_below="@+id/textView7" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_mobile_phone" android:layout_below="@+id/activity_detail_mobile_phone" android:layout_marginTop="25dp" android:text="@string/detail_header3" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/activity_detail_sms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView9" android:layout_below="@+id/textView9" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/activity_detail_sms" android:layout_below="@+id/activity_detail_sms" android:layout_marginTop="25dp" android:text="@string/detail_header4" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/activity_detail_email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView11" android:layout_below="@+id/textView11" android:text="@string/place_holder" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageButton android:id="@+id/activity_detail_call_office_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView5" android:layout_marginRight="10dp" android:src="@drawable/phone" /> <ImageView android:id="@+id/activity_detail_call_mobile_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView7" android:layout_marginRight="10dp" android:clickable="true" android:src="@drawable/phone" /> <ImageView android:id="@+id/activity_detail_send_sms_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView9" android:layout_marginRight="10dp" android:clickable="true" android:src="@drawable/sms" /> <ImageView android:id="@+id/activity_detail_send_email_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView11" android:layout_marginRight="10dp" android:clickable="true" android:src="@drawable/mail" /> </RelativeLayout> </ScrollView>
strings.xml (in res/values)
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Employee Directory</string> <string name="employee_details">Employee Details</string> <string name="employee_photo">Employee Photo</string> <string name="detail_header1">Call Office</string> <string name="detail_header2">Call Mobile</string> <string name="detail_header3">SMS</string> <string name="detail_header4">Email</string> <string name="place_holder">Text will be replaced</string> <string name="action_settings">Settings</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.himebaugh.employeedirectory" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.himebaugh.employeedirectory.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Single List Item View --> <activity android:name="com.himebaugh.employeedirectory.DetailActivity" android:label="@string/employee_details" > </activity> </application> </manifest>
Employee.java
- unchanged see Step 3
EmployeeDatabase.java
- unchanged see Step 3
EmployeeXmlParser.java
- unchanged see Step 3
activity_main.xml (in res/layout)
- unchanged see Step 3
list_item.xml (in res/layout)
- unchanged see Step 3
employee_list.xml (in res/xml)
- unchanged see Step 3
The app will run without these, but create these if you wish…
Create mail.png, phone.png, sms.png (in res/drawable)
Create employee_photo.jpg (in assets/pics)
Screenshots of the application
Category: Android Application Development
The code has been place on GitHub. Download at https://github.com/langhimebaugh/EmployeeDirectory/tree/Step-4
thank you very much sir, it’s working well from the step 1 until 7. (just a thing I do not know how to make it appear in the main screen as the last image of step 7)
until now I have not found a tutorial as useful as this one, waiting for the other tutorial in the future from you sir.