spandan madan
spandan madan

Reputation: 319

Make an android app for adding words to user dictionary

I am trying to make an app for adding a custom txt file to android user dictionary. I am extremely new to android programming and this is the first app I'm trying to make. I would like to have a simple screen with a single button, which when pressed, just adds the words in text file to the users dictionary. At the moment, the user does not have the ability to choose which text file. Its a simple one click operation for my target user base. Android has this API to add words to user dictionary, but I have no clue how to use it :-

http://developer.android.com/reference/android/provider/UserDictionary.Words.html

This is as far as I've gotten till now, I've just created a button and don't know how to give it that function. Any help will be greatly appreciated :) Thanks in advance!

package com.example.spandanmadan1.hinglish;

import android.provider.UserDictionary;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Button bsubmit = (Button) findViewById(R.id.button);

        bsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserDictionary.Words.addWord(getApplicationContext(),"hoogakabooga",1,"hoog",null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

Upvotes: 1

Views: 2261

Answers (1)

Apoorva
Apoorva

Reputation: 1375

Initialise your button in the onCreate method like:

Button bSubmit = (Button) findViewById(R.id.submitButton); //This should be the id of the concerned submit button in your xml

Then just add a click listener to it, which basically performs some operation when the button is clicked.

bSubmit.setOnClickListener(new OnClickListener() {
   public void onClick(View view) {
    //Do something on button click -- Add words to dictionary
    UserDictionary.Words.addWord(getActivity(), "Word", 1, "", Locale.ENGLISH)
   }
}

Upvotes: 2

Related Questions