Android/Java: Set textview with char from index of string - java

I want the user to put in characters into an edit text and upon a keyUp() would change a textview to match a certain char from a string.
If a user put in 4 characters in the edittext string, then the index of 3 of the string would be set for the textview.
The problem is that the program crashes when checking the 0th index of the string.
This log command gives me the proper output,
String thedefaultstring = "Hello Android";
//e_question is the edit text
int stringsum = e_question.getText().toString().length();
System.out.println(thedefaultstring.charAt(stringsum));
But when I set the textview with the char, it crashes.
// t_debug3.setText(thedefaultstring.charAt(0)); //crashes

Try:
setText(new String(thedefaultstring.charAt(stringSum));
setText takes a String as an argument. Build a new String of only one char to trick it.

Related

Is there a way to automatically select text in TextView?

Is there a way to automatically select or highlight a predetermined area of text on TextView? For instance, I want a specific line on the TextView to be already pre-selected when I start the activity, instead of having the user select that area of text themselves.
What you're looking for is most likely the setSelection, function.
it works like this:
EditText edText = findViewById(id);
edText.setSelection(start, stop);
The setSelection() function takes 2 arguments start and stop, both are ints, its the index of the character where you want the selection to start and the index of the character where you want the selection to end.
for example setSelection(0,1) will select the first character of the EditText.
If you want to select a specific String in an EditText you could do something like:
EditText edText = findViewById(id);
String lookingFor = "whatever youre looking for";
//You get the index of the first character of the string you're looking for
int start = edText.getText().toString().indexOf(lookingFor);
//You add the string's length to the index so the selection actually selects the
//whole string.
int stop = start+lookingFor.length();
//You -start- selecting from the 1st character and -stop- at the last character
//Of the string you're looking for.
edText.setSelection(start,stop);

Setting the color of multiple characters of TextView in java

I will do my best to explain my problem, sorry if I am not clear. Basically I want to set the color of individual characters of a text, and then display them in Android App Development Kit. The problem I am having is that I am taking an array of the characters, and don't know how to set the color of them to a certain hexadecimal value.
For instance if the String is "hello". I would want each character to be a different color.
So I would take 'h' and assign it the hexadecimal value of "#000000". And then display it using xml. Is this possible? Here is what I am attempting to do now with my code.
String end = "";
for (int p = 0; p < charzart.size(); p++) {
if (charzart.get(p).equals(" ")) {
}
else{
Spannable colorSpan = new SpannableString(charzart.get(p));
int fake = Integer.parseInt(color.get(p));
colorSpan.setSpan(new ForegroundColorSpan(fake), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
end += colorSpan;
}
}
output.setText(end);
Right now my code doesn't work and I am not sure why. So i am running through charzart which holds my characters. And then attempting to assign the hexadecimal value ( which I have in another array list called color). I check to see if there is a space, if there is I ignore it and move on until i reach a character. From there, I get the character, set it to a spannable. I then get the color, apply it to the spannable, add it to a string and at the end set the TextView to that string.
Basically I want to know how to assign a hexadecimal value to a character, which will then be outputted by XML.

Hide part of a string in EditText (Java/Android)

Is it possible to hide part of a string in the editText with a method like
editText.hide(int start, int finish);
so that in the display I can't read the hidden part of the string, but if I use a method like editText.getText() in the code, it will include the hidden string?
You can do something like:
String wholeString = editText.getText().toString();
editText.setText(editText.getText().toString().substring(int start, int end));
where start and end are integers that you want your string to begin and end at the positions you set.
So now you have the whole string before it was hidden. Additionally, if you want to hide a certain part in the middle of your editText, you can do something like this:
editText.setText(editText.getText().toString().substring(0, 20) + editText.getText().toString().substring(25, 30));
where you set your editText text with many strings. This will hide the string from position 21 till 25. You have to modify it according to your needs.
You may also need to know a method for a string called lastIndexOf("Some Text")
Example:
//You have a string called example.
String example = "Android development is fun and nice."
String cutExample = example.substring(example.lastIndexOf("is") +3, example.lastIndexOf(".");
The result will be: "fun and nice".
So lastIndexOf() will return the position of the defined string.
Test with what I wrote. Hope that helps.

Get a specific line from a multiple line TextView

How to get the specific text from a multiple line TextView in Android Studio and later set the text extracted to another TextView?
Check this answer, generally you should do something like this:
//find the character offsets in the text
int startPos = myTextView.getLayout().getLineStart(linenumber);
int endPos = myTextView.getLayout().getLineEnd(linenumber);
String theLine = myTextView.getText().substring(startPos, endPos);
Then set this specific line to your other TextView.
You can use
textView.getLayout().getLineStart(int line)
and
getLineEnd to find the character offsets in the text.
Then you can just use
textView.getText().substring(start, end)
or subsequence if you are using Spannables for formatting/etc.

how to replace a word with in contains() Method form a JtextPane

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.

Categories