0

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.

Mauker
  • 11,237
  • 7
  • 58
  • 76
MARSH
  • 117
  • 1
  • 7
  • 22
  • What is the system language? – Mauker Feb 17 '23 at 09:40
  • Hopefully this would help you https://stackoverflow.com/questions/21257014/set-language-to-french-in-android-datepickerdialog – Zain Feb 20 '23 at 12:07
  • @Zain Thanks I have tried this also but not working . – MARSH Feb 21 '23 at 06:16
  • Please add this line hopefully solve your problem: `config.setLocale(locale1) config.setLayoutDirection(locale1) requireContext().createConfigurationContext(config)` – Kamran Manzoor Feb 22 '23 at 07:52
  • Can you please tell us that which API level is you're getting this issue because sometimes API level have the different type of issue with language & the background services too so would like to test it once in lower or upper API level then i will suggest you which solution is work for you. – Eddie Brock Feb 23 '23 at 19:42
  • Can you try setting setDefaultLanguage before initializing time picket dialogue? – Hussain Shabbir Feb 26 '23 at 23:25

3 Answers3

1

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

Waseem
  • 439
  • 3
  • 18
  • Thanks for helping me this have given as a sample but in my case, it is the same condition i have added its was not working let me try again. – MARSH Feb 21 '23 at 06:16
1

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

 Calendar.getInstance(Locale.US)
Kartika Vij
  • 493
  • 2
  • 4
  • 18
0

You can just add this declaration before TimePicker

Locale.setDefault(Locale.FRANCE);
Jaydip
  • 592
  • 5
  • 27