Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android

 
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 9-Aug-12 17:53pm
Edited 9-Aug-12 19:22pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
				
				
				
			}
		});
		
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

A super easy way that I found to implement a DatePicker is to call it in xml:
 

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());
}}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In case you want to hide the date then use mdayPicker instead if myearpicker.
Similarly use mMonthPicker for hiding the month in DatePicker.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid