I built a string based numberPicker with all country names, and an editText to function as a search field. I want the numberPicker to refresh\update itself according to what letters the user enters to the search editText. I.E suppose the user types in the letter "I", the picker should show the first country that starts with the letter "I", and update its results according to the rest of the string, like it shows suggestions.
editTextCountryInput = (EditText) findViewById(R.id.editTextCountryInput);
String[] countriesForPicker = new String[236];//array with all countries names
//this fills the country picker with names
private void generateCountryPicker() {
picker.setMinValue(0);
picker.setMaxValue(countriesForPicker.length()-1);
picker.setDisplayedValues(countriesForPicker);
}
Is there any way of doing it?
Posting as an answer so you can close this out*.
You should be able to use a text changed listener like this guy: Android: On EditText Changed Listener
*I'm not too concerned about the points, I just prefer it when people can tell the question has been answered
Related
I want the user to type in his or her Facebook account-link (don't have a better solution atm).
Now, when the user clicks the edittext it is supposed to say : "www.facebook.com/". Now the cursor is supposed to be at the END of the edittext (after the "/") and the user is not supposed to delete the first letters so that the "www.facebook.com/" stays exactly where it is. This will have the user to ONLY type in his or her facebook name and therefore connect the profile.
Is there a way of doing this?
Thank you :)
You can do that by using the event "TextChanged" and in your code validate if the size is big than your string, like that:
if (((EditText)sender).Text.Length >= 17)
{
((EditText)sender).Text = e.NewTextValue;
}
else
{
((EditText)sender).Text = "www.facebook.com/";
}
So if the value is bigger than your string you will replace that for the value, if not you just set the value with your string
How to set the string values on spinner prompt?
The process how I am doing is:
I am able to retrieve the json data(it has only one single value), so I am getting the single text with the help of String.
Later, I am trying to set the string value in Spinner prompt
SP_gender is called as Spinner here. In String gender1, I have texts called as "male"
String gender1 = i.getStringExtra("bundle_CusGender");
SP_gender.setPrompt(gender1);
System.out.println("Check bundle_CusGender = : " + gender1);
When I try to print this, I am getting a value as male
System.out: Check bundle_CusGender = : male
How should I set the single text in spinner Android?
Firstly the setPrompt() methods documentation states:
Sets the prompt to display when the dialog is shown.
Which is pretty vague but internally it calls the setPromptText() method which sets the description on the popup and has the following documentation.
Set hint text to be displayed to the user. This should provide
a description of the choice being made.
So if I am understanding your question correctly you want to set the spinners selected item. You should be using either setSelection(int) or setSelection(int, boolean)
For those to work you would need to give your spinner an adapter if you haven't already. If you don't know how you should check out this answer which explains in more detail.
final String[] defaulttask1 = {"abc"," def"," ghi"};
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)this.findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,defaulttask1);
autoCompleteTextView.setThreshold(0);
autoCompleteTextView.setAdapter(adapter);
I have a method using autoCompleteTextView.getText().toString().While the user types words which is not in the array, it may happen "null pointer exception"
Is there any solution that I can use autoCompleteTextView, that can search for user and let user enter text they want?
P.S. sorry, I'm not a native speaker, there might be some grammar mistake.
AutoCompleteTextView is right solution for your needs
An editable text view that shows completion suggestions automatically
while the user is typing. The list of suggestions is displayed in a
drop down menu from which the user can choose an item to replace the
content of the edit box with.
AutoCompleteTextView will allow user to type whatever he/she want but will give suggestions from array.
autoCompleteTextView.getText().toString()
will not return null in case of user types words which is not in the array. It will return whatever text is there right now in text view. It might crash if you do getText().toString() on a empty text view. So have a check before you convert textview to string
if(autoCompleteTextView.getText()!=null)
String textviewString = autoCompleteTextView.getText().toString()
Can someone explain the difference between MultiAutoCompleteTextView and AutoCompleteTextView?
AutocompleteTextView only offers suggestions about the whole sentence and MultiAutoCompleteTextView offers suggestions for every token in the sentence. You can specify what is the delimiter between tokens.
String[] words=new String[] {
"word1", "word2", "word3", "word4", "word5"
};
MultiAutoCompleteTextView macTv = (MultiAutoCompleteTextView) this.findViewById(R.id.mac_tv);
ArrayAdapter<String> aaStr = new ArrayAdapter<String>(this,android.R.layout.dropdown_item,words);
macTv.setAdapter(aaStr);
macTv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer() );
and:
<MultiAutoCompleteTextView
android:id="#+id/mac_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>
with this example the suggestion comes after every comma.
The choice between using the AutoCompleteTextView or the MultiAutoCompleteTextView comes down to whether or not the user should be allowed to input only "one item" as provided by the adapter, or "multiple items."
So for example, if you were writing an email app, and you wanted the "To:" field to be an autocomplete field, pulling matches from an address book, chances are you want to allow the user to pick multiple recipients for a message, and would make this field a MultiAutoCompleteTextView.
On the other hand, the "From:" field in the same example email app, you would need to enforce only a single selection by the user from their configured email accounts. And so an AutoCompleteTextView would be appropriate here.
Difference between AutoCompleteTextView and MultiAutoCompleteTextView
AutoCompleteTextView Vs MultiAutoCompleteTextView
AutocompleteTextView only offers suggestions about the whole sentence MultiAutoCompleteTextView offers suggestions for every token in the sentence. You can specify what is the delimiter between tokens.
AutoCompleteTextView is used for selecting single Item MultiAutoCompleteTextView is used for for selecting multiple Items by using a delimiter(such as comma) in betwwen them.
the “From:” field in example of email app, you would need to enforce only a single selection by the user from their configured email accounts. If you were writing an email app, and you wanted the “To:” field to be an autocomplete field, getting matches from an address book, chances you want to allow the user to pick multiple recipients for a message, and would make this field a MultiAutoCompleteTextView
AutocompleteTextView only offers suggestions about the whole
sentence
MultiAutoCompleteTextView offers suggestions for every token in the
sentence.
You can specify what is the delimiter between tokens also set the first or any no. of characters using setThreshold() with MultiAutoCompeleteTextView control in Android.
I want to execute one method wheneer i enter number in EditText view.But i did not change the focus of EditText.I am stay in the same EditText when i enter a new number automatically i eant to execute one function.Now my question is how we find that event that means enter a new number in EditText
You need to associate a TextWatcher to your EditText, and then, check in onTextChanged() if the character added was a number. You could use a regular expression for that :
if(s.matches("[0-9]")){
doYourStuffMethod();
}
You can also use the Matcher class from Android SDK which is an helper for this kind of stuff.