I have a EditText in my app which is supposed to take only numeric values; this is why I set the inputType property this way:
android:inputType="number|numberSigned|numberDecimal"
It works properly, except when the user presses enter, because it losts focus; is there any property to set to avoid this? Thank you for your answers.
What do you want to do? Keep the focus?
You can implement this method and check the value... if the value is not what you expect, you put back the focus.
public void onFocusChange(View v, boolean hasFocus) {
// ...
}
Regarding your comment:
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus && (v instanceof EditText)) {
((EditText) v).requestFocus();
}
Related
When I turn TalkBack on, once a selection is made in my Spinner, the focus immediately goes back to the top of the screen (toolbar). I would like the focus to be on the spinner immediately after the user's selection.
I have tried setting the spinner to be focusable:
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
I have checked out the following StackOverflow questions and answers which do not directly address the issue I have:
How can I use onItemSelected in Android?
Adding Focus to a spinner
I am noticing that the spinner is briefly disabled as the selection is set. I am guessing this is why the focus gets delegated to a different view. How can I circumvent this disabling or work around it?
Any help would be appreciated.
I was able to fix this by performing the following (hopefully this helps someone else with the same issue):
private boolean isUserSelection; // to detect user selection
spinner.setChangeListener(this);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
if (((AccessibilityManager)getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled())
{
spinner.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus && !isUserSelection)
v.performClick(); //bypasses the caret inside the spinner so user doesn't have to tap twice to open the drop down
else if (!hasFocus && isUserSelection)
v.requestFocus();
}
});
}
In the itemSelectedListener for the spinner, use this code:
if (isUserSelection && (AccessibilityManager)getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()))
durationSpinner.requestFocusFromTouch();
Where does the focus go to? Try requesting focus from there like this.
newlyFocusedView.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
newlyFocusedView.clearFocus();
spinner.requestFocus();
spinner.performClick();
}
}
});
Also try spinner.setFocusable(true);
I have added FocusChangeListener to EditText. like
etZip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
callAreaDetailService(zipCode,false);
}
}
});
Now If I have focus on etZip and I click on Button to go to anotherActivity. etZip loses focus and show Response alert in Next Activity.
I know I can add a boolean flag for not calling service if button is clicked. But I am sure there must be an android build in method to stop focus change.
There is no function like etZip.removeFocusChangeListener . So is there any way to do this using Android SDK.
Basically, I am validating a field when the the field loses focus. When an error occurs when we check at the OnFocusChangeListener, I want it to cancel the focus and stay on the same field. Is there a way to cancel the focus on it?
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
if(checkError(v))
{
// Cancel focus here
}
}
}
I think requestFocus() should do the trick, see this answer for usage: https://stackoverflow.com/a/8077842/1173391
Update:
I think you are missing the check for the view id, because the onFocusChange method is implemented via interface in your class:
try setting this:
if (v.getId() == myEditText.id)
as first check on the focus change method; you have to do it for every edit-field (or use a switch instead of if) ....
OR
attach an OnFocusChangeListener to every single field:
edittext1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
With a Button it is simple,
<Button
android:blablabla="blabla"
...
android:onClick="doSomething" />
this will preform the doSomething(View) function.
How can we mimic this with an EditText ?
I have read about this and i read that most people use an imeOptions (which still seems necessary) and then implement a actionListener on that EditText object.
This is were i'm lost.
Is there a way to implement the "Done"-action (or send or...) from our keyboard to a onClick function like we do with a Button, or do we need to explicitly implement the listener ?
Regards !
The below code will perform some action when you press the Done key in the softkeyboard.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do your actions here that you like to perform when done is pressed
//Its advised to check for empty edit text and other related
//conditions before preforming required actions
}
return false;
}
});
Hope it helps !!
I am assuming what you are wanting to do is run some code when the EditText is clicked?
If so, I have found a solution from another thread on the site:
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
then do this code here
}
}
});
via: A better way to OnClick for EditText fields?
I am trying to validate my edittext field after focus is changed but it gives when the time of typing only.i.e, after entering one charecter I am used below code
fname.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s){//some validations}
But my requirement, in my form so many fields are there if we go for validation submition time its a risky one. so please help me.
Add OnFocusChangeListener to your edit text
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If view having focus.
} else {
// If view not having focus. You can validate here
}
}
});