4

I've read in a couple of places that spell checking cannot be turned on for EditText on Samsung devices... so it MUST be true, right? If so, there are probably other devices that also cannot spell check, such as the LG VK700 tablet my wife just bought from Verizon (don't ask).

Is there any way to detect programmatically whether a device can spell check? I'd like an option for user to turn it on or off, but not if it can't be turned on. I'd like then to have the option grayed out.

(Googling programmatically determine whether android device can spellcheck turned up this, which looks interesting, but I can't justify a lot of work (slow learner, here) for something that most users would likely turn off or ignore anyway since the flagged words would appear only in a list of words matching a user's "word pattern" (e.g., p?tt??n) for solving word puzzles.)

Community
  • 1
  • 1
DSlomer64
  • 4,234
  • 4
  • 53
  • 88

3 Answers3

4

According to the Android Spell Checker Framework documentation, a Spell Checker Service should be exposed as a Service in the app's manifest with a specific intent filter and metadata tag:

<service
    android:label="@string/app_name"
    android:name=".SampleSpellCheckerService"
    android:permission="android.permission.BIND_TEXT_SERVICE" >
    <intent-filter >
        <action android:name="android.service.textservice.SpellCheckerService" />
    </intent-filter>

    <meta-data
        android:name="android.view.textservice.scs"
        android:resource="@xml/spellchecker" />
</service>

So reasonably, we should be able to detect whether any such services are installed by trying to resolve a matching intent.

I don't have a Samsung device to test the "not found" case, but I think this should work:

TextView tv = new TextView(this);
PackageManager pm = getPackageManager();
Intent spell = new Intent(SpellCheckerService.SERVICE_INTERFACE);
ResolveInfo info = pm.resolveService(spell, 0);
if (info == null) {
    tv.setText("no spell checker found");
} else {
    tv.setText("found spell checker " + info.serviceInfo.name + " in package " + info.serviceInfo.packageName);
}

Regardless of whether I enable or disable spell checking in Settings, my Moto G (2013) says:found spell checker com.android.inputmethod.latin.spellcheck.AndroidSpellCheckerService in package com.android.inputmethod.latin.

This is the same package as the vanilla AOSP keyboard. I'd assume the problematic Samsung phones have replaced that package with their own keyboard, without replacing the spell checking service?

Note that even if you detect the existence of a matching service, the actual settings for activating it may also differ between devices...

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
  • --Thanks, but there needs to be an `xml` file `spellchecker.xml` that I am going to see if I can put together. At least that's what `Gradle` says: `Error:(36, 39) No resource found that matches the given name (at 'resource' with value '@xml/spellchecker').` Or, if you already have it, please revise. – DSlomer64 Sep 22 '15 at 22:35
  • And the `xml` file found at your link has errors that I don't know how to fix: ` – DSlomer64 Sep 22 '15 at 22:41
  • BUT if I remove BOTH `subtypes` then your `Java` code tells me what I know: the `LG` does NOT have spellchecker and the `Verizon` DOES. SO THANKS!! – DSlomer64 Sep 22 '15 at 22:51
  • That xml is what a spell checker *service* (providing the spell checking functionality) has to define. It was part of the explanation of why/how the Java code I then posted works (by detecting if there are any apps with such a service installed on the system). – Snild Dolkow Sep 23 '15 at 15:18
  • But the `xml` that is at the link has errors. What then? Can't ignore; Gradle gripes. Whatever; the four lines in my other Answer seemed to detect correctly. – DSlomer64 Sep 23 '15 at 17:33
  • Again, that xml was for someone *providing* a spell checker; *it was not meant for inclusion in your app*. I included it in the post as background information for why the resolveService() stuff works to detect its presence. I thought I made that clear in the post, but maybe you just copy-pasted without reading the rest of my explanation? – Snild Dolkow Sep 23 '15 at 19:42
  • Did you read all that I wrote? Clearly I did more than cut and paste. Regardless of the intention of the code at the link, maybe by happy accident, the four-line `xml` that *I* provided *did* tell correctly which of my two devices DOES have a builtin spellcheckerr (Verizon Ellipsis tablet, whether spellchecking is on or off) and which does NOT (LG VK700). Why not cut and paste my `xml` and `Java` code `boolean blnSpellcheckerPresent = (pm.resolveService(new Intent(SpellCheckerService.SERVICE_INTERFACE), 0)!= null)` to see if it correctly tells if your device has builtin spellchecker? – DSlomer64 Sep 27 '15 at 19:49
  • Note that I modified the Question to say "has builtin spellchecker". – DSlomer64 Sep 27 '15 at 19:53
1

Here's the Java code to manage Preferences to grey out the Spellchecker option or not, depending on whether user's device has it built-in.

SettingsActivity.java:

public class SettingsActivity extends Activity
{
  public static boolean blnSpellcheckerPresent; // slight hack

  @Override protected void onCreate(Bundle _savedInstanceState) {
    super.onCreate(_savedInstanceState);
    // Inserted Snild's code here:    
    PackageManager pm = getPackageManager();
    Intent spell = new Intent(SpellCheckerService.SERVICE_INTERFACE);
    ResolveInfo info = pm.resolveService(spell, 0);
    blnSpellcheckerPresent = (info != null);
    // end insert
    setContentView(R.layout.activity_settings);
  }
} // end class SettingsActivity

SettingsFragment.java:

public class SettingsFragment extends PreferenceFragment
{
  @Override public void onCreate(Bundle _savedInstanceState){
    super.   onCreate(_savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    Preference spellchecker = getPreferenceManager().findPreference("pref_spell_check");

    spellchecker.setEnabled(SettingsActivity.blnSpellcheckerPresent);

  }
} // end class SettingsFragment

Here's addition to preferences.xml:

<CheckBoxPreference
    android:key="pref_spell_check"
    android:defaultValue="false"
    android:persistent="true"
    android:enabled="false"
    android:title="Spell checker"
    android:summary="Allow your phone's built-in spell checker to underline questionable matches"
    />
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
0

To see IF a device has builtin spellchecker, use this xml:

<spell-checker xmlns:android="http://schemas.android.com/apk/res/android"
               android:label="spellchecker_name"
               android:settingsActivity="com.example.SpellCheckerSettingsActivity">
</spell-checker>

(I imagine that the requirements include actually having files to DO the spell checking and since that's not MY goal, deleting the following lines from the xml at the link provided above before the closing tag above makes sense:

<subtype
    android:label="@string/subtype_generic"
    android:subtypeLocale="en”
/>
<subtype
        android:label="@string/subtype_generic"
android:subtypeLocale="fr”
/>

To provide a spellchecker, there's a lot more work to do. Me: not interested. )

DSlomer64
  • 4,234
  • 4
  • 53
  • 88