Android - Forms - android

on my app a have a form which contains 2 fields and a save button.
What do i need at the end of my onClick to return the cursor back to the first field.
i have this to clear them both
txtData.setText("");
txtData2.setText("");
but the cursor always stays on the bottom field
regards

Have you tried progamatically calling requestFocus on the view you want to have focus after the user clicks on your save button? For instance, you could in your onClick method do something like:
public void onClick(View){
//do other stuff like saving and clearing the fields
//then request the focus be switched back to the first text field.
txtData.requestFocus();
}

Related

Android - Handling event on an add button issue

I want to insert text from field into a String array every time the add button is pressed, and when done so, the field should empty itself for a new string.
Here is my code:
Editor note: no code provided as example, only a screenshot of the code
Is the add function ever called? Otherwise you are sitting with a button without an onClickListener.
Try assigning The onClickListener in oncreate instead.

Android - In-line autocomplete with EditText

I want to get an in-line autocomplete with an EditText, not a result list but the best suggestion directly in EditText.
Something like this : In-line auto-complete (near the bottom of the page).
Is it possible in Android ?
Thank you.
I have no eclipse now so I will try to give you some hints.
To create a custom autocomplete I would do something like this.
First
In the view layout add the EditText and an OutputText (this with visibility=hidden)
Second
In the activity create a TextWatcher and implement the method afterTextChanged.
Inside this method call a service with the input text and then update the content of the outputText.
Something like:
afterTextChanged(Editable s){
// you know your input is an EditText
final EditText input= (EditText) s;
// TODO make this call async
String suggestedText= someService.getSuggestion(input.getString());
outputText.setText(suggestedText);
outputText.setVisibility(View.VISIBLE);
// to avoid infinite loops
if(suggestedText!=null && !"".equals(suggestedText) && !suggestedText.equals(input.getString())
{
// add a onclick control to update the input
outputText.setOnClickListener(new View.OnClickListener(){
editText.setText(suggestedText);
});
}
}
Third
Implement the suggestion service.
Android has an AutoCompleteTextView which should do the job you want.
According to the official Android docs
"AutoCompleteTextView is 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.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold."
For an example code snippet refer to AutoCompleteTextView
For autocomplete you must another type of EditText called AutocompleteTextView or MultiAutocompleteTextView. Here you can find simple example for that option.
P.S. if you want to create your own type of List Filtration, your Adapter class must implement Filterable interface

required field validation on android

In android how do I create a required field validation? like on a page where use can enter some data into some EditText. I wanted to make sure user enter something before continuing, like if the user forgot to enter some data, the app would not crash. I have done other validation like number only using those input-type provided. but so far I research I only found ways to validate content entered but not whether there is something entered.
So I should put something in the onCreate method like
if(EditText text is !=null){ do the stuff} else {error message}
But if I did that, the moment the app is run there will be error displayed.
And how do I write the "text of EditText" in c# I believe is TextBox Text. But I do not know how to do that in java android. I know setText but do not know how to refer to the content without changing it.
To get text user entered in the EditText you can call getText() method. I recommend you to perform validation after user clicks some button. Validating content of EditText inside onCreate() method is useless.
It's always better to tell the user that they need to put the correct information the earliest possible. Why? Because it allows the user to quickly identify the problem and fix it. That being said, I would recommend checking if its null or whatever you want the textfield to contain as soon as it looses focus.
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//We get the text and then convert it to a string and check
if(v.getText().toString() == ""){
//Do what you want in this area
}
}
});
String str = textview.getText().toString().trim()

How to know, from addTextChangedListener, how the text has changed

I have a screen with an EditText and a ListView. As you type into the EditText, an AsyncTask is launched (any existing AsyncTask is cancelled and tossed) to query locally for some data to display in the ListView. If the EditText is empty of text, all data is queried for and displayed. Essentially, the EditText is a filter of the data on the screen. This is easy to do. In the addTextChangedListener, simply cancel your previous AsyncTask, launch a new one, and pass to it s.toString().
When the user hits the backspace to erase some text in the EditText, I want the default behavior (where all data populates the list). This works with my current implementation of addTextChangedListener.
However, I also have an X button on the right side of my EditText. This is intended to clear out the EditText (EditText.setText("");). This is a special case. In this case, I do not want the query to happen. The problem is, addTextChangedListener does not seem intelligent enough to know this. It gets triggered because the text has changed and all data is queried for.
How do I prevent this?
Use a boolean ignoreNextTextChangeKThxBye data member, setting/clearing that boolean as appropriate and using an if() statement to avoid doing the query.

How can I react when an EditText is being focused?

I have an EditText that the user can write in, when the app starts there is already a string in the EditText. When the user clicks the EditText it becomes focused and the curser is where the user clicked the EditText text box.
I know that the code for setting the curser to the start is :
editText.setSelection(0);
But I don't know where to put this code, I tried to it in beforeTextChanged but it didn't do the job.
You can do this by setting an putting an OnFocusChangedListener. You'd do something like this:
et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasFocus){
if(hasFocus){
((EditText)view).setSelection(0);
}
}
});
Where et is the text edit you want to set the listener on.
Full-discolsure: haven't tried this code out myself.
While there is probably a way to do this, I'm not entirely sure it's the best user experience, because when the user taps a text box at a specific spot, they really expect the cursor to be there. Imagine for instance if the user sees "abcd" written there and wants to edit that to "abcde", so they figure "I'll just tap at the end and append an 'e'". Imagine the user's frustration when that doesn't work as expected.
If you expect the user to edit the textbox, I'd consider leaving it empty. If you are using the existing text as a hint ("email#example.com"), it's probably a better idea to indicate that in some other way.

Categories

Resources