Mohammad Behnam
Mohammad Behnam

Reputation: 11

DatePickerDialog month name shows M+0+Month number

I am using DatePickerDialog. But month name are showing wrong, i.e. instead of January 2019 it is showing 2019 M01 and February as 2019 M02 and so on. My problem is that default view for calendarView is showing the month numbers like that, i am not changing or setting anything in this views, normally it must show something like : 2019 January.

How can i fix this issue?

My Layout:

MY LAYOUT IMAGE

<CalendarView
        android:id="@+id/calender"
        android:layout_width="320dp"
        android:layout_height="300dp"/>

My Code:

  CalendarView calendarView = view.findViewById(R.id.calender);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            saveDate(year, month, dayOfMonth);
        }
    });

Upvotes: 1

Views: 1334

Answers (2)

Rinkesh
Rinkesh

Reputation: 3316

this is happened due to misconfiguration of Locale you must set Locale to en to get January like month

the following code will use to set locale

fun setLocale(context: Context, language: String): Context {
        persist(language, context)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else updateResourcesLegacy(context, language)
    }

@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)
    val configuration = context.resources.configuration
    configuration.setLocale(locale)
    return context.createConfigurationContext(configuration)
}

private fun updateResourcesLegacy(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)
    val resources = context.resources
    val configuration = resources.configuration
    configuration.locale = locale
    resources.updateConfiguration(configuration, resources.displayMetrics)
    return context
}

Somewhere in your base Activity

override fun attachBaseContext(newBase: Context) {
    super.attachBaseContext(setLocale(context, "en"))//or other language
   }

Upvotes: 1

Midhun PM
Midhun PM

Reputation: 522

Please follow this

public class MainActivity extends AppCompatActivity {

    Calendar calendar;
    CalendarView calendarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendar = Calendar.getInstance();


        calendar.set(Calendar.MONTH, Calendar.MAY);
        calendar.set(Calendar.DAY_OF_MONTH, 9);
        calendar.set(Calendar.YEAR, 2019);


        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.YEAR, 1);


        calendarView = findViewById(R.id.calendarView);


        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {

                String msg = "Selected date Day: " + i2 + " Month : " + (i1 + 1) + " Year " + i;
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();


            }
        });


    }
}

Upvotes: 0

Related Questions