compile files('libs/android-query-full.0.26.8.jar')
The following graphics shows different tooltips with translations:
Searching for the meaning of a particular word can be easier using a tooltip. Tooltip works by either using double click or long press on a word.
By double clicking a word, a popup will show which contains the image, part of speech, sound and meaning of the word.
The following code shows how to make a request using the Tooltip Word API.
/** * Method to get Tooltip Word from API */ private class TooltipWord extends AsyncTask{ private String sSource; private String sSourceLanguage; private String sTargetLanguage; private int iReverse; public TooltipWord(String _source, String _sourceLang, String _targetLang, int _reverse) { this.sSource = _source; this.sSourceLanguage = _sourceLang; this.sTargetLanguage = _targetLang; this.iReverse = _reverse; } @Override protected void onPreExecute() { progressBar.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected JSONObject doInBackground(String... args) { new ServiceHandler(); JSONParser jParser = new JSONParser(); // Construct API Parameters int level = prefs.getInt("level", 21); nameValuePair = new ArrayList (10); nameValuePair.add(new BasicNameValuePair("slang", sSourceLanguage)); nameValuePair.add(new BasicNameValuePair("tlang", sTargetLanguage)); nameValuePair.add(new BasicNameValuePair("level",String.valueOf(21))); nameValuePair.add(new BasicNameValuePair("q", sSource)); nameValuePair.add(new BasicNameValuePair("d", "1")); // Debug parameters AQUtility.debug("TOOLTIP WORD PARAMETERS:", nameValuePair); JSONObject json = jParser.getJSONFromUrl(Constants.TOOLTIP_WORD_URL, nameValuePair); return json; } @Override protected void onPostExecute(JSONObject json) { AQUtility.debug("Tooltip Word Result", json); progressBar.setVisibility(View.GONE); try { int returnType = json.getInt("type"); if (returnType == 0) { speakerImageView.setVisibility(View.GONE); txt_meaning.setText("No result(s) found."); txt_meaning.setGravity(Gravity.CENTER); } else if(returnType > 0) { // Check for contents object if (!json.getString("contents").equals("0")) { JSONObject contents = json.getJSONObject("contents"); if (!contents.getString("word").equals("0")) { JSONObject words = contents.getJSONObject("word"); txt_voca.setTypeface(Typeface.create(VocaDBUtils.setNationFont(targetLang), Typeface.BOLD)); txt_voca.setText(words.getString("voca")); txt_part.setText(words.getString("part") + " / " + words.getString("level")); speakerImageView.setVisibility(View.VISIBLE); txt_meaning.setTypeface(Typeface.create(VocaDBUtils.setNationFont(targetLang), Typeface.NORMAL)); txt_meaning.setText(VocaDBUtils.displayMeaning(words.getString("voca"), words.getString("means"), targetLang)); txt_meaning.setGravity(Gravity.LEFT); if (words.getString("image").equals("")) image_imageview.setVisibility(View.GONE); else { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); URL url = new URL(Constants.VOCA_URL + "small/" + words.getString("image") + ".jpg"); Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); image_imageview.setImageBitmap(bmp); image_imageview.setVisibility(View.VISIBLE); AQUtility.debug("Image URL", Constants.VOCA_URL + "word/" + words.getString("image") + ".jpg"); } } } } } catch (Exception e){ e.printStackTrace(); } super.onPostExecute(json); } }
Another method to get the meaning of a word/words is by using long press. When you long press on a word, the tooltip menu will show. It contains the search button to make a request to the API, color buttons to highlight the selected text and a copy button if you want to simply copy the text.
You have two options when you long press a word, first is getting the relative words and the second one is extracting the word list.
Relative words means all vocabulary words that is connected or having relation to the selected word. In this example, the selected word is person.
Word list extraction means listing all unique words found in a text or sentence. In this example, the selected text is "To seize a person for legal action".
The following code shows how to make a request using the Tooltip Text API.
/** * Method to get Tooltip Text from API */ private class TooltipText extends AsyncTask{ private String sSource; private String sSourceLanguage; private String sTargetLanguage; private int iReverse; public TooltipText(String _source, String _sourceLang, String _targetLang, int _reverse) { this.sSource = _source; this.sSourceLanguage = _sourceLang; this.sTargetLanguage = _targetLang; this.iReverse = _reverse; } @Override protected JSONObject doInBackground(String... args) { new ServiceHandler(); JSONParser jParser = new JSONParser(); // Construct API Parameters int level = prefs.getInt("level", 21); nameValuePair = new ArrayList (10); nameValuePair.add(new BasicNameValuePair("slang", sSourceLanguage)); nameValuePair.add(new BasicNameValuePair("tlang", sTargetLanguage)); nameValuePair.add(new BasicNameValuePair("level",String.valueOf(21))); nameValuePair.add(new BasicNameValuePair("q", sSource)); nameValuePair.add(new BasicNameValuePair("d", "1")); String url = ""; // Check query if contains white space if (VocaDBUtils.containsWhitespace(sSource)) { AQUtility.debug("Contains white space"); url = Constants.TOOLTIP_TEXT_URL; } else { AQUtility.debug("No white space"); url = Constants.TOOLTIP_RELATIVE_WORD_URL; } // Debug parameters AQUtility.debug("TOOLTIP TEXT PARAMETERS:", nameValuePair); JSONObject json = jParser.getJSONFromUrl(url, nameValuePair); return json; } @Override protected void onPostExecute(JSONObject json) { AQUtility.debug("Tooltip Text Result", json); try { int returnType = json.getInt("type"); if (returnType == 0) { txt_no_result.setVisibility(View.VISIBLE); wordButton.setVisibility(View.GONE); wordlistView.setVisibility(View.GONE); idiomButton.setVisibility(View.GONE); idiomListView.setVisibility(View.GONE); } else if (returnType > 0) { // Check if contents is not empty if (json.getString("contents").equals("0") || json.getString("contents").equals("")) { txt_no_result.setVisibility(View.VISIBLE); wordButton.setVisibility(View.GONE); wordlistView.setVisibility(View.GONE); idiomButton.setVisibility(View.GONE); idiomListView.setVisibility(View.GONE); } else { JSONObject data = (json.getJSONObject("contents")).getJSONObject("words"); // Check if relative idioms json exists if (data.has("relative_words") && !data.getString("relative_words").equals("")) { JSONArray relative_words = data.getJSONArray("relative_words"); for (int i = 0; i < relative_words.length(); i++) { JSONObject item = relative_words.getJSONObject(i); Words w1 = new Words(item.getString("voca"), " - " + item.getString("part") + " / ", item.getString("level"), item.getString("means"), item.getInt("ilevel")); if (wordNotExists(item.getString("voca"))) { arrWordlist.add(w1); } } wordButton.setVisibility(View.VISIBLE); wordlistView.setVisibility(View.VISIBLE); idiomListView.setVisibility(View.GONE); } else { wordButton.setVisibility(View.GONE); wordlistView.setVisibility(View.GONE); idiomListView.setVisibility(View.VISIBLE); AQUtility.debug("No relative_words"); } JSONObject idioms = (json.getJSONObject("contents")).getJSONObject("idioms"); // Check if relative idioms json exists if (idioms.has("relative_idioms") && !idioms.getString("relative_idioms").equals("")) { JSONArray relative_idioms = idioms.getJSONArray("relative_idioms"); for (int i = 0; i < relative_idioms.length(); i++) { JSONObject item = relative_idioms.getJSONObject(i); Idioms i1 = new Idioms(item.getString("idiom"), item.getString("level"), item.getString("means"), item.getInt("ilevel")); if (vocaDBUtils.idiomNotExists(item.getString("idiom"), arrIdiomList)) { arrIdiomList.add(i1); } } } else { idiomButton.setVisibility(View.GONE); idiomListView.setVisibility(View.GONE); AQUtility.debug("No relative_idioms"); } } } } catch (Exception e){ e.printStackTrace(); } if(arrWordlist.size()>0) { wordButton.setVisibility(View.VISIBLE); wordButton.setText("W ("+arrWordlist.size()+")"); wordDataAdapter.notifyDataSetChanged(); } else wordButton.setVisibility(View.GONE); if(arrIdiomList.size()>0) { idiomButton.setVisibility(View.VISIBLE); idiomButton.setText("I ("+arrIdiomList.size()+")"); idiomsAdapter.notifyDataSetChanged(); } else idiomButton.setVisibility(View.GONE); // Check if both relative words and idioms is empty if (arrWordlist.size() == 0 && arrIdiomList.size() == 0) txt_no_result.setVisibility(View.VISIBLE); super.onPostExecute(json); } }
// API Keys public static final String TOOLTIP_API_KEY = "YOUR_API_KEY";
// API URLs public static final String BASE_URL = "http://www.vocadb.com/v2_dic/"; public static final String TOOLTIP_WORD_URL = BASE_URL + "api_dic_tooltip_word.php"; public static final String TOOLTIP_RELATIVE_WORD_URL = BASE_URL + "api_dic_relative_word.php"; public static final String TOOLTIP_TEXT_URL = BASE_URL + "api_dic_tooltip_text.php";
x-voca-apikey
, along with your API key.httpPost.addHeader("x-voca-apikey", YOUR_API_KEY);
UTF-8
encoding type to the request.httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
Tooltip Word
http://www.vocadb.com/v2_dic/api_dic_tooltip_word.php?slang=en&tlang=en&level=21&q=person&d=1
Tooltip Relative Words
http://www.vocadb.com/v2_dic/pi_dic_relative_word.php?slang=en&tlang=en&level=21&q=person&d=1
Tooltip Text
http://www.vocadb.com/v2_dic/api_dic_tooltip_text.php?slang=en&tlang=en&level=21&q=to+seize+a+person+for+legal+action&d=1
Tooltip Word API Documentation