Android ArrayList TextView does't show text after setText - android

Hello I got some problems making an app and couldn't find the answer anywhere.
I have an EditText for user input. I have a String word and I have an ArrayList with TextViews in it.
I want to set the text of a TextView based on if the user input equals a character from the String. If a character is equal it must be shown in the right TextView.
The problem now is that if I put in a character that should match with a character from the word, it doesn't show me anything, even when I want to show the StringBuilder in another TextView it looks like it is empty.
I have this:
public void onClickButtons(View view) {
if(view==mBtnGuess) {
String getInput = mEtxtUserInput.getText().toString();
word = "someword";
if(getInput.length()==1) {
List<TextView> txtCharArr= new ArrayList<TextView>();
txtCharArr.add(mChar1);
txtCharArr.add(mChar2);
txtCharArr.add(mChar3);
txtCharArr.add(mChar4);
txtCharArr.add(mChar5);
txtCharArr.add(mChar6);
txtCharArr.add(mChar7);
txtCharArr.add(mChar8);
txtCharArr.add(mChar9);
txtCharArr.add(mChar10);
txtCharArr.add(mChar11);
txtCharArr.add(mChar12);
StringBuilder sb = new StringBuilder();
for(i=0;i<getInput.length();i++) {
if(getInput.equals(Character.toString(word.charAt(i)))) {
txtCharArr.get(i).setText(Character.toString(word.charAt(i)));
sb.append(Character.toString(word.charAt(i)));
}
}
}
}
}

I think instead of word.length() you are using getInput.length()
for(i=0;i<getInput.length();i++) {
if(getInput.equals(Character.toString(word.charAt(i)))) {
txtCharArr.get(i).setText(Character.toString(word.charAt(i)));
sb.append(Character.toString(word.charAt(i)));
}
}
Hence you will only loop once and check the getInput for only the first char of word, which is s.

Related

Android - Go to next found word from EditText in TextView

I have a TextView and I can search some words, so that they're marked.
The TextView is named textView and it shows the text, I have an EditText Etxt to get the searched word and a Button to start the search. The code above is the code of the search. The app marks all found words big and italic. And I have a TextView text_total which shows the number of found words.
The problem: But if there is a searched word in the text below the shown screen, you must scroll and find the marked word:
int total = 0;
String word_search = Etxt.getText().toString().trim().toLowerCase();
String fullTxt = textView.getText().toString();
String[] array = fullTxt.split("\n");
String word;
StringBuilder st = new StringBuilder();
for (int i = 0; i < array.length; i++) {
word = array[i];
if (word.toLowerCase().contains(word_search)) {
String abc = word.trim();
st.append("<b><i>" + abc + "</i></b>");
total++;
} else {
st.append(word);
}
st.append("<br>");
}
textView.setText(Html.fromHtml("" + st));
text_total.setText("Ergebnisse: " + total);
Now, I have a problem because the text is too long to see all search results. I want that I have a 'back' and a 'next' button and the view goes to the next result if I click the next button and that the the found word goes automatically to the shown screen.
Does anyone know how to code this?
That's very important. Thanks for help!
Try this.
Put all the finded words in a list
List words= new ArrayList();
Add the words to the list
words.add(word_search);
Finally in next and previous buttons, you need to check the positions of the words in the list
words.get(position)
See how in this link

smiley validation not working in android

I have EditText, in that we can enter all keyboard characters like alphanumerics, smileys, special characters.
I have to validate for some special characters and all smileys not to be allowed. Needs to show all the entered non-allowed characters as alert dialog.
I tried with many smileys, for smileys it is taking only empty spaces inside the below function and for validation it is showing only one smiley, sometimes it is showing two.
Below is the code.
public static String validateSpecialCharacters(String message, Pattern messagePattern){
StringBuilder matchedCharacters = new StringBuilder();
Map<Character, Integer> uniqueSpecialCharacters = new HashMap<>();
Matcher m = messagePattern.matcher(message);
while (m.find()){
if(!uniqueSpecialCharacters.containsKey(message.charAt(m.start()))) {
uniqueSpecialCharacters.put(message.charAt(m.start()), 1);
matchedCharacters.append(message.substring(m.start(),m.end()));
}
}
return matchedCharacters.toString();
}
Inside the while the if condition is getting true only once or twice. But I have entered different smileys. The issue is the previous key added for the first smiley is similar with the second. But actually the smiley's are not similar. How to solve this.

Html Styling in textview goes wrong Android

