Majstor
Majstor

Reputation: 879

How to create a TimePicker from class which do not extends Activity, Android?

I'm trying to create TimePicker dialog from a class that doesnt extends the Activity class. I need TimePicker in a class called EnterTime which has Context attribute containing Activity Context.

All examples of TimePicker on the WEB are basic ones written in Activity class and that's why they confusing me.

I want time picker which works good on Android lower then 3.0. I don't use XML layout.

So basically I need suggestion where to put onCreateDialog() , how to retrieve chosen time, etc.

I've EditText which have onClickListener() for calling a TimePicker.

Upvotes: 0

Views: 761

Answers (1)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

may be this one help you

public class MyTimePicker {
TimePickerDialog mTimePickerDialog;

public interface onTimeSet {
    public void onTime(TimePicker view, int hourOfDay, int minute);
}

onTimeSet mOnTimeSet;

public void setTimeListener(onTimeSet onTimeset) {
    mOnTimeSet = onTimeset;
}

public MyTimePicker(Context ctx) {
    mTimePickerDialog = new TimePickerDialog(ctx, new OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mOnTimeSet.onTime(view, hourOfDay, minute);

        }
    }, 1, 1, true);
}

public void show() {
    mTimePickerDialog.show();
}

and call this

public void ShowTimePicker() {
    MyTimePicker myTimePicker = new MyTimePicker(this);
    myTimePicker.show();
    myTimePicker.setTimeListener(new onTimeSet() {

        @Override
        public void onTime(TimePicker view, int hourOfDay, int minute) {
            Toast.makeText(MainActivity.this,
                    "time is " + hourOfDay + ":" + minute,
                    Toast.LENGTH_LONG).show();

        }
    });
}

Upvotes: 3

Related Questions