Search code examples
androidandroid-fragmentsandroid-datepicker

Dialog onCreateDialog show error when pick up the DatePickerDialog


My code is working fine in Activity but when i used this code in fragment its not override the method Dialog onCreateDialog() , its shows 'Method does not override the method from itssuperclass'.

TestFragment.java:

public class TestFragment extends DialogFragment implements View.OnClickListener{
private ImageView calendarOne_ime;
Context context;
private TextView tv_dateone;
private int myear;
private int mmonth;
private int mday;
static final int DATE_DIALOG_ID = 999;
TestFragment dialogFragment;


public TestFragment()
{
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.activity_date_test, container, false);


     FragmentManager fm = getFragmentManager();
     dialogFragment = new TestFragment ();

    calendarOne_ime = (ImageView)rootview. findViewById(R.id.cone);
    tv_dateone = (TextView)rootview. findViewById(R.id.dateone_txt);
    calendarOne_ime.setOnClickListener(this);

    return rootview;


}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.cone:
            setCurrentDateOnView();
            addListenerOnButton();
            break;
    }
}


public void setCurrentDateOnView() {
    final Calendar c = Calendar.getInstance();
    myear = c.get(Calendar.YEAR);
    mmonth = c.get(Calendar.MONTH);
    mday = c.get(Calendar.DAY_OF_MONTH);
}
public void addListenerOnButton() {
    getActivity().showDialog(DATE_DIALOG_ID);
}
@Override
public Dialog onCreateDialog(int id){
    switch (id) {
        case DATE_DIALOG_ID:
            DatePickerDialog _date =   new DatePickerDialog(getActivity(), datePickerListener, myear,mmonth, mday){
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    if (year < myear)
                        view.updateDate(myear, mmonth, mday);
                    if (monthOfYear < mmonth && year == myear)
                        view.updateDate(myear, mmonth, mday);
                    if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                        view.updateDate(myear, mmonth, mday);
                } };
            return _date; }
    return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        myear = selectedYear;
        mmonth = selectedMonth;
        mday = selectedDay;
        // set selected date into textview
        // Toast.makeText(getApplicationContext(),new StringBuilder()  .append(mmonth + 1).append("-").append(mday).append("-") .append(myear).append(" "),Toast.LENGTH_LONG).show();
        tv_dateone.setText(new StringBuilder().append(mmonth + 1).append("/").append(mday).append("/").append(myear).append(" "));
    }
   };
 }

xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
    android:id="@+id/dateviewone_layout"
    android:layout_width="wrap_content"
    android:layout_weight="1.2"
    android:text="Area"

    android:gravity="center_vertical"

    android:layout_height="match_parent">


    <TextView
        android:id="@+id/dateone_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date"

        android:gravity="center_vertical"
        android:textStyle="bold"/>


    <ImageView
        android:id="@+id/cone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/appointment"
        android:layout_alignParentRight="true"

        android:layout_gravity="right"
        />





</RelativeLayout>

I also share the screen shot where i face the problem.enter image description here

Any idea how to solve it?


Solution

  • Use this

    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.support.v4.app.Fragment;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.DatePicker;
    import android.widget.ImageView;
    import android.widget.TextView;
    import com.pickcel.lite.agent.R;
    import java.util.Calendar;
    
    /**
     * A simple {@link Fragment} subclass.
     */
    public class TestFragment extends Fragment implements View.OnClickListener{
        private ImageView calendarOne_ime;
        Context context;
        public static TextView tv_dateone;
    
        public TestFragment()
        {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootview = inflater.inflate(R.layout.fragment_payment, container, false);
            calendarOne_ime = (ImageView)rootview. findViewById(R.id.cone);
            tv_dateone = (TextView)rootview. findViewById(R.id.dateone_txt);
            calendarOne_ime.setOnClickListener(this);
            return rootview;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.cone:
                    android.support.v4.app.DialogFragment newFragment = new SelectDateFragment();
                    newFragment.show(getFragmentManager(), "DatePicker");
                    break;
            }
        }
    
      public static class SelectDateFragment extends android.support.v4.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }
    
        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, mm+1, dd);
        }
        public void populateSetDate(int year, int month, int day) {
            tv_dateone.setText(month+"/"+day+"/"+year);
        }
    
       }
    }
    

    Output:
    enter image description here

    enter image description here