Dec 29, 2010

Intent of "MediaStore.ACTION_IMAGE_CAPTURE"

Using Intent of "MediaStore.ACTION_IMAGE_CAPTURE", we can request Android build-in Camera App or other Service Provider to take picture.

MediaStore.ACTION_IMAGE_CAPTURE

main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/captureimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call for ACTION_IMAGE_CAPTURE"
/>
<ImageView
android:id="@+id/imagecaptured"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


AndroidImageCapture.java
package com.AndroidImageCapture;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidImageCapture extends Activity {

ImageView imageiewImageCaptured;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonImageCapture = (Button)findViewById(R.id.captureimage);
imageiewImageCaptured = (ImageView)findViewById(R.id.imagecaptured);

buttonImageCapture.setOnClickListener(buttonImageCaptureOnClickListener);
}

Button.OnClickListener buttonImageCaptureOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);

}};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
imageiewImageCaptured.setImageBitmap(bmp);
}

}
}

Dec 28, 2010

Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets


Product Description

Mobile devices have evolved to focus on rich media production and consumption. Developers of mobile applications are able to create applications that allow people to play, capture, and share media in a variety of new ways on mobile devices. The popularity of Android has soared in part because the platform offers developers a rich set of capabilities including access to media capturing and playback functions.

Pro Android Media provides concise and clear instruction on how to utilize the media APIs made available through Android to create dynamic apps. It takes you from a simple means to gain access to the camera to complex video capture and sharing examples. It also covers sound, graphics, painting, and more—everything you need to make your app come "alive."

After reading this book, the app you create will showcase the best of multimedia that Android has to offer.

What you’ll learn

  • Develop graphics, music, video and rich media apps for Android smartphones and tablets
  • Build touchscreen input features into Android apps that allow users to draw, paint, and do other creative forms of input.
  • Turn the Android smartphone into a full fledged media player
  • How to integrate and use location based services and media related web service APIs

Who this book is for

This book is aimed primarily at the growing market of Android developers. It is written in such a way that it may be used by those who are familiar with Android, but have no experience developing applications that deal with images, audio, or video.



Dec 27, 2010

Clear the status bar notification

To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().

Clear the status bar notification

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/fire1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Fire Notification #1 -"
/>
<Button
android:id="@+id/fire2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Fire Notification #2 -"
/>
<Button
android:id="@+id/clear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Clear Notification #1 -"
/>
<Button
android:id="@+id/clear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Clear Notification #2 -"
/>
<Button
android:id="@+id/clearall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Clear ALL Notification -"
/>
</LinearLayout>


package com.AndroidStatusBarNotifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidStatusBarNotifications extends Activity {

private static final int ID_My_Notification_1 = 1;
private static final int ID_My_Notification_2 = 2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonFire1 = (Button)findViewById(R.id.fire1);
Button buttonFire2 = (Button)findViewById(R.id.fire2);
Button buttonClear1 = (Button)findViewById(R.id.clear1);
Button buttonClear2 = (Button)findViewById(R.id.clear2);
Button buttonClearAll = (Button)findViewById(R.id.clearall);
buttonFire1.setOnClickListener(buttonFire1OnClickListener);
buttonFire2.setOnClickListener(buttonFire2OnClickListener);
buttonClear1.setOnClickListener(buttonClear1OnClickListener);
buttonClear2.setOnClickListener(buttonClear2OnClickListener);
buttonClearAll.setOnClickListener(buttonClearAllOnClickListener);
}

private Button.OnClickListener buttonClearAllOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
mNotificationManager.cancelAll();
}};

private Button.OnClickListener buttonClear1OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
clearNotification(ID_My_Notification_1);
}};

private Button.OnClickListener buttonClear2OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
clearNotification(ID_My_Notification_2);
}};

private void clearNotification(int notification_id){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
mNotificationManager.cancel(notification_id);
}

private Button.OnClickListener buttonFire1OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
fireNotification(ID_My_Notification_1);
}};

private Button.OnClickListener buttonFire2OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
fireNotification(ID_My_Notification_2);
}};

