buddy123
buddy123

Reputation: 6317

How to read TimePicker chosen values?

I followed this guide: http://developer.android.com/guide/topics/ui/controls/pickers.html im not quite sure what goes where with this (im faily new to android dev) I have a package with both my mainActivity class and the timepicker class. i want that when time is set, to write it to mainactivity's variables, or the other way around, to do that "onTimeSet" saves values as class variables and read those vars from my mainActivity. How do i do that?

Here's my fragment class:

package com.example.remoteswitch;

import java.util.Calendar;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    int hour, min;
    @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, true);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        hour = hourOfDay;
        min = minute;

    }
}

from my MainActivity class, I have a function that is called when a button is pressed:

public void showTimePickerDialog(View v) {
            //DialogFragment newFragment = new TimePickerFragment();
            TimePickerFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker"); 

            //todo: read values from timePicker
            int pickerHour = 0, pickerMin = 0;
            //some manipulations on this data
            }
        }

Upvotes: 4

Views: 8481

Answers (2)

VVB
VVB

Reputation: 7641

Use below answer. This worked for me.

Interface Class:

public interface OnTimeSetListener {
    void onTimeSet(String time);
}

Activity Class onClick:

        DialogFragment newFragment = new TimePickerFragment(getActivity(), this);
        newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");

Overide the interface listener method:

    @Override
    public void onTimeSet(String time) {
        // TODO Auto-generated method stub
//do something here to set the values
    }

Upvotes: 0

Sam
Sam

Reputation: 86948

Rewrite
I combined both of the examples from the Developer's Guides that we discussed. Now you should be able to understand how to send the time that user chooses from the TimePickerFragment back to the Activity where it is more useful. (I made some changes from the first example.)

TimePickerFragment:

public class TimePickerFragment extends DialogFragment {
    // Notice I removed "implements OnTimeSetListener" and changed the variables

    private Activity mActivity;
    private OnTimeSetListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;

        // This error will remind you to implement an OnTimeSetListener 
        //   in your Activity if you forget
        try {
            mListener = (OnTimeSetListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it

        // I made a couple changes here!
        return new TimePickerDialog(mActivity, mListener, hour, minute,
                DateFormat.is24HourFormat(mActivity));
    }
}

MainActivity:

// add "implements OnTimeSetListener" to "public class MainActivity ..."
private int pickerHour = 0;
private int pickerMin = 0;

// onCreate() and your other methods...

public void showTimePickerDialog(View v) {
    TimePickerFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker"); 
}    

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    pickerHour = hourOfDay;
    pickerMin = minute;
}

Upvotes: 14

Related Questions