Reputation: 835
I have a Datepicker in a custom Dialog which shows the date and the user can select the date and I get the user selected date. Now I want to go further by showing other calendar
for e.g. Persian calendar date instead of the Gregorian calender date. Now in my app when the user selects the date from the datepicker I can return the Persian date in the log. but I don't know how to change the DatePicker to show perisan date.please help.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Add Detail").setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
final EditText amount = (EditText) dialog.findViewById(R.id.amtEditText);
final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.datePicker1);
Button cancelButton = (Button) dialog.findViewById(R.id.canelButton);
Button OKButton = (Button) dialog.findViewById(R.id.OKbutton);
OKButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("---------------", datePicker.getDayOfMonth() +" "
+ datePicker.getMonth() + " "
+ datePicker.getYear() + " "
+ amount.getText());
Toast.makeText(MainActivity.this, "Got click: "
+ datePicker.getDayOfMonth() + " "
+ datePicker.getMonth() + " "
+ datePicker.getYear(), Toast.LENGTH_SHORT).show();
CivilDate civil = new CivilDate(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
PersianDate perDate = DateConverter.civilToPersian(civil);
Log.d("-------", perDate.getDayOfMonth()+ " " + (perDate.getMonth() + 1) + " " + perDate.getYear() + " " + perDate.getMonthName());
datePicker.init(perDate.getYear(), (perDate.getMonth() + 1), perDate.getDayOfMonth(), null);
}
});
Upvotes: 2
Views: 2678
Reputation: 1034
There is library for that. You can get more information in the library's github page.
A screenshot from the DatePickerDialog :
Upvotes: 2
Reputation: 12674
Instead of using the DatePicker
switch to using the Android Wheel library.
It's written by a Google Developer and it can be styled to look like the Android DatePicker
it also provides consistent look and feel on older phones.
It's not restricted by what you can put in the fields either which means you can use anything you want.
Upvotes: 2
Reputation: 17848
Not really portable way is to use reflection to reach into DatePicker, get spinners and explicitly set spinners' contents to what you need.
Here's the code I wrote to replace times in TimePicker with Japanese Traditional Time using reflection: https://github.com/aragaer/jtt_android/blob/0855eb7ae1b3de897640bf560cc3bda700cbc2e2/src/com/aragaer/jtt/JttPickerDialog.java
One of two methods fixOld
and fixNew
is called depending on API version. Changes in API might cause either fixNew to fail on even newer versions, that's why it's not really good way.
Better way might be to simple copy the code and layout of DatePicker and create your own custom view from it.
Upvotes: 1