enter image description here I want to make such an alertDialog to select the month and year without selecting the day. Tell me the name of this component and how to implement it?

    2 answers 2

    the class is called DatePickerDialog, Google recommends implementing it through a fragment, or rather DialogFragment, to implement something like this:

    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); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int day) { Calendar c = Calendar.getInstance(); c.set(year, month, day); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = sdf.format(c.getTime()); } } 

    in order to remove the day - it is advisable to create your own class by taking from the samples: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/DatePickerDialog.java or after you manage to make a dialogue, try do this way:

     ((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE); 

      The class is called DatePickerDialog , and inside it is used DatePicker .

      The problem is that hiding the column with the day just will not work. Here it is necessary either through reflection to get a link to the private field inside the DatePicker and work with it, or take the code from the SDK sources and make it your own. With the first approach, there is a problem in that on a different version of the android DatePicker inside works differently. Here in this question there is a wonderful example of how this is solved in different versions.

      • I know about DatePickerDialog but this is not the case. In the studio, I found the NumberPicker but its design is not at all the same as in the picture. I basically can add 3 such NumberPickers and there will be the same date as in the picture. But the design is not the same. - user239760