0

I've started to develop a new Android App in Kotlin, actually in my Activity i have a fragment with two EditText, if in my preferences i doesn't have the preference "keyboard" enabled i shouldn't show the virtual keyboard on those EditTexts.

So i was trying to do something like setting showSoftInputOnFocus to false as it was the solution in another question but it's not working so my code looks like this:

Fragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
    val keyboard = sharedPref.getBoolean("keyboard", true)

    Toast.makeText(activity, keyboard.toString(), Toast.LENGTH_LONG).show()

    val qtaTxt = view.findViewById<EditText>(R.id.qtaTXT)
    if (!keyboard) {
        qtaTxt.showSoftInputOnFocus = false //here i must disable the virtual keyboard
    }



    view.findViewById<Button>(R.id.btnArticoli).setOnClickListener {
        findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
    }
}
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

1 Answers1

4

The best solution lies in the Project Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

   <activity android:name=".MainActivity" 
          android:windowSoftInputMode="stateHidden" />
Fahime Zivdar
  • 341
  • 2
  • 9