0

I am trying to implement the settings activity according to the guidelines. I have an EditTextPreference that I want to keep, but instead of allowing the user to type in any value, the value should come through Bluetooth (i have the Bluetooth part ready).

At the moment, when the EditTextPreference is clicked, it shows the editor popup with okay and cancel buttons. That's how I want it to be, so I can handle the OK or Cancel click events.

1) The first problem is that the keyboard also shows up - I don't want that, because the value should come from the background. I've added these properties, but nothing seems to have any effect on hiding the keyboard(even if I switch them around):

<EditTextPreference
    android:selectable="true"
    android:enabled="true"
    android:editable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:cursorVisible="false"
    android:capitalize="words"
    android:inputType="none"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:title="@string/pref_title_id" />

2) The second problem is: how do I update the value of the EditTextPreference from the code behind, so the user doesn't have to type in anything, but just to see the value and click okay or cancel?

3) Third problem / question: is it okay to save the values in a database instead of using the shared preferences? Basically, I want to have the common settings UI, but keep the values in a database.

I hope that someone has had the same issues as me, because I was unable to find any solutions on the internet.

Apostrofix
  • 2,140
  • 8
  • 44
  • 71

3 Answers3

0

1.When you click the edit box, you should call this method, that hides the keyboard.

 private void hideKeyboard() {
    View view = getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
 }
}
  1. For setting a value to the editText use the method:

     editTextPreferences.setText("Your value");
    
  2. For saving just a value, you can use Shared Preference because its more flexible, but if you want to save more data and to have all in a db you can use SQLite db. Both them save local values,because when app is uninstall, they are deleted.

  • Thanks for your answer. The first answer doesn't seem to help. The keyboard is still showing. I've handled the `EditTextPreference.setOnPreferenceClickListener`, which calls the `hideKeyboard()` method, but it doesn't hide the keyboard. – Apostrofix Apr 25 '16 at 10:30
  • You can try EditTextPreference.setOnPreferenceChangeListener(); – user3796978 Apr 25 '16 at 10:33
  • It's still the same. The `setOnPreferenceChangeListeer()` is called, then it calls the `hideKeyboard()` method, but it doesn't hide the keyboard. – Apostrofix Apr 25 '16 at 10:35
0

For updating EditTextPreference check this one EditTextPreference.setText(value) not updating as expected

For disable Input =>

setFocusableInTouchMode(boolean)
setFocusable(boolean)
Community
  • 1
  • 1
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

Try following code in your activity's onCreate method to make the keyboard only pops up when a user clicks an EditText.

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

or

use android:windowSoftInputMode="stateHidden" in the Android Mainfest.xml under activity tag

To Hide a Keybord call this method in your onClick event

 private void hideKeyboard() {

            View view = getCurrentFocus();
            if (view != null) {
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                        hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }
sivaBE35
  • 1,876
  • 18
  • 23