When you work with an interface that allows the user to enter date into a text box, it is a good idea to have a date dialog from which she/he can pick up a date easily. In this tip, I will show how to show a date dialog when a text box is focused. After a date is selected from the dialog, this date will be displayed in the text box.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txtdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Touch here to select a date"
/>
</RelativeLayout>
package com.example.andtip;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
@SuppressLint("ValidFragment")
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtdate;
public DateDialog(View view){
txtdate=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the dialog
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
//show to the selected date in the text box
String date=day+"-"+(month+1)+"-"+year;
txtdate.setText(date);
}
}
public void onStart(){
super.onStart();
EditText txtDate=(EditText)findViewById(R.id.txtdate);
txtDate.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasfocus){
if(hasfocus){
DateDialog dialog=new DateDialog(view);
FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
Posted by: Dara | post date: 05-17-2014 | Subject: Android Apps Development
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.