I have been trying to create a time picker in a dialog and I get an error in the fragment class saying:
The method is24HourFormat(Activity) is undefined for the type DateFormat in the following line.
In the line
DateFormat.is24HourFormat(getActivity()));
This is an example straight off the android developer website so I was not expecting any errors. I have shown the code below.
If someone could help me out on this it would be most appreciated.
Thanks
Code
package com.app.timer2;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import com.app.timer.R;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
}
Fragment
package com.app.timer2;
import java.text.DateFormat;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TimePicker;
public 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
}
}
XML
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical" >
<Button
android:id="@+id/start_time_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="10dp"
android:text="@string/start_time_button"
android:onClick="showTimePickerDialog" />
</LinearLayout>
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick the valid time for the day in our application. The time picker interface exists basically in two modes one is under XML layout and another is a dialog.
Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components.
You imported java.text.DateFormat
, you need to import android.text.format.DateFormat
instead... You're using the wrong class.
The reason this is happening is because you're using the wrong DateFormat.
You have:
import java.text.DateFormat;
When you want:
import android.text.format.DateFormat
In summary, these two objects have the same name but they are not the same object. So you have to specifically use the android provided one since its the only one that knows how to deal with Context
in method you are calling DateFormat.is24HourFormat(getActivity()));
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With