Search This Blog

Wednesday, February 8, 2012

Date Picker & Time Picker Dialog and set it into the String Format


First of all make an simple interface to open the Dialog and make that dialog open on the button click. For that you have to take two text view for storing the date and time and two button for opening the date and time dialog.


<?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:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
/>
    <Button 
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Date"
     />
      <TextView  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview2"
    android:text=""
    android:textSize="30dp"
    />
     <Button 
     android:id="@+id/button2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="time"
     />
</LinearLayout>




Now make a java file with the DatePickerDemo and put the following code into it.



import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;


public class DatePickerDemo extends Activity {



private static final int DATE_DIALOG_ID=1; // for date 
private static final  int TIME_DIALOG_ID=2; // for month 
private int d,mo,y,h,m; // for date & month variables 
Button b1,b2; // button objects 
TextView e1,e2;  // textview objects

// execution starts from here 
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.datepicker);  // calling main.xml 
   e1=(TextView)findViewById(R.id.textview1); // getting textview1 id from main.xml 
     b1=(Button)findViewById(R.id.button1); // getting  button id from main.xml
         
b1.setOnClickListener(new OnClickListener() // setting listener for button one  
        
{

@Override
public void onClick(View arg0) {


// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);   // generating dialog box 
}
});
        
final Calendar cal=Calendar.getInstance(); // allocating memory for calendar instance
d=cal.get(Calendar.DAY_OF_MONTH); // present date        
mo=cal.get(Calendar.MONTH); // present month
    y=cal.get(Calendar.YEAR); // present year
    updateDate();  // update date 
        
b2=(Button)findViewById(R.id.button2); // getting listener for button2 
   e2=(TextView)findViewById(R.id.textview2); 
   b2.setOnClickListener(new OnClickListener() // setting listener for button2 
        
{
@Override
public void onClick(View arg0) {


// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
        
h=cal.get(Calendar.HOUR); // getting  present hour & minute
m=cal.get(Calendar.MINUTE);
updateTime();  // updating time 
}


public void updateTime()
{
 
e2.setText(new StringBuilder().append(h).append('-').append(m));
 
}
public void updateDate()
{
 
e1.setText(new StringBuilder().append(d).append('-').append(mo+1).append('-').append(y));
 
}


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


@Override
public void onDateSet(DatePicker view,int year, int monthofyear, int day) 
{

                     y=year;
mo=monthofyear;
                  d=day;
updateDate();
}
};
 
private TimePickerDialog.OnTimeSetListener timelistener=new TimePickerDialog.OnTimeSetListener() 
{
@Override
public void onTimeSet(TimePicker view, int hourofday, int minute) 
{
// TODO Auto-generated method stub
h=hourofday;
m=minute;
updateTime();
}
};

protected Dialog onCreateDialog(int id) 
{
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this,datelistener , y, mo, d);


case TIME_DIALOG_ID:
return new TimePickerDialog(this,timelistener,h,m,false);
}
return null;
}
}


This will show you how to pick the date from date picker android widget and time from TimePicker widget and convert Those date and Time into String format and  set it to the TextView.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.