Search code examples
androiddatepickerdatetimepickertimepicker

Showing Date/TIme with Date/Time Picker Android


I am attempting to set the Date/Time the user selects from a Lollipop Date/Time Picker. The issue I am having is the date/time will set to the Button Text no problem. The problem is that it won't save in the string that it is in. I then want to use that string for something I know how to do.

DATEPICKER CLASS

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker

        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    public void onDateSet(DatePicker view, int year, int month, int day) {

        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);


        Date d = new Date(year, month, day);
        SimpleDateFormat sdf = new SimpleDateFormat("EE, MMM dd, yyyy");
       // String newDate = sdf.format(d);
        Toast.makeText(getActivity(), mMonth, Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), mDay, Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), mYear, Toast.LENGTH_LONG).show();

    }


}

FRAGMENT CLASS

public class Request extends Fragment {




    public TextView mCurrentUser;

    public Button Request_Button;
    public Button RButton_Date;
    public Button RButton_Time;

    public String cug;
    public String cu;
    public String time;
    public String date;

    //Request newInstance()
    public static Request newInstance() {

        Request fragment = new Request();
        return fragment;

    }

    //Needed empty Request Constructor
    public Request() {
    }

    //Public onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Make a view
        View rootview = inflater.inflate(R.layout.fragment_request, container, false);

        //Where you would initialize your variables
        RButton_Date =(Button)rootview.findViewById(R.id.Request_Button_Date);
        RButton_Time =(Button)rootview.findViewById(R.id.Request_Button_Time);
        Request_Button = (Button)rootview.findViewById(R.id.Request_Button_Request);

        //Current User Logged In
        this.mCurrentUser = (TextView)rootview.findViewById(R.id.Current_User_TV);
        date = new SimpleDateFormat("EE, MMM dd, yyyy").format(new Date().getTime());
        time = new SimpleDateFormat("h:mm a" ).format(new Date().getTime());
        RButton_Time.setText(time);
        RButton_Date.setText(date);

        //Showing current user

        final ParseUser mCurrentUser = ParseUser.getCurrentUser();
        if(mCurrentUser == null){
            String mCurUserNull = "No User Logged on";
            this.mCurrentUser.setText(mCurUserNull);

        }
        else{
            cu = mCurrentUser.get("FirstName").toString();
            cug = String.format("Hi! %s do you need a technician today?", cu);
            this.mCurrentUser.setText(cug);

        }


        //=================================================================

        //Create Dialog
        RButton_Date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do Stuff

                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");


            }
        });
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        };

        RButton_Time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DialogFragment picker1 = new TimePickerFragment();
                picker1.show(getFragmentManager(), "timePicker");

            }
        });

                Request_Button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Write Code for Gathering Variables and sending request email!!!
                        //Gather user info
                        //Populate email
                        //Send Username Request to [email protected]
                        Toast.makeText(getActivity(), cu + " Requested a tech at "+time
                                +", on "+date, Toast.LENGTH_SHORT).show();
                    }
                });




        //return rootview
        return rootview;
    }

I don't understand what it is I am doing wrong. It produces error of Resources unknown. I have googled, searched Stackoverflow extensively. Everything from 2014 and older just doesn't work.

GOAL

I just want to get date/time selected by user to be used as a string that can be set to text inside a button. I am using the example code from Google .

EDIT

ONDATESET

public void onDateSet(DatePicker view, int year, int month, int day) {
        Button DateButton = (Button) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.Request_Button_Date);
        String stringDueDateFrag = (month+1) + "/" + day + "/" + year + " ";
        DateButton.setText(stringDueDateFrag);

        // Set up a Calendar object to capture the user selected date in case the user wants
        // edit the date later.  Put the user selected date in a Bundle for onCreateDialog function to use.



    }

ONTIMESET

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Button TimeButton = (Button) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.Request_Button_Date);
        String stringDueDateFrag = hourOfDay + ":" + minute;
        TimeButton.setText(stringDueDateFrag);
    }

LOG

11-08 15:07:39.229 4922-4966/com.megliosolutions.bitsnbytes W/EGL_emulation: eglSurfaceAttrib not implemented
11-08 15:07:39.229 4922-4966/com.megliosolutions.bitsnbytes W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5486920, error=EGL_SUCCESS
11-08 15:07:59.658 4922-4929/com.megliosolutions.bitsnbytes W/art: Suspending all threads took: 15.332ms
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime: FATAL EXCEPTION: main
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime: Process: com.megliosolutions.bitsnbytes, PID: 4922
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.app.DatePickerDialog.resolveDialogTheme(DatePickerDialog.java:88)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:105)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at com.megliosolutions.bitsnbytes.Adapters.DatePickerFragment.onCreateDialog(DatePickerFragment.java:40)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-08 15:08:04.618 4922-4922/com.megliosolutions.bitsnbytes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-08 15:08:06.175 4922-4929/com.megliosolutions.bitsnbytes W/art: Suspending all threads took: 5.023ms

Solution

  • WORKING SOLUTION

    I have figured out how to get this working flawlessly.

    You need to make sure you make a separate class for both Time/Datepickers. However, don't use "implements OnDateSetListener" instead just leave the class the way it is or remove that, and implement OnDateSetListener in the activity/fragment you want to use it in.

    You can combine both in this way:

    implements OnDateSetListener, OnTimeSetListener...
    

    that will allow both to be used, but you must implement the needed methods. Then your good to go.

    Make sure you initialize everything properly, and place everything where it needs to be programmatically.