How to make a datepicker dialog in sketchware



To create a DatePickerDialog in Sketchware android project then follow the steps given below;
1. In VIEW area of your sketchware android project, insert a LinearH and inside it insert a TextView textview1 , an EditText edittext1 and an ImageView imageview1 .
For textview1 write text as 'Date: '. For edittext1 write hint as dd/mm/yyyy and deselect 'enabled' in it's properties.
2. Choose image of a Calendar using image manager and set it as image of imageview1.


3. In LOGIC area add a new String variable date .


4. In onCreate event, insert an add source directly block and put following code in it:
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
int mon = month +1;
date = day + "/" + mon + "/" + year;
edittext1.setText(date);
}

5. Add a new event imageview1 onClick . In the event insert an add source directly block and put following code:
showDatePickerDialog(imageview1);


6. Save and run the project. Now on clicking the ImageView a DatePickerDialog will appear.

Post a Comment

2 Comments

  1. This is not working in Android Oreo and onwards versions.
    Fix this problem please.

    ReplyDelete
    Replies
    1. first make sure your sketchware is up to date, and is sketchware not working on your phone or the tutorial is not working on sketchware, i will update the the tutorial now with images

      Delete

Please comment on our page thank you