Zafar Kurbonov
Zafar Kurbonov

Reputation: 2077

Material DatePicker is not working in Fragments

Used compile 'com.wdullaer:materialdatetimepicker:3.0.0' material design from github to support similar designs for android version > 4.

In Activity, it is working. But in Fragments it is not.

public class Abs extends AppCompatActivity implements com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener {

//onCreate method



 button.setOnClickListener(new OnClickListener){
    //Edittexts
    //Edittexts
    startActivity(new Intent(Abs.this, Random.class))
         }
    }



imgdob.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar now = Calendar.getInstance();
            DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(Abs.this, now.get(Calendar.YEAR),now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.show(getFragmentManager(), "DatePicker");
        }
    });
}

@Override
public void onDateSet(datePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
    edtxdOb.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
}

Similar code is implemented in fragment, but instead of Abs.this I have used getActivity() and getContext() both of them didnt work.

Upvotes: 4

Views: 2288

Answers (1)

Zafar Kurbonov
Zafar Kurbonov

Reputation: 2077

Finally, found the solution.

 DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(**FragmentClassName**.this,  //Your Fragment Class Name should be here, not getActivity(), or getContex().
        now.get(Calendar.YEAR),
        now.get(Calendar.MONTH), 
        now.get(Calendar.DAY_OF_MONTH));
 datePickerDialog.show(getActivity().getFragmentManager(), "DatePicker");

Upvotes: 1

Related Questions