i know highlighting in text-view is possible
Highlight Textview Using EditText
and scrolling text-view is also possible
(got the scroll code from here and is successfully scrolling too)
textView scroll at first line
now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,
point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?
i found an answer for this, but later i realized its for iOS
Search occurrences of a string in a TextView
Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by using the following
String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"
As you see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.
This is working for loading a file into a textview so that the user's last selection or cursor position is at the top of the screen.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
page.setSelection(pos, pos2);
Layout layout = page.getLayout();
scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2))); }
}, 500);
page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.
Related
I'm working on a calculator app in android studio, and I would the calculator to append the answer to the existing equation, more like a graphing calculator, where it shows the equation then the answer beneath it. My current solution works for the first equation only. I've tried
int start = display.getLayout().getLineStart(display.getLineCount());
int end = display.getLayout().getLineEnd(display.getLineCount());
then
double result = calc(display.getText().toString().substring(start,end));
The result is that I get an IndexOutOfBoundsException from getLineEnd, and I don't know how to go about it?
Ticked answer is wrong. I have multiline TextView but the text has no '\n' or '\r' characters. It is multiline because width isn't enough for whole text. Your problem is you sending getLineCount() as parameter you must send getLineCount() -1 for last line. I am calculating last line width as below:
float lastLineWidth = textViewMessage.getLayout().getLineRight(textViewMessage.getLineCount() - 1) - textViewMessage.getLayout().getLineLeft(textViewMessage.getLineCount() - 1);
But I have one problem with using it. I cant call textView.getLayout() method in ListView.getView() mehtod. It always returns null. I can only call it in TextView.Post() method but I don't want to call it in Post() method because I do some adding and removing rules to LayoutParams and it causes problem scroll problems in ListView.
Is there a way to get last line string or last line width without calling getLayout() method of TextView.
Sounds like you have a multiline TextView so you could try splitting by new line chars and getting the last element from resulting array like this:
public static String getLastLine(TextView display){
String lines[] = display.getText().toString().split("\\r?\\n");
String lastOne = lines[lines.length-1];
return lastOne;
}
I hate asking questions unless I absolutely have to, but I can't seem to find a clear answer to this problem.
I'm working on a practice app and one of the activities should take user input via an EditText view, and then return that input to the activity's TextView view, i.e. it should say hello back to the user after the user has typed their name in.
Instead of returning the user input, it gives me back this strange string of characters for some reason. Here's my code for reference:
text = (TextView) findViewById(R.id.mTextView);
input = (EditText) findViewById(R.id.mEditText);
enter = (Button) findViewById(R.id.mButton);
enter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { text.setText("Hello " + input);
}
});
}
So rather than returning "Hello (user input)", it outputs something like:
Hello android.support.v7.widget.AppCompatEditText{3f5e428a .. So I'm confused as all hell right now.
Help!
P.S. - I would've posted screenshots but apparently I need 10 reputation points before I can do that, so hopefully this is enough..
If you want the string value from an EditText:
String s = input.getText().toString()
You can put that into the TextView.
input is an EditText, not the actual text thats been typed into the view.
You need to call input.getText() to get the text in it.
The value that you are getting is the default behavior of toString on the EditText.
At the outset, I would like to apologize for my English :).
I have a String with long text. I display it in a TextView. If the text is automatically wrapped to new line, I want to add "\t" at the beginning of the new line. I don't know how do it. Any ideas?
E.g.
String text = "1. abcdefghij\n\tklmnopqrstuvwxyz";
Display:
1. abcdefghij
klmnopqrstuvwxyz
But if:
String text = "1. abcdefghij\n\tklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Display:
1. abcdefghij
klmnopqrstuvwxyzABCDEF
GHIJKLMNOPQRSTUVWXYZ
I want:
1. abcdefghij
klmnopqrstuvwxyzABCDEF
GHIJKLMNOPQRSTUVWXYZ
You can count the lines of your TextView and if there are more than 1 insert a "\t".
In this post it's shown how to count the lines although is not an obvious question.
You also need to take into account if the tab is already inserted, because could be inserted more than one \b.
For example, lets say I have a for-loop and at every iteration(let's say for TextView t), I do t.setText(wordlist.get(i)). That just sets the value of the textview to whatever the last word in the list is. I want to set all the words next to each other. So what is the best way to do that?
Note: storing all the words in one big string is not an option
You can use the append method of the textView. append all the string each iteration
example:
t.append(wordlist.get(i));
why isnt it an option? for example take a look at this snippet:
String nums=""; for (int k=0;k<5;k++){
if (milist[k][0].getNumber()<10){
nums=nums+"0"+Integer.toString(milist[k][0].getNumber())+" ";
}else{
nums=nums+Integer.toString(milist[k][0].getNumber())+" ";}
}
tnums.setText(nums+"\n");
i have an edit text in my activity.i am entering numbers in it manually but
int mystart = destinationNumber.getSelectionStart();
int myend = destinationNumber.getSelectionEnd();
numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
"1", 0, 1);
its entering fine according to the cursor position.
i have a delete button in my acitivity which deletes single character according to cursor postion.
numberText.getText().delete(myend - 1, mystart);
But this logic is not working properly when i select whole text and call delete method it gives me IndexOutOfBoundsException OR i select 4-5 digits and call this delete.
I want the same functionality as android contact dialpad number enter field.Can someone help me figure out what is the correct logic to delete single digit from edittext and multiple selected digits as well.
Thanks
delete receives the start as first parameter and end as second, not the other way around.
Probably the error its taht mystart or myend(probably this) are bigger or smoller than numberText.lenght().
Try to put a Log.d("","" ) with the lenght of the text, mystart and myend and check if you need a myend -1 or something like that.