DSlomer64
DSlomer64

Reputation: 4283

Is there any way to programmatically determine whether android device has builtin spellchecker?

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.)

Upvotes: 4

Views: 2213

Answers (3)

DSlomer64
DSlomer64

Reputation: 4283

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. )

Upvotes: 0

DSlomer64
DSlomer64

Reputation: 4283

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"
    />

Upvotes: 1

Snild Dolkow
Snild Dolkow

Reputation: 6866

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...

Upvotes: 4

Related Questions