Reputation: 33
In my app design plan, I want to use the language to be Bahasa Indonesia. In this case of choosing date, I use standart datepicker spinner dialog and I don't know how to change the language of months. This is how my datepickker spinner looks like. How to change the month names to be Bahasa Indonesia, like "Jan, Feb, Mar, Apr, Mei, Jun, Jul, Agu, Sep, Okt, Nov, Des" ?
And this is my code
val cal: Calendar = Calendar.getInstance()
val year: Int = cal.get(Calendar.YEAR)
val month: Int = cal.get(Calendar.MONTH)
val day: Int = cal.get(Calendar.DAY_OF_MONTH)
val dialog =
DatePickerDialog(
context,
R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth,
{ _, year, month, day ->
listener.setTheDate(year, month, day) // this to change TextView
},
year, month, day
)
dialog.datePicker.maxDate = Date().time
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.show()
Also I want to make the buttons (Cancel and OK) to be Bahasa Indonesia like "Batal" and "OK". Looking forward for the support. Thank you.
Upvotes: 0
Views: 1300
Reputation: 83
You could make a custom date picker dialog like here.
So you create your own custom widget called AppLocaleDatePickerDialog or something that extends the DatePickerDialog. In some date and time pickers there are delegates so you need to check for those too.
private void initPicker(Object object, String[] values) {
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
// If there's a delegate, we use it instead.
if (field.getName().equals("mDelegate")) {
field.setAccessible(true);
object = field.get(object);
fields = object.getClass().getDeclaredFields();
break;
}
}
for (Field field : fields) {
if (field.getName().equals("mAmPmStrings") ||
field.getName().equals("mShortMonths")) {
field.setAccessible(true);
field.set(object, values);
} else if (field.getName().equals("mAmPmSpinner") ||
field.getName().equals("mMonthSpinner")) {
field.setAccessible(true);
Object innerObject = field.get(object);
Method method = innerObject.getClass().getDeclaredMethod(
"setDisplayedValues", String[].class);
method.setAccessible(true);
method.invoke(innerObject, (Object) values);
}
}
Method[] methods = object.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("updateAmPmControl") ||
method.getName().equals("updateSpinners")) {
method.setAccessible(true);
method.invoke(object);
break;
}
}
} catch (Exception e) {
Log.e(APP_TAG, e.getMessage(), e);
}
}
Define your months in res/values/arrays.xml and create values for your language too. If it were German, it would be res/values-de/arrays.xml.
<string-array name="months_values">
<item>Jan</item>
<item>Feb</item>
<item>Mar</item>
<item>Apr</item>
<item>May</item>
<item>Jun</item>
<item>Jul</item>
<item>Aug</item>
<item>Sep</item>
<item>Oct</item>
<item>Nov</item>
<item>Dec</item></string-array>
And initialize it like this in the declaration of your custom widget:
initPicker(datePicker, resources.getStringArray(R.array.calendar_months));
About the buttons. You can set the dialog's button texts pretty easily with setNegativeText() and setPositiveText().
dialog.setNegativeText(context.getResources().getString(R.string.cancel));
dialog.setPositiveText(context.getResources().getString(R.string.ok));
If you need more information about using string resources you can find it here.
Upvotes: 1