private void fireNotification(int notification_id){
//Get a reference to the NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//Instantiate the Notification
int icon = android.R.drawable.ic_dialog_alert;
CharSequence tickerText = "Hello" + String.valueOf(notification_id);
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

//Define the Notification's expanded message and Intent
Context context = getApplicationContext();
CharSequence contentTitle = "My Notification" + String.valueOf(notification_id);
CharSequence contentText = "Hello My Notification!" + String.valueOf(notification_id);
//Intent notificationIntent = new Intent(AndroidStatusBarNotifications.this, AndroidStatusBarNotifications.class);
Intent notificationIntent = new Intent(getBaseContext(), AndroidStatusBarNotifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(AndroidStatusBarNotifications.this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

//Pass the Notification to the NotificationManager
mNotificationManager.notify(notification_id, notification);

}
}




Dec 26, 2010

Status Bar Notification

Status Bar Notification

A simple example to implement a Status Bar Notification

main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/fire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Fire Notification -"
/>
</LinearLayout>


AndroidStatusBarNotifications.java
package com.AndroidStatusBarNotifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidStatusBarNotifications extends Activity {

private static final int ID_My_Notification = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonFire = (Button)findViewById(R.id.fire);
buttonFire.setOnClickListener(buttonFireOnClickListener);
}

private Button.OnClickListener buttonFireOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//Get a reference to the NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

//Instantiate the Notification
int icon = android.R.drawable.ic_dialog_alert;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

//Define the Notification's expanded message and Intent
Context context = getApplicationContext();
CharSequence contentTitle = "My Notification";
CharSequence contentText = "Hello My Notification!";
//Intent notificationIntent = new Intent(AndroidStatusBarNotifications.this, AndroidStatusBarNotifications.class);
Intent notificationIntent = new Intent(getBaseContext(), AndroidStatusBarNotifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(AndroidStatusBarNotifications.this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

//Pass the Notification to the NotificationManager
mNotificationManager.notify(ID_My_Notification, notification);

}};
}




Dec 25, 2010

ProgressDialog

ProgressDialog
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0"
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -"
/>
</LinearLayout>


package com.AndroidProgressDialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class AndroidProgressDialog extends Activity {

Button buttonStart;
ProgressBar progressBar;
ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button)findViewById(R.id.start);
progressBar = (ProgressBar)findViewById(R.id.progressbar);

buttonStart.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonStart.setClickable(false);
new asyncTaskUpdateProgress().execute();
}

});

}

public class asyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {

int progress;

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
buttonStart.setClickable(true);
progressDialog.dismiss();
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress = 0;
progressDialog = ProgressDialog.show(AndroidProgressDialog.this, "ProgressDialog", "Running");
}

@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}

@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
while(progress<100){
progress++;
publishProgress(progress);
SystemClock.sleep(100);
}
return null;
}

}
}

Dec 20, 2010

Update ProgressBar inside AsyncTask

Update ProgressBar inside AsyncTask

<?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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ProgressBar
    android:id="@+id/progressbar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"
    android:progress="0"
    />
<Button
    android:id="@+id/start"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- Start -"
    />
</LinearLayout>


package com.AndroidProgressBar;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class AndroidProgressBar extends Activity {
 
 Button buttonStart;
 ProgressBar progressBar;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        buttonStart = (Button)findViewById(R.id.start);
        progressBar = (ProgressBar)findViewById(R.id.progressbar);
        
        buttonStart.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    buttonStart.setClickable(false);
    new asyncTaskUpdateProgress().execute();
   }
         
        });
        
    }
    
    public class asyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {

     int progress;
     
  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   buttonStart.setClickable(true);
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   progress = 0;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   // TODO Auto-generated method stub
   progressBar.setProgress(values[0]);
  }

  @Override
  protected Void doInBackground(Void... arg0) {
   // TODO Auto-generated method stub
   while(progress<100){
    progress++;
    publishProgress(progress);
    SystemClock.sleep(100); 
   }
   return null;
  }
     
    }
}


Related: Implement vertical ProgressBar

Dec 17, 2010

ProgressBar: progressBarStyleHorizontal

ProgressBar: progressBarStyleHorizontal


main.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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ProgressBar
    android:id="@+id/progressbar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"
    android:progress="50"
    />
</LinearLayout>


Related:


ProgressBar: default round shape

ProgressBar: default round shape

main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>




Dec 16, 2010

Interaction with RatingBar

Interaction with RatingBar

Override RatingBar.OnRatingBarChangeListener() to interact with RatingBar, assign rating via Rating.setRating() method.

The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) do not support user interaction and should only be used as indicators.


workspace/AndroidRating/res/layout/main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RatingBar
android:id="@+id/ratingbar1"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
/>
<RatingBar
android:id="@+id/ratingbar2"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleIndicator"
/>
<RatingBar
android:id="@+id/ratingbar3"
android:max="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyle"
/>
</LinearLayout>


workspace/AndroidRating/src/com/AndroidRating/AndroidRating.java
package com.AndroidRating;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

public class AndroidRating extends Activity {

final float MaxRating = 5.0f;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RatingBar ratingBar1 = (RatingBar)findViewById(R.id.ratingbar1);
final RatingBar ratingBar2 = (RatingBar)findViewById(R.id.ratingbar2);
RatingBar ratingBar3 = (RatingBar)findViewById(R.id.ratingbar3);

ratingBar3.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
// TODO Auto-generated method stub
ratingBar1.setRating(arg1);
ratingBar2.setRating(MaxRating - arg1);
}
});
}
}




