I'm learning about screen rotation for an App, I came across this example and I tried it.
My issue is that when I try this with a Button widget, it's text is still blank. When the button is clicked a date picker dialog appears to pick a date. But if the screen is rotated after the selection is made, the previously selected date is lost.
This is my code:
startDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setDate(startDate);
}
});
public void setDate(final Button B)
{
final Calendar calender = Calendar.getInstance();
day = calender.get(Calendar.DAY_OF_MONTH);
month = calender.get(Calendar.MONTH);
year = calender.get(Calendar.YEAR);
DatePickerDialog pickDate = new DatePickerDialog(add_event.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
calender.set(i,i1,i2);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MM-yyyy");
String dateString = dateFormat1.format(calender.getTime());
B.setText(dateString);
}
},year,month,day);
pickDate.show();
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("startDate", startDate.getText().toString());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
startDate.setText(savedInstanceState.getString("startDate"));
}
Here are some images:
Try using the version of onSaveInstanceState(Bundle outState) with just one argument. The other is for when you have an attribute set in the manifest for the activity to persist across reboots.
Related
So, I try to set an OnDismissListener on a dialog thing.
Datepicker dialog = new Datepicker(v);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface Dialog) {
mGoogleMap.clear();
setMap(mGoogleMap);
}
});
But it says
"Cannot resolve method 'setOnDismissListener(anonymous android.content.DialogInterface.OnDismissListener)'"
The Datepicker class extends DialogFragment, so it should have the setOnDismissListener method?
I've imported android.content.DialogInterface'. Is it confused on thenew DialogInterface.onDismissListener()` for some reason?
Edit: Someone asked for some of the Datepicker code. So here's the constructor and stuff. Let me know if you need anything else.
public class Datepicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
public Datepicker(View v){
txtDate = (EditText)v;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
You have to resolve this issue with a help of interface
public class Main3Activity extends AppCompatActivity implements DatePickerListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
//Initialise or create your dialog like this
DatePicker dialog = new DatePicker(v,this);
}
#Override
public void onDatePickerDismissed() {
//Here You receive the dialog dissmiss listner
mGoogleMap.clear();
setMap(mGoogleMap);
}
}
Create a interface class like
public interface DatePickerListener {
public void onDatePickerDismissed();
}
Change you dialog picker like this
#SuppressLint("ValidFragment")
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
DatePickerListener datePickerListener;
#SuppressLint("ValidFragment")
public DatePicker(View v, DatePickerListener _datePickerListener){
txtDate = (EditText)v;
this.datePickerListener = _datePickerListener;
}
public DatePicker() {
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(android.widget.DatePicker datePicker, int i, int i1, int i2) {
}
#Override
public void onDismiss(DialogInterface dialog) {
datePickerListener.onDatePickerDismissed();
super.onDismiss(dialog);
}
}
Hope it will help you :)
I am trying to set an editText with a DatePickerDialog inside a .setOnClickListener. where, when the user clicks on the editText the DatePickerDialog will appear and then after the user selects the desirable date it will set it on the editText... something like this : Android:DatePickerDialog on EditText Click Event
and here is my code mycode
Any ideas how to do that?
I tried the above example and many more but nothing seems to work inside the .SetOnClickListener
Use this code:
public class MainActivity extends AppCompatActivity {
EditText text;
private int year,currentYear;
private int month,currentMonth;
private int day,currentDay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.edit);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
currentDay = mDay;
currentMonth = mMonth+1;
currentYear = mYear;
text.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
}
},year,month,day).show();
}
});
}
}
I have store the user selected year, month, day in currentYear,currentMonth,currentDay respectively.
I am trying to follow along this tutorial: http://www.learn-android-easily.com/2013/01/using-timepickerdialog-and.html
However, showDialog cannot be resolved because I am using a fragment. How can I use a DateTime Picker Dialog inside of a class that extends a fragment?
Thanks.
EDIT: Show code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.date_and_time_picker);
// get the references of buttons
btnSelectDate=(Button)findViewById(R.id.buttonSelectDate);
btnSelectTime=(Button)findViewById(R.id.buttonSelectTime);
// Set ClickListener on btnSelectDate
btnSelectDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the DatePickerDialog
showDialog(DATE_DIALOG_ID);
}
});
// Set ClickListener on btnSelectTime
btnSelectTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the TimePickerDialog
showDialog(TIME_DIALOG_ID); //not resolved in fragment
}
});
}
This is working for me in a fragment
Time currDate = new Time(Time.getCurrentTimezone());
currDate.setToNow();
DatePickerDialog d = new DatePickerDialog(this, datePickerListener,
currDate.year, currDate.month, currDate.monthDay);
d.show();
private DatePickerDialog.OnDateSetListener datePickerListener =
new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
{
// Do as you need
}
};
I want to show month and year only, how i can do it? I have been try to search some solution but for me it's not work. May be i need to create my custom date picker dialog ? Here is my code:
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()
{
public int Syear;
public int Smonth;
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH,monthOfYear);
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// Auto-generated method stub
}
});
}
};
public void setDate()
{
new DatePickerDialog(InboxActivity.this,d,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
#Override
public void onClick(View v)
{
this.setDate();
}
May be this may work... not a tested solution though
import java.util.date;
SimpleDateFormat dateformat=new SimpleDateFormat("MM/yyyy");
dateformat.format(new Date());
I'm using multiple textviews with onclick listeners that direct me to a TimePicker Dialog.
However I cannot seem to know how to write the code after reading the tutorials,
textview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
textview2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
To show the dialog:
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, true);
}
return null;
}
and finally,
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
}
};
now, to .setText for the textview that CALLED the timepicker after the TimePickerDialog.OnTimeSetListener callback, how should I write the code?
The answer is very simple but I lack OOP knowledge and still learning.
Thanks...
You need to use a global varible. say,String fromWhere="";
now,
textview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fromWhere="textview1";
showDialog(TIME_DIALOG_ID);
}
});
textview2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fromWhere="textview2";
showDialog(TIME_DIALOG_ID);
}
});
...
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
if(fromWhere.equals(textview1))
// set hour and minute to according variable and textview
else if(fromWhere.equals(textview2))
// set hour and minute to according variable and textview
.. //so on for others
//mHour = hourOfDay;
//mMinute = minute;
fromWhere="";
}
};
create a member variable (say int whichViewClicked = 0) in your class to keep track of the textview clicked to bring-up the time-picker.
Now if you click textview1 set whichViewClicked = 1, if textview2 is clicked set whichViewClicked = 2 and so on. In the onTimeSet() method check the value of whichViewClicked and set the textview text accordingly
Use EditText and we can get time pikker long press on edit text.
EditText timeDisplay=(EditText)findViewById(R.id.timeDisplay);
timeDisplay.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});