MARSH
MARSH

Reputation: 281

How to fix Time picker dialog showing in spanish language

This is popup for time picker.

enter image description here

when I click on keyboard icon it's showing like below screen.

enter image description here

I am using localization in our APP and below code I have written to open Time picker Popup


  private fun openTimePickerDialog(datetime: String) {
          val mHour: Int = datetime.substring(8, 10).toInt()
          val mMinute: Int = datetime.substring(10, 12).toInt()
          setDefaultLanguage()
          Log.i("TAG", "openTimePickerDialog: $mHour $mMinute")
          val timePickerDialog = TimePickerDialog(
              activity,
              { _, hourOfDay, minute ->
                  var selectedMinute = minute.toString()
                  var selectedHour = hourOfDay.toString()
                  if (minute < 10) {
                      selectedMinute = "0$selectedMinute"
                  }
                  if (hourOfDay < 10) {
                      selectedHour = "0$selectedHour"
                  }
                  isClickedDate = ""
                  val date: String = datetime.substring(0, 8)
                  val time = selectedHour + selectedMinute + "00"
                  Log.i("TAG", "onTimeSet: $date$time")
                  roveR3SettingFragmentViewModel.setDateTime(date + time)
                  // txtTime.setText(hourOfDay + ":" + minute);
              }, mHour, mMinute, false
          )
          timePickerDialog.setButton(
              DatePickerDialog.BUTTON_POSITIVE,
              getString(R.string.ok),
              timePickerDialog
          )
          timePickerDialog.setButton(
              DatePickerDialog.BUTTON_NEGATIVE,
              getString(R.string.txt_cancel),
              timePickerDialog
          )
          timePickerDialog.show()
      }
   

  private fun setDefaultLanguage() {
          if (appPreference.appLanguage == LANGAUGE_ES) {
              val locale1 = Locale("en")
              Locale.setDefault(locale1)
              val config = requireContext().resources.configuration
              config.setLocale(locale1)
              requireContext().createConfigurationContext(config)
          } else if (appPreference.appLanguage == LANGAUGE_ES) {
              val locale1 = Locale("ja")
              Locale.setDefault(locale1)
              val config = requireContext().resources.configuration
              config.setLocale(locale1)
              requireContext().createConfigurationContext(config)
          }
      }

Can any one please help me how to fix the language issue which is coming when i click on keyboard icon of time picker dialogue. I want it in english language I have selected it for English only other all thing coming in english but time picker is coming in Spanish language. Thanks.

Upvotes: 0

Views: 194

Answers (3)

Jaydip
Jaydip

Reputation: 686

You can just add this declaration before TimePicker

Locale.setDefault(Locale.FRANCE);

Upvotes: 0

Kartika Vij
Kartika Vij

Reputation: 613

You can pass the locale at the time of initializing your Time Picker Dialog like this

 Calendar.getInstance(Locale.US)

Upvotes: 1

Waseem
Waseem

Reputation: 527

Check the setDefaultLanguage() method in your code carefully. Conditions for both if & else if looks the same, so always it will run the functions in the else if section.

Try to use a different condition for else if

Check the example code here,

  private fun setDefaultLanguage() {
      if (appPreference.appLanguage == CONDITION_1) {
          val locale1 = Locale("en")
          Locale.setDefault(locale1)
          val config = requireContext().resources.configuration
          config.setLocale(locale1)
          requireContext().createConfigurationContext(config)
      } else if (appPreference.appLanguage == CONDITION_2) {
          val locale1 = Locale("ja")
          Locale.setDefault(locale1)
          val config = requireContext().resources.configuration
          config.setLocale(locale1)
          requireContext().createConfigurationContext(config)
      }
  }

In your scenario, both condition 1 and 2 are same. So, always the popup using the language from else if

Upvotes: 1

Related Questions