• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help How to put datepicker in fragment

naighz

Lurker
Hello i got an error on codes. Tried to put date picker in my layout but it won't work if it using fragment.

Here the codes

JAVA

Java:
package com.example.kucingku;


import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

import androidx.fragment.app.Fragment;
import butterknife.Unbinder;


public class customer_booking_fragment_activity extends Fragment {

    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime;
    private int mYear, mMonth, mDay, mHour, mMinute;
    private Unbinder unbinder;

   

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.customer_booking_fragment_activity, container, false);

        btnDatePicker = (Button) view.findViewById(R.id.button7);
        btnTimePicker = (Button) view.findViewById(R.id.button8);

        txtTime =  (EditText) view.findViewById(R.id.in_time);

       // unbinder = ButterKnife.bind(this, view);

        EditText dep_date= (EditText ) view.findViewById(R.id.in_date);


        btnDatePicker.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
                // Get Current Date

                // TODO Auto-generated method stub
                //To show current date in the datepicker
                Calendar mcurrentDate=Calendar.getInstance();
                mYear=mcurrentDate.get(Calendar.YEAR);
                mMonth=mcurrentDate.get(Calendar.MONTH);
                mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(customer_booking_fragment_activity.this, new [COLOR=#ff0000]OnDateSetListener[/COLOR]() { [B][COLOR=#ff0000]//HERE IT DOES NOT RECOGNIZE[/COLOR][/B]
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                        // TODO Auto-generated method stub
                        /*      Your code   to get date and time    */
                        txtDate = (EditText) getActivity().findViewById(R.id.in_date);
                        //TextView tv = (TextView) getActivity().findViewById(R.id.textView);
                        txtDate.setText(selectedday+ "/" + selectedmonth +"/"+selectedyear);
                    }
                },mYear, mMonth, mDay);
                mDatePicker.getDatePicker().setCalendarViewShown(false);
                mDatePicker.setTitle("Select date");
                mDatePicker.show();


            }
        });

        return view;
    }

 


    }


}

XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CC99FF"
    tools:context=".customer_booking_fragment_activity">

    <FrameLayout
        android:id="@+id/frameLayout2"
        android:layout_width="447dp"
        android:layout_height="226dp"
        android:background="#FFFFFF"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline20">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="434dp"
            android:layout_height="247dp">

            <TextView
                android:id="@+id/textView9"
                android:layout_width="0dp"
                android:layout_height="25dp"
                android:gravity="center_vertical"
                android:text="Select Cat"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toTopOf="@+id/guideline29"
                app:layout_constraintEnd_toStartOf="@+id/guideline17"
                app:layout_constraintHorizontal_bias="0.058"
                app:layout_constraintStart_toStartOf="@+id/guideline50"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.659" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="139dp"
                android:layout_height="35dp"
                android:gravity="center_vertical"
                android:text="Select Package"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toTopOf="@+id/guideline49"
                app:layout_constraintEnd_toStartOf="@+id/guideline17"
                app:layout_constraintStart_toStartOf="@+id/guideline50"
                app:layout_constraintTop_toTopOf="@+id/guideline29" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="142dp"
                android:layout_height="34dp"
                android:layout_marginBottom="56dp"
                android:gravity="center_vertical"
                android:text="Select Service"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline17"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/guideline50"
                app:layout_constraintTop_toTopOf="@+id/guideline49"
                app:layout_constraintVertical_bias="1.0" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="169dp" />

            <Spinner
                android:id="@+id/catlist"
                android:layout_width="0dp"
                android:layout_height="32dp"
                app:layout_constraintBottom_toTopOf="@+id/guideline29"
                app:layout_constraintEnd_toStartOf="@+id/guideline56"
                app:layout_constraintStart_toStartOf="@+id/guideline17"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline29"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_begin="70dp" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline49"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_begin="142dp" />

            <Spinner
                android:id="@+id/servicelist"
                android:layout_width="0dp"
                android:layout_height="36dp"
                app:layout_constraintBottom_toTopOf="@+id/guideline49"
                app:layout_constraintEnd_toStartOf="@+id/guideline56"
                app:layout_constraintStart_toStartOf="@+id/guideline17"
                app:layout_constraintTop_toTopOf="@+id/guideline29" />

            <Spinner
                android:id="@+id/spinner4"
                android:layout_width="0dp"
                android:layout_height="32dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline56"
                app:layout_constraintStart_toStartOf="@+id/guideline17"
                app:layout_constraintTop_toTopOf="@+id/guideline49"
                app:layout_constraintVertical_bias="0.283" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="27dp" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline56"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="387dp" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="448dp"
        android:layout_height="246dp"
        android:background="#FFFFFF"
        app:layout_constraintBottom_toTopOf="@+id/buttonSubmit"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/frameLayout2">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="475dp"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView13"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="24dp"
                android:layout_marginBottom="40dp"
                android:text="Select Date"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toTopOf="@+id/textView14"
                app:layout_constraintEnd_toStartOf="@+id/guideline18"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/guideline51" />


            <TextView
                android:id="@+id/textView14"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:text="Reserve Time"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toTopOf="@+id/textView15"
                app:layout_constraintEnd_toStartOf="@+id/guideline18"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/guideline51" />

            <EditText
                android:id="@+id/in_time"
                android:layout_width="132dp"
                android:layout_height="35dp"
                android:layout_marginStart="8dp"

                android:layout_marginBottom="36dp"
                android:ems="10"
                android:hint="Time"
                android:inputType="textPersonName"
                android:textSize="14sp"
                app:layout_constraintBottom_toTopOf="@+id/radioButton"
                app:layout_constraintStart_toStartOf="@+id/guideline18" />

            <TextView
                android:id="@+id/textView15"
                android:layout_width="129dp"
                android:layout_height="27dp"
                android:layout_marginBottom="56dp"
                android:text="Time Format"
                android:textColor="@color/selecthitam"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline18"
                app:layout_constraintHorizontal_bias="0.278"
                app:layout_constraintStart_toStartOf="@+id/guideline51" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="167dp" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline51"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_end="445dp" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline57"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="421dp" />

            <EditText
                android:id="@+id/in_date"
                android:layout_width="134dp"
                android:layout_height="37dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="36dp"
                android:ems="10"
                android:hint="Date"
                android:inputType="textPersonName"

                android:textSize="14sp"
                app:layout_constraintBottom_toTopOf="@+id/in_time"
                app:layout_constraintStart_toStartOf="@+id/guideline18"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

            <Button
                android:id="@+id/button7"
                android:layout_width="86dp"
                android:layout_height="31dp"
                android:layout_marginTop="36dp"
                android:background="@color/colorPrimary"
                android:text="SELECT DATE"
                android:textColor="@color/colorFont"
                android:textSize="12sp"
                app:layout_constraintBottom_toTopOf="@+id/button8"
                app:layout_constraintEnd_toStartOf="@+id/guideline57"
                app:layout_constraintHorizontal_bias="0.461"
                app:layout_constraintStart_toEndOf="@+id/in_date"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.25" />

            <Button
                android:id="@+id/button8"
                android:layout_width="86dp"
                android:layout_height="31dp"
                android:layout_marginBottom="40dp"
                android:background="@color/colorPrimary"
                android:text="SELECT TIME"
                android:textColor="@color/colorFont"
                android:textSize="12sp"
                app:layout_constraintBottom_toTopOf="@+id/radioButton2"
                app:layout_constraintEnd_toStartOf="@+id/guideline57"
                app:layout_constraintHorizontal_bias="0.482"
                app:layout_constraintStart_toEndOf="@+id/in_time"
                tools:ignore="MissingConstraints" />

            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_marginBottom="56dp"
                android:gravity="center_vertical"
                android:text="AM"
                android:textAlignment="textStart"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/radioButton2"
                app:layout_constraintHorizontal_bias="0.21"
                app:layout_constraintStart_toStartOf="@+id/guideline18"
                tools:ignore="RtlCompat" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="133dp"
                android:layout_height="30dp"
                android:layout_marginBottom="56dp"
                android:gravity="center_vertical"
                android:text="PM"
                android:textAlignment="textStart"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/guideline57"
                tools:ignore="RtlCompat" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="351dp"
        android:layout_height="71dp"
        android:layout_marginBottom="24dp"
        android:background="@color/colorPrimary"
        android:text="@string/btn_submit"
        android:textColor="@color/colorFont"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="131dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
 
Back
Top Bottom