How to search and Highlight Text within an EditText - android

I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
grab the text from the (EditText) assign it to a string
search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
if found, start the (EditText).setSelection highlight beginning on the returned position for the length of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated

grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
http://developer.android.com/reference/java/lang/String.html
http://developer.android.com/reference/java/util/regex/package-summary.html

Related

Skip suggestion string value from Android EditText

I have an Android EditText with suggestions enabled. My goal is not to disable suggestion (since i'm able to do that), but to skip some string values from them.
For example, suppose that i'd like to skip 'hello' word.
If i write 'he' in the EditText, my suggestions list shouldn't contain 'hello' since it's a string that has to be skipped. Is it possible?
Hope i explained myself, thanks

Hide only part of text in TextView

Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);

Android EditText - Capitalize first letter of sentences using setText()

Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}

SearchView widget - any way to SELECT ALL text inside?

I'm using SearchView widget (new in Honeycomb). I can set initial text by setQuery, that works.
But is there some way to select all text inside (similar to EditText.selectAll)?
I'd like to give user preset text from previous search, yet easy way to type new text without need to delete old.
You just need to capture the inner EditText, then do anything you want.
EditText edit = (EditText)searchView.findViewById(R.id.search_src_text);
edit.selectAll();
A little late but i had a similar issue.
I'd like to give user preset text from previous search, yet easy way to type new text without need to delete old.
I found a solution that worked for me. You just need to access to the inner EditText and then select all the text in usual way.
To do this try something like the following.
Please note that my code is in c# not in Java but its similar.
int id = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
EditText editText = searchView.FindViewById<EditText>(id);
editText.SelectAll();
In Java it should be something like this:
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
editText.selectAll();
I have just testet it on API level 15! But i think it works on lower levels, too.
What you want to do works well on PCs, but but is not very convenient on touch devices.
I'd use suggestion provider to allow user to reuse recent searches, possibly with query refinement enabled.

Reading from and writing to EditText

What is the best way to read the text from an EditText into code, and to write some text from code to the EditText?
Sorry I have ment not the TextView but the EditText
Hi all
I am a new to android I wish to write automatically from code to EditText and read in code from EditText
What is the best way to do it.
Java classes usually expose readable attributes with a get* method, and writable attributes with a set* method. In the case of a EditText these are:
getText
and
setText
see here and here (they are inherited from TextView)
Note: Scroll around a bit. You will see that they are defined multiple time. With different parameters. Pick the one you need.
A simple example. Let's assume you have a TextView with the id myTextField:
EditText myText = (EditText) this.findViewById(R.id.myTextField);
// Setting the text:
myText.setText( "Hello World!" );
// "Reading" the text (printing it to stdout):
System.out.println( myText.getText() );

Categories

Resources