I use the following code to get TEXT sent to my app from other apps via Share menu, and display the TEXT in an EditText.
Intent receivedIntent = getIntent();
String receivedAction = receivedIntent.getAction();
String receivedType = receivedIntent.getType();
TextView txtView = (EditText) findViewById(R.id.edWord);
//if(receivedAction.equals(Intent.ACTION_SEND)){
if (Intent.ACTION_SEND.equals(receivedAction) && receivedType != null) {
if(receivedType.startsWith("text/")) {
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT).toLowerCase();
if (receivedText != null)
{
txtView.setText(receivedText);
txtView.requestFocus();
ListView myList=(ListView) findViewById(R.id.lstWord);
myList.setFocusableInTouchMode(true);
myList.setSelection(0);
}
else
txtView.setText("");
}
}
Everything works well, i.e., the sent text is displayed in my EditText (namely edWord in the above code). But the problem is the text sent via Share sometimes consists of meaningless elements or derivatives, e.g.: "word, word', word, or looked, books, tomatoes.
Now what I want is to format the text so that it contains only real word or the base form of the word before it is added to EditText.
I've heard about approximate string matching or fuzzy searching but I have no idea how to apply it to my code. I wonder whether you can give me a little help to solve the above problem, at least with formatting/stripping non-word elements.
Thanks in advance.
I think I have found the answer for the first part of my question, i.e., removing non-word elements from a string (starting and/or ending a string). Here is the code with a bit of Regex algorithm that I use:
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
if (receivedText != null)
{
receivedText = receivedText.toLowerCase();
//Remove all non-word elements starting and/or ending a string
String strippedInput = receivedText.replaceAll("^\\W+|\\W+$", "");
System.out.println("Stripped string: " + strippedInput);
txtView.setText(strippedInput);
txtView.requestFocus();
ListView myList=(ListView) findViewById(R.id.lstWord);
myList.setFocusableInTouchMode(true);
myList.setSelection(0);
}
For the second part of my question, which is about fuzzy searching, I guess it more or less involves re-coding how my app searches for results from its SQLlite database. This is still my un-answered question.
Related
I want to get the current letter in EdiText to String, I tried using some ways but without success, because I'm beginner and still learning.
what I want exactly is to insert special char after every written special char (in onTextChanged event), after get it into String to use (if ...equals ...)
String selected = edittext1.getText().toString().substring(startSelection);
Editable editAble = edittext1.getEditableText();
int start = edittext1.getSelectionStart();
if (selected.equals("(")) {
editAble.insert(start, ")");
}
this code not working.
any suggestions ?
I've been trying to display arabic text with arabic numbers in text view. But the problem is the numbers in text appear in reverse order.
((TranslationViewHolder)holder).text.setText("وَاِذَا لَقُوا الَّذِيْنَ اٰمَنُوْا قَالُوْ٘ا اٰمَنَّاﵗ وَاِذَا خَلَوْا اِلٰي شَيٰطِيْنِهِمْﶈ قَالُوْ٘ا اِنَّا مَعَكُمْﶈ اِنَّمَا نَحْنُ مُسْتَهْزِءُوْنَ ١٤ ");
Here the string has a number at the end, but this number or any other number gets reversed so number '١٤' become '٤١'.
I have already tried to use Bidi as below
String mixedLanguageText = elements[position].getAr(); // mixed-language text
mixedLanguageText = BidiFormatter.getInstance(new Locale("ar")).unicodeWrap(mixedLanguageText, TextDirectionHeuristics.RTL);
((TranslationViewHolder)holder).text.setText(mixedLanguageText);
((TranslationViewHolder)holder).text.setTypeface(typeface);
But it has no impact on the output.
I have also tried to setup text direction like below
mixedLanguageText = BidiFormatter.getInstance(new Locale("ar")).unicodeWrap(mixedLanguageText, TextDirectionHeuristics.ANYRTL_LTR);
Even though you said you tried BidiFormatter, I had the same problem with mixed language 'Hebrew/English' and this is how I solved it:
// msg = "506, cvv או תעודת שהות שגויים";
if(BidiFormatter.getInstance().isRtlContext()){
Locale rtlLocale = Locale.getDefault(); // RTL locale
msg = BidiFormatter.getInstance(rtlLocale).unicodeWrap(msg, TextDirectionHeuristicsCompat.ANYRTL_LTR);
}
Snackbar.make(mDrawerLayout,msg,length).show();
I hope it helps.
I’m developing an android app that gets objects from a server and shows them in a simple list.
I’m trying to figure out how to deal with long object’s titles :
Every title populates a designated multi-line TextView.
If a title is longer than 16 characters, it messes with my desired UI.
There are two scenarios I need to solve -
1). If the title is longer than 16 characters & contains more than one word, I need to split the words into different lines (I tried to .split("") and .trim(), but I don’t want to use another view, just break a line in the same one, and the use in ("") seems unreliable to me).
2). If the title is longer than 16 characters and contains only one long word, I only need to change font size specifically.
Any ideas for a good and reliable solution?
Thanks a lot in advance.
use SpannableString for a single view
For title:
SpannableString titleSpan = new SpannableString("title String");
titleSpan.setSpan(new RelativeSizeSpan(1.3f), 0, titleSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
for Message
SpannableString messageSpan = new SpannableString("Message String");
messageSpan.setSpan(new RelativeSizeSpan(1.0f), 0, messageSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
set in TextView
tvTermsPolicyHeading.setText(TextUtils.concat(titleSpan, messageSpan));
Code like below it will work as you need
String title; //your title
//find length of your title
int length = title.length();
if (length>16){
string[] titles = myString.split("\\s+");
int size = titles.length;
if (size < 2){
yourTextview.setText(title);
// reduce the text size of your textview
}else {
String newTitle= "";
for (int i=0;i<titles.length;i++){
newTitle = titles[i]+"\n"
}
yourTextview.setText(newTitle);
}
}
You can split and then concatenate the words using "\n" if there are more than one words.
In case of long word
You can see this question here
Auto-fit TextView for Android
try this:
if(title.split(" ").size > 1){
String line1 = title.substring(0, 16);
int end = line1.lastIndexOf(" ");
titleTextView.setText(title.substring(0,end) + "\n" +
title.substring(end+1,title.size-1);
}else{
titleTextView.setText(title);
titleTextView.setTextSize(yourTextSize);
}
this code should work perfectly for your case.
Well, I'm trying to replace a word by using contains() Method:
String z = tfB.getText().toString();
String show = textPane.getText().toString();
if(show.contains(z)){
// how I specify the word that were found and change it without
effecting anything with in that line
}
well what I main by that:
What I'm trying to do is get the value from the user.
then search if it found replace it with something. For example:
String x = "one two three four five";
It should set the textPane to "one two 3 four five"
or
"one two 3-three-3 four five"
could any one please tell me how to do it.
Thank you
What I'm trying to do is get the value from the user. then search if it found replace it with something.
Don't use the contains() method because you will need to search the text twice:
once to see if the text is found in the string
again to replace the text with a new string.
Instead, use the String.indexof(...) method. It will return the index of the text IF it is found in the String.
Then you should replace the text directly in the Document of the text pane, not in the String itself. So the code would be something like:
int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);
String search = "abc...";
int offset = text.indexOf(search);
if (offset != -1)
{
textPane.setSelectionStart(offset);
textPane.setSelectionEnd(offset + search.length();
textPane.replaceSelection("123...");
}
Also, not that you get the text from the Document, not the text pane. This is to make sure the offsets are correct when you replace the text in the Document. Check out Text and New Lines for more information on why this is important.
Have been at this for a while so I think it's time for some much needed help. I have the following if statement in an action listener('but' - being the Search button that gets clicked for the following to happen).I have an arrayList(used to save data from database) in my class, calling a specific record works perfectly. I am trying to create a search textField(named 'tf' below). I have been able to search only by typing an exact match to what ever is being searched, but I want to be able to use partial matching on any String in tf. Everything that's commented out is different stuff that I have been testing, therefore can be ignored.
The statement:
if( srch.getText().equals("You have chosen to search by movie: " )
... this is used to make sure that the person is searching only when the panel includes this title, as there is a genre search as well on the same panel. Only the title changes.
if(source.equals(but)){
//String b = a.get(0).get(1);
String result = tf.getText();
Pattern pat = Pattern.compile("[a-z]+");
Matcher m = pat.matcher(result);
//boolean bool = m.matches();
int i = 0;
//al.contains(result) && pat.matcher(a.get(i).get()).matches())
// && z.contains(result)
if( srch.getText().equals("You have chosen to search by movie: " )){
for (ArrayList<String> al : a){
if (pat.m(a.get(i).get(0)).matches()){
System.out.println(a.get(i).get(0)); //movie name
System.out.println(a.get(i).get(1)); //movie desc
}
i++;
}
//ta.setText(b);
}
else{
ta.setText("Please try searching by movie");
}
}
In summary, everything works fine. I just need a regex code to find partial matches as well and a way to add that into my loops. I've added as little code as possible as not to waste anyone's time, so please let me know if any other is needed.Many thanks in advance. Eventually any full or partial matches will display the movie name and description.
I think there is no need of regex.
String.contains(String chars) returns true if the source string have any occurrence of the argument string.
For example:
String str = "ABCDEFG";
if(str.contains("CD")){
System.out.println("Present");
}