EditText fields not clearing when focus is changed - android

I've got two EditText fields and a button,
EditText topNumericInputField; // Top text field
EditText bottomNumericInputField; // Bottom text field
Button clearButton; // Regular Button
I'm trying to clear the text that's in them when I change focus, my onClick method looks like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topInputField:
clearTextBoxes();
break;
case R.id.bottomNumericInputField:
clearTextBoxes();
break;
case R.id.clearButton:
clearTextBoxes();
break;
}
}
private void clearTextBoxes() {
topNumericInputField.getText().clear();
bottomNumericInputField.getText().clear();
}
Now, it clears both fields fine with either:
a) The field I click on already has focus
or
b) I click the clear button
If I change focus from the top field to the bottom field, it gets focus, then I need to click it one more time.
I'm not sure exactly what's happening, but I'm sure it's related to something that keeps coming up in the LogCat log, every time I change focus two lines appear:
Tag: IInputConnectionWrapper Text: beginBatchEdit on inactive InputConnection
Tag: IInputConnectionWrapper Text: endBatchEdit on inactive InputConnection
I've tried searching SO and I've seen some submissions that are similar, but none seem to really be what I'm looking for.
Thanks!

I think problem is not with setting text but problem lies in capturing wrong event.
As you said you want to clear edittext when focus is changed, then you should override onFocusChange(View v, boolean hasfocus) to capture that event. You need to implement FocusChangeListener.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean focus) {
if (focus) {
((EditText)v).setText("");
}
}
});
A simple implementation should look like above. Refer this for detailed information

You can set the text null.
topNumericInputField.setText("");
bottomNumericInputField.setText("");

You just need to clear your Text by:
topNumericInputField.setText("");
bottomNumericInputField.setText("");

Related

Set Spinner focus to the Spinner itself after a user selection

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);

How to know when editText ends being editted?

I have two editTexts and when the user edits one and then hits done, it executes a method. I want to be able to know when the user stops editing it. Like in that case, with hitting the "done" button. Unfortunately, the user can 'stop' editing it, if he/she selects the other editText. For some strange reason, the OnEditorActionListener doesn't catch that case. What can I do about it? I've tried with onFocusChange, but that one is very unpredictable...
You can assume when the EditText looses focus
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//probably here!
}
});

onFocusChange of EditText never called

I like to detect user's focus on the EditText using
IDEditTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Toast.makeText(getBaseContext(),
((EditText) v).getId() + " has focus - " + hasFocus,
Toast.LENGTH_LONG).show();
}
});
But when I set the cursor on it, onFocusChange is never get called. When I research, found that EditText xml should have
android:focusableInTouchMode="true"
android:focusable="true"
So I put these two lines in the EditText's xml, but still not called.
My intention is if the user focus on the EditText, I like to display the AlertDialog rather than keyboard, so that user's choice on the dialog will be displayed on the EditText. How can I implement that? Where can I have similar program for that implementation?
Thanks

Android: How disable EditText depending focus

I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?
Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});

OnClick event only works second time on edittext

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
How do I fix this?
Simply try to add this to your XML file. Your keyboard pops up when widget gains focus.
So to prevent this behaviour set focusable to false. Then normal use OnClickListener.
<EditText
android:focusable="false"
...
/>
Now, it should works.
You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
Nothing much to do you just have to
edt.setFocusable(false);
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
make your alert dialog box appear on
setOnFocusChangedListener()
You should add onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText to receive focus before your onClick call.
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
It maybe helpful to you.
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for #neaGaze. This worked for me in my_layout.xml :
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>

Categories

Resources