I am selecting a part of the TextView and on click of a "highlight" button, I am sending the start and the end index of selection to the database. Then I am loading all the start and end indexes from db and changing the color of text between them.
The problem is after once or twice, the app is changing the color of text that is not in selection.. and the selected part remains unchanged.
MY CODE:
When user selects and presses the highlight button
int i=contentText.getSelectionStart();
int j=contentText.getSelectionEnd();
db.insertHiglightIndex(String.valueOf(i),String.valueOf(j));
setHighlightedText();
The setHighlightedText() method..
String fullText=contentText.getText().toString();
for(int i=0; i<db.getAllStartIndex().size();i++){
String a=fullText.substring(Integer.parseInt(db.getAllStartIndex().get(i)),Integer.parseInt(db.getAllEndIndex().get(i)));
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
}
contentText.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);
MY SCREENSHOTS.
The selection:
The Result:
Clearly the selected area is from "Garrick" to "Bart", and the result is from "entity" to "2012"
I am not able to understand why is this happening. I think there is some problem with this <font color='red'>"+a+"</font> line.
Thank you
It got wrong indexed because There is already added <font color='red'> in the beginning, So that in second time This tag is also counted as a part of string, So I suggest creating a new temporary String, assign same text to the String but after replacing the previous font tag it held. Use this syntax to remove previous font tag from originalString
String tempString = originalString.replaceAll("[<](/)?font[^>]*[>]", "");
After that work with only tempString. That means again add every previous font tag you have to tempString and set that text.
In next time again do the same first remove all font tag and again add all of them back in tempString as well as current selection using same loop you are using currently.
You have wrong indexes because you are modifying the fullText content within the loop.
Taking a look at this example you can figure it:
final TextView tv = (TextView) findViewById(R.id.textView);
tv.setText( "abcdefghijklmnopqrstuvwxyz0123456789");
String fullText= tv.getText().toString();
// your first iteration
String a = fullText.substring(1,3);
// a contains "ab"
fullText = fullText.replace(a, "<font color='red'>"+a+"</font>");
After the first iteration full text contains now
a<font color='red'>bc</font>defghijklmnopqrstuvwxyz0123456789"
Then the substring() in the second iteration won't returns the substring base on your initial content.
If you want to be able to have multiple substrings colored in red you can try this:
String fullText = contentText.getText().toString();
StringBuilder result = new StringBuilder();
for(int i=0; i < db.getAllStartIndex().size(); i++){
fullText = applyFont(result, fullText, Integer.parseInt(db.getAllStartIndex().get(i)), Integer.parseInt(db.getAllEndIndex().get(i)));
}
// Add here the remaining content
result.append(fullText);
contentText.setText(Html.fromHtml(result.toString()), TextView.BufferType.SPANNABLE);
private String applyFont(StringBuilder result, String source, int from, int to){
result.append(source.substring(0, from));
result.append("<font color='red'>");
result.append(source.substring(from, to));
result.append("</font>");
return source.substring(to, source.length());
}

Android BreakIterator hyphenated words?

I using breakIterator to get each word from a sentence and there is problem when a sentence like "my mother-in-law is coming for a visit" where i am not able to get mother-in-law as a single word.
BreakIterator iterator = BreakIterator.getWordInstance(Locale.ENGLISH);
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next())
{
String possibleWord = sentence.substring(start, end);
if (Character.isLetterOrDigit(possibleWord.charAt(0)))
{
// grab the word
}
}
As I'm seeing in your code what are you trying to do is to check if the first character in every word are a character or a digit. Every time you use the BreakIterator.getWordInstance() you will always get all the words depending on the boundary rules of the Locale and it is a little hard to accomplish what you want to do with the use of this class until I know, so my advice is this:
String text = "my mother-in-law is coming for a visit";
String[] words = text.split(" ");
for (String word : words){
if (Character.isLetterOrDigit(word.charAt(0))){
// grab the word
}
}

adding prefix to words in EditText

I have a List of Strings and i want to compare every i write in an EditText with that list. If there is a match then i have to add a "-" character as a prefix for that word.
I am using a TextWatcher and this is my code so far:
#Override
public void afterTextChanged(Editable s) {
String tmp = s.toString();
words = tmp.split(" ");
for (int i = 0; i < words.length; i++) {
for (Iterator iterator = myList.iterator(); iterator
.hasNext();) {
String str = (String) iterator.next();
if (str.equalsIgnoreCase(words[i])) {
if (!words[i].contains("-")) {
tmp = tmp.replace(words[i], "-" + words[i]);
}
editMain.setText(tmp);
editMain.setSelection(tmp.length());
}
}
}
}
It works but if i type the same word twice in my EditText, the first ocurrence gets two "--".
For example:
hello this is -android (works ok)
hello this is --android -android (does not work ok)
And the desired result should be:
hello this is -android android (because the repeated word already exists)
Any help? thanks in advance
Your question is not very clear. Maybe you mean android word has already been found and then it should not be prefixed by a -.
If that's the case, just remove a mathcing word from mylist. For that use a listIterator.
try to set a counter. If the counter is bigger than 1, then don't write the -

Categories

Resources