Datepicker: How to Popup Datepicker When Click on Edittext

Datepicker: How to popup datepicker when click on edittext

Try this in the XML file:

<EditText
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/birthday"/>

And this in the Java File:

public class MainActivity extends AppCompatActivity {
final Calendar myCalendar= Calendar.getInstance();
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.BirthDate);
DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH,month);
myCalendar.set(Calendar.DAY_OF_MONTH,day);
updateLabel();
}
};
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}

private void updateLabel(){
String myFormat="MM/dd/yy";
SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
editText.setText(dateFormat.format(myCalendar.getTime()));
}
}

Add android:focusable="false" within the xml file of the EditText to allow for a single touch.

Opening DatePicker in EditText click not showing at first click

Try this it will works on single click on edittext

MainActivity.java

public class MainActivity extends AppCompatActivity {

private DatePickerDialog mDatePickerDialog;
private EditText edDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edDate = (EditText) findViewById(R.id.activity_ed_date);

setDateTimeField();
edDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mDatePickerDialog.show();
return false;
}
});
}

private void setDateTimeField() {

Calendar newCalendar = Calendar.getInstance();
mDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy");
final Date startDate = newDate.getTime();
String fdate = sd.format(startDate);

edDate.setText(fdate);

}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

}
}

activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demoapp.MainActivity">

<EditText
android:id="@+id/activity_ed_date"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:focusable="false"
android:hint="Date"
android:imeOptions="actionNext"
android:maxLength="30" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />

</android.support.constraint.ConstraintLayout>

Sample Image

Kotlin:How to make a popup date pick when click the EditText

The default DatePicker looks like a calendar. You can change the behavior back to the old spinner style by adding this extra style in themes.xml.

<style name="MyDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>

and then inside your style for the app's theme, add this line:

<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>

Note that the calendar style is the default because it has been found to be preferred by most users.

DatePicker dialog with preselected value from edittext

Try this

Enter Date in MM/DD/YYYY Format and Click on Edittext so DatePicker popup with entered date.

MainActivity.java

package mydemo.com.datepiker;

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

String date = "";
EditText edittext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // Make sure user insert date into edittext in this format.
Date dateObject;
try {
String dob_var = (edittext.getText().toString());
dateObject = formatter.parse(dob_var);
date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);

String[] items1 = date.split("/");
String d1 = items1[0];
String m1 = items1[1];
String y1 = items1[2];
int d = Integer.parseInt(d1);
int m = Integer.parseInt(m1);
int y = Integer.parseInt(y1);

DatePickerDialog datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear + 1)
+ "/" + String.valueOf(year);
edittext.setText(date);
}
}, y, m - 1, d);
datePicker.show();

} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Exception is: ", e.toString());
}
}
});
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mydemo.com.datepiker.MainActivity">

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:hint="Enter Date:"
android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

Sample Image

Hope this may help you now.

Popup DatePicker for EditText

I have used setOnClickListener EditText method to show a DatePicker.

And then in xml file set EditText property android:focusable="false".So that you can avoid focus and virtual keyboards.

yourEditText.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To show current date in the datepicker
Calendar mcurrentDate=Calendar.getInstance();
mYear=mcurrentDate.get(Calendar.YEAR);
mMonth=mcurrentDate.get(Calendar.MONTH);
mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

DatePickerDialog mDatePicker=new DatePickerDialog(**YourActivityName**.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
}
},mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show(); }
});

try this one...comment your thoughts...thanks..

Open a DatePickerDialog on Click of EditText takes two clicks

I'll try to address your problem, but I am not completely sure about the first reason.

  1. The calendar opening only on the second click is because you are using an edittext. On the first click, your Edit Text will get focus. then the second click only calls the onClickListener.

    If you are not looking forward to edit the date set manually (using keyboard), then why not using a TextView to display the selected Date?

  2. The problem with the date not updating in editText is occurring because you are not setting the DateSetListener in your code. You need to set that to notify the system that a Date was set. The DateChange listener only returns the date while you are changing the date, and it doesn't appear that you are setting the date in the EditText.

Try this code:

            Calendar cal = Calendar.getInstance(TimeZone.getDefault());
DatePickerDialog datePicker = new DatePickerDialog(this,
R.style.AppBlackTheme,
datePickerListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));

datePicker.setCancelable(false);
datePicker.setTitle("Select the date");

return datePicker;
}
} catch (Exception e) {
showMsgDialog("Exception",
"An error occured while showing Date Picker\n\n"
+ " Error Details:\n" + e.toString(), "OK");
}
return null;
}


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

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
String year1 = String.valueOf(selectedYear);
String month1 = String.valueOf(selectedMonth + 1);
String day1 = String.valueOf(selectedDay);
TextView tvDt = (TextView) findViewById(R.id.tvDate);
tvDt.setText(day1 + "/" + month1 + "/" + year1);
}
};

In this, I am updating the date to a TextView with the ID "tvDate". I advise using a TextView instead of EditText and try this code.

Update

If you need to use EditText and load the calender in the first click, then try setting an onFocusListner to the editText instead of onClickListner.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// Show your calender here
} else {
// Hide your calender here
}
}
});


Related Topics



Leave a reply



Submit