I am new to android word.
I am doing a text view spinner with time and date picker.
- How to set current time and date in the text view by default.
- After select time/date and click “set” button, why my time/date selection is empty
here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView
android:id="@+id/startTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.Holo.Spinner"
android:onClick="showTimePickerDialog"/>
<TextView
android:id="@+id/endTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.Holo.Spinner"
android:onClick="showTimePickerDialog"/>
<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.Holo.Spinner"
android:onClick="showDatePickerDialog"/>
here is my MainActivity.java
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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) {
// Do something with the date chosen by the user
}
}
}
Thank you.
The problem I see is that your
DatePickerFragmentandTimePickerFragmentsare static, so they cannot access your activity’s textviews. If they were not static, then all you’ll need to do istimeTextView.setText(...)inside ofonTimeSet(...)method. (By the way, keeping them static is good because you can reuse in another activity if you need to.)One way to get around this would be to create an extra interface and then pass that to your picker dialog fragments when you create them and then make a callback. But why do that when OnDateSetListener and OnTimeSetListener interfaces already exist?
I am going to refer only to the DatePickerFragment below, but all the same applies to the TimePickerFragment as well.
Your
MainActivityshould implementOnDateSetListener, not yourDatePickerFragment. One of the overriden methods from OnDateSetListener isonDateSet(...)where you set the date in your TextView. For example:Your
DatePickerFragmentshould have a constructor which takes anOnDateSetListeneras a parameter and holds a reference to it. See how to implement your DatePickerFragment here https://gist.github.com/2935353Now inside your
showDatePickerDialog(View v), pass your MainActivity (which is also an OnDateSetListener) like this:Then when the time is set, your MainActivity which implements ‘OnDateSetListener’ will get a callback and your textview will be updated.
Hope this clears things up a bit.
P.S. You should also have empty constructor in the
DatePickerFragment, it will prevent your app from crashing when the device is rotated.