Skip to content Skip to sidebar Skip to footer

Set TextView To Time From TimePicker - Android

I have been using this tutorial here to create a time picker. I can select the time fine, but I can't get the time selected to show up in my TextView. Java is not my preferred la

Solution 1:

you can do it like that:

1) Solution 1 (better):

public class MainActivity extends Activity {

        TextView resultText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            resultText = (TextView)findViewById(R.id./*YOUR TEXT VIEW ID*/);
        }

        //code from: http://developer.android.com/guide/topics/ui/controls/pickers.html
        public void showTimePicker(View v) {
            DialogFragment newFragment = new TimePickerFragment(resultText);
            newFragment.show(getFragmentManager(), "timePicker");
        }

        public class TimePickerFragment extends DialogFragment
                implements TimePickerDialog.OnTimeSetListener {

             TextView mResultText;

             public TimePickerFragment(TextView textView) {
                mResultText = textView;
            }

            @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) {
                String time = /*CONVERT YOUR TIME FROM hourOfDay and minute*/;
                mResultText.setText(time);
            }
        }
    }

2) Solution 2:

public class MainActivity extends Activity {
    public TextView mResultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mResultText = (TextView)findViewById(R.id./*YOUR TEXT VIEW ID*/);
    }

    //code from: http://developer.android.com/guide/topics/ui/controls/pickers.html
    public void showTimePicker(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "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) {
            String time = /*CONVERT YOUR TIME FROM hourOfDay and minute*/;
            mResultText.setText(time);
        }
    }
}

Solution 2:

You can actually use findViewById, as with getActivity() your fragment gets access to the activity it is running in and thus to the views inside the activity.

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    TextView textView = (TextView) getActivity().findViewById(R.id.my_text_view);
    textView.setText(String.format("%02d", hourOfDay), String.format("%02d", minute);
}

This simple solution should be preferred over passing a reference to the TextView to the fragment, as this reference will be lost on reinstantiation of the fragment (see comment).


Post a Comment for "Set TextView To Time From TimePicker - Android"