Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:



HI, I am developing an app in which a calender is to be displayed in edit text.
How to display date picker for android with only month and year fields
It means when a user clicks on edit text a calender should be displayed with month and year only. Its urgent
Thank you.
Posted
Updated 9-Aug-12 19:22pm
v2

This code will be used for showing the name of month and hiding date

et1.setOnClickListener(new View.OnClickListener() {
			// @Override
			public void onClick(View v) {
				final Calendar c = Calendar.getInstance();
				
				int y = c.get(Calendar.YEAR)+4;
				int m = c.get(Calendar.MONTH)-2;
				int d = c.get(Calendar.DAY_OF_MONTH);
				final String[] MONTH = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

				DatePickerDialog dp = new DatePickerDialog(MainActivity.this,
						new DatePickerDialog.OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								String erg = "";
								erg = String.valueOf(dayOfMonth);
								erg += "." + String.valueOf(monthOfYear + 1);
								erg += "." + year;

								((TextView) et1).setText(erg);

							}

						}, y, m, d);
				dp.setTitle("Calender");
				dp.setMessage("Select Your Graduation date Please?");
				
				dp.show();
				
				
				
			}
		});
 
Share this answer
 
v2
A super easy way that I found to implement a DatePicker is to call it in xml:

<datepicker>
android:id="@+id/thePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

and then hide whichever field you want (in this case the year) in java:

picker = (DatePicker) findViewById(R.id.thePicker);
try {
Field f[] = picker.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearPicker")) {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(picker);
((View) yearPicker).setVisibility(View.GONE);
}
}
} catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
}}
 
Share this answer
 
Comments
Member 13248960 8-Jun-17 15:49pm    
I tried the code and didnt work for me. I tried printing the "field.getName()", it gave me four values - "mDelegate,mMode,MODe_CALENDAR,MODE_SPINNER".
In case you want to hide the date then use mdayPicker instead if myearpicker.
Similarly use mMonthPicker for hiding the month in DatePicker.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900