Dec 15, 2010

RatingBar

RatingBar
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleIndicator"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyle"
/>
</LinearLayout>

Dec 14, 2010

ImageView

ImageView
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/icon"
/>
</LinearLayout>



Related:
- Load ImageView with JPG file in SD Card

Dec 13, 2010

Spinner

Spinner


workspace/AndroidSpinner/res/layout/main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


workspace/AndroidSpinner/src/com/AndroidSpinner/AndroidSpinner.java
package com.AndroidSpinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class AndroidSpinner extends Activity {

private static final String[] dayOfWeek =
{"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
private ArrayAdapter<String> adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner);

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dayOfWeek);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}



Related Post:
- Apply List to Spinner

Dec 9, 2010

TimePickerDialog

TimePickerDialog

workspace/TimePicker/res/layout/main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/starttimepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start TimePicker"
/>
</LinearLayout>


workspace/TimePicker/src/com/TimePicker/TimePicker.java
package com.TimePicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TimePicker extends Activity {

private int hour, minute;
static final int ID_TIMEPICKER = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button startTimePicker = (Button)findViewById(R.id.starttimepicker);
startTimePicker.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
showDialog(ID_TIMEPICKER);
}});
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_TIMEPICKER:
return new TimePickerDialog(this, timeSetListener, hour, minute, false);
default:
return null;
}
}

private TimePickerDialog.OnTimeSetListener timeSetListener
= new TimePickerDialog.OnTimeSetListener(){

@Override
public void onTimeSet(android.widget.TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
String.valueOf(arg1) + ":" + String.valueOf(arg2),
Toast.LENGTH_LONG).show();
}};
}

DatePickerDialog

DatePickerDialog
workspace/DatePicker/res/layout/main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startdatepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start DatePicker"
/>
</LinearLayout>


workspace/DatePicker/src/com/DatePicker/DatePicker.java
package com.DatePicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DatePicker extends Activity {

private int year, month, day;
static final int ID_DATEPICKER = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button startDatePicker = (Button)findViewById(R.id.startdatepicker);
startDatePicker.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}});
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_DATEPICKER:
return new DatePickerDialog(this, dateSetListener, year, month, day);
default:
return null;
}
}

private DatePickerDialog.OnDateSetListener dateSetListener
= new DatePickerDialog.OnDateSetListener(){

@Override
public void onDateSet(android.widget.DatePicker arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(),
String.valueOf(arg1) + "/"
+ String.valueOf(arg2+1) + "/"
+ String.valueOf(arg3),
Toast.LENGTH_LONG).show();
}
};
}

Dec 7, 2010

Create OptionsMenu using menu.add()


package com.OptionsMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionsMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 0, 0, "Option 0");
menu.add(0, 1, 1, "Option 1");
menu.add(0, 2, 2, "Option 2");
menu.add(0, 3, 3, "Option 3");
menu.add(0, 4, 4, "Option 4");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (0):
break;
case (1):
break;
case (2):
break;
case (3):
break;
case (4):
break;
}
return true;
}
}

Dec 5, 2010

Create OptionsMenu in /res/menu/menu.xml

Create OptionsMenu in /res/menu/menu.xml

/workspace/OptionsMenu/res/menu/menu.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/opt1"
android:title="Option 1" />
<item android:id="@+id/opt2"
android:title="Option 2" />
<item android:id="@+id/opt3"
android:title="Option 3" />
<item android:id="@+id/opt4"
android:title="Option 4" />
<item android:id="@+id/opt5"
android:title="Option 5" />
</menu>


/workspace/OptionsMenu/src/com/OptionsMenu/OptionsMenu.java
package com.OptionsMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionsMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (R.id.opt1):
break;
case (R.id.opt2):
break;
case (R.id.opt3):
break;
case (R.id.opt4):
break;
case (R.id.opt5):
break;
}
return true;
}
}


Related Post:
- Overwrite MENU key to create custom menu

Dec 2, 2010

AlertDialog.Builder - a simple way to create AlertDialog


eg.

package com.android.coding.HelloWorld;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(HelloWorld.this)
.setTitle("AlertDialog").setMessage("Hello!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}});
}
}

Dec 1, 2010

Make Android App in Landscape/Portrait

To force Android App working in Landscape/Portrait, you can modify the AndroidManifest.xml file, to add "android:screenOrientation" in <activity>.

It can be "unspecified", "user", "behind", "landscape", "portrait", "sensor" or "nosensor".

eg.

android:label="@string/app_name"
android:screenOrientation="landscape"
>










Make Android App in Landscape

Change backbround color of activity



Changing of backbround color of activity can be achieved by assigning color value to "android:background" of the layout.

eg.

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e0e0ff"
>





Change backbround color of activity

android:background may be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".



Infolinks In Text Ads