Sonu Kumar
Sonu Kumar

Reputation: 969

How to read text dictionary from assests display on Candidate view in Softkeyboard

I'm developing softkeyboard in updateCandidates(). My question is how to retrieve each text when Composing have some words then load my dictionary in candidate view.

Image:

enter image description here

How to load words.

Image

enter image description here like this

Thanks for share me any information about it.

Upvotes: 1

Views: 469

Answers (2)

user5427010
user5427010

Reputation:

Solution

You can make method like this :

public ArrayList<String> readFile() {
        ArrayList<String> list = new ArrayList<String>();

        try {
            AssetManager am = getAssets();
            InputStream data = am.open("data.txt");
            InputStreamReader dataInputStream = new InputStreamReader(data);
            BufferedReader reader = new BufferedReader(dataInputStream);
            String each = null;
            while ((each = reader.readLine()) != null) {
                list.add(each);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;

    }

Edit Softkeyboard

pickSuggestionManually()

public void pickSuggestionManually(int index) {
        if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
            CompletionInfo ci = mCompletions[index];
            getCurrentInputConnection().commitCompletion(ci);
            if (mCandidateView != null) {
                mCandidateView.clear();
            }
            updateShiftKeyState(getCurrentInputEditorInfo());
        } else if (mComposing.length() > 0) {
            // If we were generating candidate suggestions for the current
            // text, we would commit one of them here. But for this sample,
            // we will just commit the current text.
            mComposing.setLength(index);
            mComposing = new StringBuilder(mCandidateList.get(index) + " ");
            commitTyped(getCurrentInputConnection());
        }
    }

Important here updateCandidates()

    ArrayList<String> listData = readFile();

    System.out.println("Sonu Kumar: " + listData);

    if (!mCompletionOn) {
        if (mComposing.length() > 0) {
            ArrayList<String> list = new ArrayList<String>();
            // list.add(mComposing.toString());

            for (int j = 0; j < listData.size(); j++) {
                String str = mComposing.toString().toLowerCase();
                if (listData.get(j).startsWith(str)) {
                    list.add(listData.get(j));
                }
            }
            mCandidateList = list;
            setSuggestions(list, true, true);
        } else {
            setSuggestions(null, false, false);
        }
    }

Thank

Upvotes: 2

Avanish Singh
Avanish Singh

Reputation: 777

To show the suggestion words in candidate view, please modify the updateCandidates() method of SoftKeyboard.java file:

 ArrayList<String> list = new ArrayList<String>();
    updateCandidate(){ 
    if (!mCompletionOn) {
      list.clear();
        if (mComposing.length() > 0) {       

            String mStr= mComposing.toString().toLowerCase();

            for (int i = 0; i < Dictionary.data.length; i++) {
                 String str = Dictionary.data[i];
                if (str.startsWith(mStr)) {
                    list.add(str);
                }
            }       
            setSuggestions(list, true, true);
        } else {
            setSuggestions(null, false, false);
        }
     }

    }

To enter the picked word from candidate view to input text, please modify following method in SoftKeyboard example project.

public void pickSuggestionManually(int index) {
    if (mCompletionOn && mCompletions != null && index >= 0
            && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        getCurrentInputConnection().commitCompletion(ci);
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        updateShiftKeyState(getCurrentInputEditorInfo());
    } else if (mComposing.length() > 0) {

       mComposing.setLength(index);
        mComposing = new StringBuilder(mCandidateList.get(index) + " ");
        commitTyped(getCurrentInputConnection());

    }
}

Upvotes: 1

Related Questions