user2059311
user2059311

Reputation: 355

Android: Time Picker Dialog

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>

Upvotes: 6

Views: 6764

Answers (2)

Gabe Sechan
Gabe Sechan

Reputation: 93698

You imported java.text.DateFormat, you need to import android.text.format.DateFormat instead... You're using the wrong class.

Upvotes: 16

jtt
jtt

Reputation: 13541

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()));.

Upvotes: 5

Related Questions