Use AutoCompleteTextView but hide characters on the keyboard - android

I'm using AutoCompleteTextView that the user can see the opportunities.
So when I tap on two characters e.g. "ba" I will see "Bahamas", "Bahrain","Azerbaijan" etc - this works!
But if I don't have a country starting with the letter "z" I will hide the z on the keyboard. And if I tap the two characters "ba", I will only see "h" on my keyboard.
How can I do that? And how can I realize it if I still want to tap "ba" and will get "bahamas" AND "azerbaijan"?
Thanks everyone!

as Cata pointed out, you won't be able to hide the keys on the softkeyboard unless you write your own keyboard - you do have a couple of other options however:
you could put a text watcher on the the edit text, and if they key they type isn't in any of the words, you could delete it
you could use setKeyListener http://developer.android.com/reference/android/widget/TextView.html#setKeyListener%28android.text.method.KeyListener%29 to and validate their entries.
Both of these will not hide keys from the keyboard but you could use them to prevent the user from typing an invalid key.
As for your second question about matching the user input to the middle of a string, this is not in the code of the autocomplete:
Relevant code form the android source:
for (int i = 0; i < count; i++) {
final T value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
you can see it will only match the first characters of any individual word in an item. So to achieve your second goal, you would have to write your own adapter that implements filterable.
here is a tutorial that might get you started on that: http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

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

Restricting Android NumberPicker to Numeric Keyboard for Numeric Input (not Alpha keyboard)

Is there a way to suggest or restrict keyboard input when selecting a NumberPicker so only the number controls are shown when entering values, similar to how you can use android:inputType="number" with a EditText?
I have a series of values, from 0.0 to 100.0 in 0.1 increments that I'd like to be able to use a NumberPicker to select in Android 4.3. In order to have the numbers selectable, I've created an array of strings which corresponds to these values, as shown below:
NumberPicker np = (NumberPicker) rootView.findViewById(R.id.programmingNumberPicker);
int numberOfIntensityOptions = 1001;
BigDecimal[] intensityDecimals = new BigDecimal[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i++ )
{
// Gets exact representations of 0.1, 0.2, 0.3 ... 99.9, 100.0
intensityDecimals[i] = BigDecimal.valueOf(i).divide(BigDecimal.TEN);
}
intensityStrings = new String[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i ++)
{
intensityStrings[i] = intensityDecimals[i].toString();
}
// this will allow a user to select numbers, and bring up a full keyboard. Alphabetic keys are
// ignored - Can I somehow change the keyboard for this control to suggest to use *only* a number keyboard
// to make it much more intuitive?
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
np.setDisplayedValues(intensityStrings);
np.setWrapSelectorWheel(false);
As more info, I've noticed that if I dont use the setDisplayedValues() method and instead set the integers directly, the numeric keyboard will be used, but the problem here is that the number being entered is 10 times more than it should be - e.g. if you enter "15" into the control its interpreted as "1.5"
// This will allow a user to select using a number keyboard, but input needs to be 10x more than it should be.
np.setMinValue(0);
np.setMaxValue(numberOfIntensityOptions-1);
np.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int value) {
return BigDecimal.valueOf(value).divide(BigDecimal.TEN).toString();
}
});
Any suggestions on how to raise a numeric keyboard to allow a user to enter decimal numbers like this?
I have successfully achieved this, borrowing heavily from #LuksProg's helpful answer to another question. The basic idea is to search for the EditText component of the NumberPicker and then to assign the input type as numeric. First add this method (thanks again to #LuksProg):
private EditText findInput(ViewGroup np) {
int count = np.getChildCount();
for (int i = 0; i < count; i++) {
final View child = np.getChildAt(i);
if (child instanceof ViewGroup) {
findInput((ViewGroup) child);
} else if (child instanceof EditText) {
return (EditText) child;
}
}
return null;
}
Then, in my Activity's onCreate() method I call this and set the input type:
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
EditText input = findInput(np);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
That did the trick for me!
My answer is to improve on the basis of Alan Moore, just change the last line
input.setInputType(InputType.TYPE_CLASS_NUMBER);
to
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
then there will no more problem like personne3000 said, no more crashes.
I'm sorry my English is not good, but I hope you can understand what I mean.

