Monday, December 31, 2012

Create Dialog with options, using AlertDialog.Builder

Hi Guys!

In this tutorial I am sharing the code about the Alert Dialog with select option in android.
More details about the Alert Dialog visit the android developer site Alert Dialog

Lets start the coding part.

activty_main.xml






MainActivity.java

package com.sunil.alertdialogwithoption;

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

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

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

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

private void StartDialog(){
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("My Alert Dialog");
myAlertDialog.setMessage("It provide options for user to select");
myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_LONG).show();
}
});
myAlertDialog.setNeutralButton("Option 1", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Option 1' button clicked", Toast.LENGTH_LONG).show();
}
});
myAlertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_LONG).show();
}
});
myAlertDialog.show();
}

}

 You can download the source code Alert Dialog.

Cheers Guys! 

AutoCompleteTextView

Hi Guys!
An AutoComplete TextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.

FOr detail about the Auto Complete Text View please visit the android developer site AutoCompleteTextView.

Now lets start the coding about the auto text complete view . Here we are getting the item data in array from xml resource.And these array data stored in the adapter.

res/value/myvalues.xml



January
February
March
April
May
June
July
August
September
October
November
December


activity_main.xml









MainActivity.java

package com.sunil.autocompletetext;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity implements TextWatcher {

AutoCompleteTextView autoCompleteTextView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.input);
String[] month = getResources().getStringArray(R.array.month);
autoCompleteTextView.addTextChangedListener(this);
autoCompleteTextView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, month));
}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}
}

 You can download the source code Auto Complete Text View

Cheers Guys!

Friday, December 28, 2012

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.

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);
  }
   
 }
}
 

MediaStore.ACTION_IMAGE_CAPTURE
 Cheers Guys!!!!!!!!!!!

I love your comment here...
 

ProgressDialog

Hi Guys!

Today I am going to share the code about the ProgressDialog in android.
A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.
More About the progress dialog and progress bar then visit the android developer site Progress Dialog.

Now lets start the coding about the progress dialog in android that will execute inside the doInBackground() of Asynctask till.

activity_main.xml








MainActivity.java

package com.sunil.progressdialog;

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 MainActivity 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.activity_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 {
int progress1;

@Override
protected void onPostExecute(Void result) {
buttonStart.setClickable(true);
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progress = 0;
progressDialog = ProgressDialog.show(MainActivity.this, "ProgressDialog", "Running");
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
}
@Override
protected Void doInBackground(Void... arg0) {
while(progress1<100 data-blogger-escaped-null="" data-blogger-escaped-pre="" data-blogger-escaped-progress1="" data-blogger-escaped-publishprogress="" data-blogger-escaped-return="" data-blogger-escaped-systemclock.sleep=""> 
 You can download the source code Progress Dialog
 
Cheers Guys! 

Spinner

Hi Guys!!

Today I am sharing the code about the Spinner in android.
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
For detail please visit android developer site Spinner.

So lets start the coding about the spinner in android.

activity_main.xml









MainActivity.java

package com.sunil.spinner;

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

public class MainActivity extends Activity {

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

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

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

 You can download the source code Spinner. 

Cheers Guys!!

TimePickerDialog

Hi Guys!

A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second . The minutes can be entered by entering single digits.

For more details you can visit the android developer site Timer Picker
Now lets start the coding about the Time Picker android.

acivity_main.xml







MainActivity.java

package com.sunil.timepickerdialog;

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 MainActivity 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.activity_main);

Button startTimePicker = (Button)findViewById(R.id.starttimepicker);
startTimePicker.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {

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) {

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();
}};
}


 You can download the source code Time Picker

DatePickerDialog

Hi Guys!
Today I am going to share about the code of Date Picker in Android.
This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a CalendarView. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.
For detail about the Date Picker you can visit the android developer site Date Picker.

So lets start the coding about the date picker android.

activity_main.xml







MainAcivity.java

package com.sunil.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 MainActivity 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.activity_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) {
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) {

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


You can download the source code Date Picker

 

Copyright @ 2013 Android Developers Tipss.