Android how to disable checkbox during long operation in PreferenceScreen

I have this PreferenceScreen downloaded from xml. The thing is, when I toggle the im flag, it starts the service using to connect to the server it takes 5-10 seconds. How can I disable the check box at this time. Since the layout is bloating, I don't see how to get checkbox.setEnable = false?

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
    android:key="checkBoxenableincomingcall"
    android:title="Enable incoming call"
    android:defaultValue="true"
    android:summary="hasse running as background service" />
<CheckBoxPreference
    android:key="checkBoxmakephoneringonincoming"
    android:title="Dont ring on message"
    android:defaultValue="true"
    android:summary="dont disturb me" />
<EditTextPreference
    android:key="edittexvalue"
    android:title="EditText"
    android:summary="EditTextPreference" />

public class EditPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
}

}

+3
source share
1 answer

use PreferenceActivity OnPreferenceChangeListenerand then register your checkbox. When you find that it has been pressed, turn off preference

//in onCreate
findPreference("checkboxpreferencekey").setOnPreferenceChangeListener(this);


public boolean onPreferenceChange(Preference preference, Object newValue){
   if(preference.getKey().equals("checkboxpreferencekey")){
      preference.setEnabled(false);
      return true;
   }
   else return true;
}

So you can turn it off, you still need some kind of callback or broadcast from your service to find out when to turn it on again. But when you do it the same way.

findPreference("checkboxpreferencekey").setEnabled(true);
+3

Source: https://habr.com/ru/post/1787309/


All Articles