Format Android EditText to specific pattern

I need the user to enter text in an EditText according to this specfic pattern:
123.456-7890-123.456
The user can input any number of integers, so they could as well enter 123.456-7
I do not want the user to enter . or - just the numbers, like an input mask.
Also the numeric keyboard should only show.
I've searched StackOverflow extensively and have seen examples that use InputFilter, ChangedListener, TextWatcher but have not found anything simlar to what I'm trying to do. I've tried in various implementations of what I've found, but I'm inexperienced in using these so I may have overlooked something.
Any suggestions would be welcome.
You're going to have to use a TextWatcher and a regular expression pattern matcher to accomplish what you're trying to do.
This answer should be helpful: Android AutoCompleteTextView with Regular Expression?
You can create your own class that implements InputFilter. Then you would apply it as follows:
MyInputFilter filter = new MyInputFilter(...);
editText.setFilters(new InputFilter[]{filter});
Refer to the docs for how InputFilter is intended to work, then refer to the source code for some of the InputFilters used in Android for some ideas how to implement them.
After many failed attempts to implement InputFilter or Regular Expressions I opted for something a little more straight forward:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String a = "";
String str = id.getText().toString();
String replaced = str.replaceAll(Pattern.quote("."),"");
replaced = replaced.replaceAll(Pattern.quote("-"),"");
char[] id_char = replaced.toCharArray();
int id_len = replaced.length();
for(int i = 0; i < id_len; i++) {
if(i == 2 || i == 12) {
a += id_char[i] + ".";
}
else if (i == 5 || i == 9) {
a += id_char[i] + "-";
}
else a += id_char[i];
}
id.removeTextChangedListener(this);
id.setText(a);
if(before > 0) id.setSelection(start);
else id.setSelection(a.length());
id.addTextChangedListener(this);
}
I don't know if this is the best approach but it does work. One problem I still haven't solved is how to handle cursor placement after the user deletes or inserts a number. If the user inserts the cursor somewhere in the EditText and enters a new number the cursor jumps to the end of the EditText. I would like the cursor to stay where it is at. Another problem if the user inserts the cursor within the EditText number and backspaces to delete a number, then the first key entry doesn't work and on the second key the number is entered. I can only guess this has to do with focus?
Use this: https://github.com/alobov/SimpleMaskWatcher.
Just set your mask for this watcher (###.###-####-###.###). It will add special symbols automatically and wont check your input string for being complete.
But showing the numeric keyboard you must handle by your own using android:inputType="number" tag for your EditText.

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 -

How can I pull individual characters from a string and convert them to numbers?

What I'm trying to do is find a way I can take the word "camel" for example from a EditText field and make for instance c=2 a=1 m=4 e=5 l=3. Is there anyway I can pull the individual characters from a string and convert them to numbers?
I've tried using "split" to separate each character into an array but I can't figure out how to convert the letters into numbers
so I can do something like:
a=1
b=2
c=3
int temp = (int)(array[1]+array[2]+array[3]+etc...)
using the example of "camel" would equal 15
This is what I have so far:
String name = inputarea.getText().toString();
String[] array = name.split("");
for(int i =0; i < array.length ; i++)
The biggest problem I keep having is if I try to pull from the 7th position in the array and nothing is there. (camel only has 5 characters) then I get a nice big error.
Thank you for any help that can be provided.
Edit: I figured it out after a few hours of playing with it here is my working code:
String firstname = inputarea.getText().toString();
char[] array = firstname.toCharArray();
final char[] array2 = new char[15];
System.arraycopy(array, 0, array2, 0, array.length);
if (array2[0] == 'A' ) {
array2[0] = '1';
}
suggestion:
first, need define all letter, from a-z (A-Z), the ASCII code 'a' to 'z' is 97 to 122, if you want support the upper letter, you need add A-Z.
then, get the letter in the string, u can use this:
for(int i=0;i<string.length();i++){
int number = string.charAt(i);
}
when you get the number size, you can reduce to the base number('a' is 97), you will get the individual number
Does String.charAt() works for you?
As for converting to number, if the numbers are consecutive you can define a fixed string with all the characters you want to map and use String.indexOf(). If not, you can have a parallel array with ints or use a Map.

Categories

Resources