5. public void showAlert(){
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
builder.setMessage("Unfortunately,the process com.android.phone has stopped");
builder.setPositiveButton("OK",null);
builder.setNegativeButton("Report", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//show Report Activity
}
});
builder.create().show();
}
by Eakapong Kattiya
AlertDialog :Alerts without title bar
Monday, July 15, 13
6. public void showAlertWithTitleBar(){
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle("Erase USB Storage ?");
builder.setMessage("You’ll lose all photos and media!");
builder.setPositiveButton("Erase", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do Report Activity
}
});
builder.setNegativeButton("Cancel", null);
builder.create().show();
}
by Eakapong Kattiya
AlertDialog :Alerts with title bar
Monday, July 15, 13
8. by Eakapong Kattiya
Text Fields : EditText
Single line &
Multi line
Text field types
Text selection
Monday, July 15, 13
9. AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_layer);
builder.setTitle("Prompt user input");
builder.setMessage("Please type a message.");
// Set an EditText view to get user input
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("AlertDialog","input text =" + input.getText().toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
by Eakapong Kattiya
AlertDialog : Prompt user input
Monday, July 15, 13
21. public void showPopupMenuWithIcon(){
final String [] items = new String[] {"From Gallery", "From Camera"};
final Integer[] icons = new Integer[] {android.R.drawable.ic_menu_gallery,
android.R.drawable.ic_menu_camera};
ListAdapter adapter = new ArrayAdapterWithIcon(this, items, icons);
ContextThemeWrapper wrapper = new ContextThemeWrapper(this,
android.R.style.Theme_Holo_Light_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
builder.setIcon(android.R.drawable.ic_menu_share);
builder.setTitle("Share Photo");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
//...
}
});
builder.create().show();
}
by Eakapong Kattiya
AlertDialog : Popup Menu With Icon
Monday, July 15, 13
31. by Eakapong Kattiya
Camera & Gallery
//Take a photo with camera
Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(intentCamera, "Take a with:"),MY_CAMERA_REQUEST);
//Choose photo from gallery
Intent intentGallery = new Intent(android.content.Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intentGallery,MY_CAMERA_REQUEST);
//Get Taken Photo from camera & gallery
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
}
}
Monday, July 15, 13
32. by Eakapong Kattiya
MediaPlayer : Play Raw Music File (background.mp3)
//Start Music
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.background);
mediaPlayer.start();
//Set Volume
AudioManager mAudioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
int current_volume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(current_volume, current_volume);
//Set Completion Listener
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
Monday, July 15, 13