Android Class to View Contact List and Call
The below Java code for Android application development is used to view the contact details of a contact from the contact list. This class contains the following list of functions to manage the contact details1. Set the Contact's Image width and height
2. Status view
3. Phone Number view
4. First Name and last Name Display setting
5. Display layout setting
6. Make Calling when the user is clicked
7. Bitmap setting
Java Code to View Contact List and Call
package edu.bu.CS893;
import java.net.URL;
import android.net.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.view.*;
/** A class that defines the view for the Contact list.
*
* @author Michael Braude
*/
public class ContactView extends LinearLayout implements
View.OnClickListener {
/** This is the width / height of the profile image.
*
*/
public static int IMAGE_SIZE = 50;
/** This is the contact that we are viewing.
*
*/
private Contact mContact;
/** True when the view is selected.
*
*/
private boolean mIsSelected;
/** This is the status view.
*
*/
private TextView mStatusView;
/** This is the phone number view.
*
*/
private TextView mPhoneNumberView;
/** This is the bitmap view that we use to draw the profile picture.
*
*/
private BitmapView mBitmapView;
/** Creates the contact view.
*
* @param context
* @param contact
*/
public ContactView(Context context, Contact contact) {
super (context);
this .mContact = contact;
// TODO display rating as stars
this .setOrientation(LinearLayout.HORIZONTAL);
// Add the profile image, then the phone # view:
mBitmapView = new BitmapView(context);
mBitmapView.setLayoutParams(new LayoutParams(IMAGE_SIZE,
IMAGE_SIZE));
this .addView(mBitmapView);
// This layout displays the first name / phone number:
LinearLayout phoneNumberLayout = new LinearLayout(context);
phoneNumberLayout.setOrientation(LinearLayout.VERTICAL);
phoneNumberLayout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView nameView = new TextView(context);
nameView.setText(mContact.getName());
nameView.setTextSize(16f);
nameView.setTextColor(Color.WHITE);
nameView.setLayoutParams(params);
phoneNumberLayout.addView(nameView);
mPhoneNumberView = new TextView(context);
mPhoneNumberView.setTextSize(16f);
mPhoneNumberView.setTextColor(Color.WHITE);
mPhoneNumberView.setLayoutParams(params);
phoneNumberLayout.addView(mPhoneNumberView);
mStatusView = new TextView(context);
mStatusView.setTextSize(16f);
mStatusView.setTextColor(Color.YELLOW);
mStatusView.setTypeface(Typeface
.defaultFromStyle(Typeface.ITALIC));
mStatusView.setLayoutParams(params);
phoneNumberLayout.addView(mStatusView);
this .addView(phoneNumberLayout);
// When this is clicked, call the contact!
this .setOnClickListener(this );
this .refreshFields();
this .setIsSelected(false);
}
/** Refreshes the contacts' fields.
*
*/
public void refreshFields() {
mPhoneNumberView.setText(mContact.getPhoneNumber());
String statusMessage = mContact.getStatusMessage();
if (statusMessage != null && statusMessage.length() > 0)
mStatusView.setText(String.format("%s %s", mContact
.getName(), statusMessage));
String profileURL = mContact.getProfilePictureURL();
if (profileURL != null && profileURL.length() > 0) {
try {
URL url = new URL(profileURL);
Bitmap bitmap = BitmapFactory.decodeStream(url
.openStream());
this .mBitmapView.setBitmap(bitmap);
} catch (Exception e) {
// Ignored
}
}
}
/** Called when the view is clicked. Dial the number!
*
* @param view
*/
public void onClick(View view) {
Intent i = new Intent();
Uri uri = Uri.parse("tel:" + mContact.getPhoneNumber());
i.setAction(Intent.CALL_ACTION);
i.setData(uri);
this .getContext().startActivity(i);
}
/** Returns the mIsSelected boolean.
*
*/
public boolean getIsSelected() {
return mIsSelected;
}
/** Sets the is selected value.
*
* @param isSelected
*/
public void setIsSelected(boolean isSelected) {
this .mIsSelected = isSelected;
this .setBackgroundColor((isSelected) ? Color.DKGRAY
: Color.BLACK);
}
/** This is a private class that is used to draw the users' profile
* picture on the contact view.
*
* @author Michael Braude
*
*/
private class BitmapView extends View {
/** This is the bitmap where we display the users' picture.
*
*/
private Bitmap mBitmap;
/** Default constructor.
*
*/
public BitmapView(Context context) {
super (context);
mBitmap = Bitmap.createBitmap(IMAGE_SIZE, IMAGE_SIZE, true);
}
/** Draws the bitmap to the screen.
*
*/
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
/** Returns the bitmap.
*
* @return
*/
public Bitmap getBitmap() {
return mBitmap;
}
/** Sets the bitmap.
*
* @param bitmap
*/
public void setBitmap(Bitmap bitmap) {
// Just in case....
if (bitmap == null)
return;
this.mBitmap = bitmap;
this.invalidate();
}
}
}
