OnTextChang for edittext in android? - android

I have 8 edit texts each allow only one character.when user enter something it automatically moves to next edit text. It's work fine by using onTextchanged.But i need to move backward also like when user enter back button it automatically moves to previous edit text and when enter something it move again next edit text. How i can do it.Please can any one help.
Any help would be highly appreciated.
editText_Pin1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count) {
if(editText_Pin1.getText().toString().length()==1) { //size as per your requirement {
editText_Pin2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start,int count, int after) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});

Try something like this...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(editText_Pin2.getText().toString().length()==1){//Here you will check the last editField which has lenght == 1
editText_Pin1.requestFocus();
return false;//This will make sure your activity doesn't gets finished
}
}
return super.onKeyDown(keyCode, event);
}
Hope this helps..

you can do it by oveeriding the onBackPressed method
#Override
public void onBackPressed() {
//check here which edittext has current focus,change the focus to previous edittext
//you can use edittext array also.And also check if it is the first edittext then do nothing
return;
}

You have several options:
Activity.onBackPressed()
Activity.onKeyDown() or Activity.onKeyUp()
View.setOnKeyListener() on each of your EditTexts
View.onKeyPreIme()
I'd probably go with #1 or #3

Related

Android addTextChangedListener not fired when EditText Invisible

I am building a PIN entry screen that has an invisible EditText to collect the PIN and four ImageViews that I populate as the user enters the PIN. It all works fine when the EditText is visible but when I make it invisible the addTextChangedListener does not fire?
XML
<EditText
android:id="#+id/pinEntryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:visibility="invisible"
android:maxLength="4"
android:inputType="numberPassword"/>
Code
mPinEntryEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
switch (s.length()) {
case 1:
setPinFieldColor(mPinOneImageView, R.color.white);
break;
case 2:
setPinFieldColor(mPinTwoImageView, R.color.white);
break;
case 3:
setPinFieldColor(mPinThreeImageView, R.color.white);
break;
case 4:
setPinFieldColor(mPinFourImageView, R.color.white);
break;
}
}
});
I'm assuming you don't want to have the EditText be visible since you want the input to go straight to the ImageViews.
Can I maybe recommend an external library like https://github.com/alphamu/PinEntryEditText?
--
If you want to make a homegrown solution, maybe you can just show the keyboard and listen for keyboard input with onKeyDown?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return super.onKeyDown(keyCode, event);
//Logic...
}
This seems to be intentional behavior.
The TextView source (EditText extends TextView) overrides onVisibilityChanged():
#Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (mEditor != null && visibility != VISIBLE) {
mEditor.hideCursorAndSpanControllers();
stopTextActionMode();
}
}
As you can see, if the visibility of the View isn't VISIBLE, stopTextActionMode() will be called, which basically stops all updates to the View. This is probably to save on resources, since, usually, when a View is invisible, you don't need to be listening for changes in its state.
Unfortunately, it doesn't seem like you can override this behavior, although I don't really see why you need to receive updates from an invisible text input.

Android: soft keyboard Backspace on EditText not working

This is what happens:
Activity A and B have an EditText and they both have IME_SEARCH. The input is done only via soft keyboard on a SAMSUNG tablet.
On Activity A I can use the EditText without problems. The thing is that on Activity B I can't erase text with backspace after I hit 'space' or whenever I use a word from the suggestions. It behaves like there wasn't text there anymore. If I type new characters, I can erase them up to the space.
Important points:
The View hierarchy that contains the EditTexts are identical
The code that configures the IME_SEARCH processing (via setOnEditorActionListener) is identical
The TextWatcher of both are also identical
In the Manifest, both activities are configures with
android:configChanges="keyboardHidden|keyboard|orientation"
android:windowSoftInputMode="stateAlwaysHidden|adjustUnspecified"
I set a breakpoint on the method beforeTextChanged of both TextWatcher. I inserted a 'space' and hit 'backspace'. On the Edittext of activity A, the method is triggered but on activity B's it is not triggered. I can't see the reason for this to happen since the properties of both Edittext are configured identically.
I also tried removing the IME option but the behavior kept the same.
Does anyone know what could be happening?
EDIT 1:
searchTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
btnClear.setVisibility(View.GONE);
} else{
btnClear.setVisibility(View.VISIBLE);
}
}
});
searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
buildGrid();
return true;
}
return false;
}
});
EDIT 2:
The layout hierarchy is as following.
<LinearLayout
... >
<include layout="#layout/title_bar" />
<RelativeLayout
...>
<EditText
...>
The problem was that, for some reason, the Activity B was overriding dispatchKeyEvent() and always returning true. Removing it solved the problem.
I have similar problems that you are facing and I somehow managed to stumble on the solution. Apparently, I had setOnKeyListener to 'return true'. After I changed it to 'return false', the phone keyboard works perfect with backspace functioning properly once again on edittext. Hope this helps:
.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
...
return false;
}
});

Edittext worked as searchview by post the search api with some params

I used EditText instead of SearchView. But here the problem is different I used to search in ListView and data in the ListView is coming when I hit the search api..let me explain..
Actually I have an api the when I put char in EditText , I get that char from EditText and hit the api in postman by passing the parameters in body, two params I have passed("key"=the char i get from EditText, user_id=null).
And I want to search the data like when I press "m" then "m" will post as value of "key" and shows the data , at the same time if i press "mo" then the api again hits at the same time and get me the results in ListView
Please help me...
mSearchFriends.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
String input;
if(actionId == EditorInfo.IME_ACTION_DONE)
{
input = String.valueOf(mSearchFriends.getText());
new PostSearchApi(Add.this) {
#Override
public void fetchData(String output) {
Log.d("response>>>>>",output);
//input = String.valueOf(mSearchFriends.getText());
rowItemListForSearchList = new ArrayList<>();
mSearchListAdapter = new SearchListAdapter(getApplicationContext(),rowItemListForSearchList);
getjsonOfContact(output);
lvContacts.setAdapter(mSearchListAdapter);
mSearchListAdapter.notifyDataSetChanged();
mTextView_No_Result_Found.setVisibility(View.GONE);
}
}.execute(input,"");
It shows me result but after pressing the the arrow button on phone keyboard. What I want is I want to search random data when I type a character. I also tried the code in OnTextChanged method but when I type "a" then api hits and takes time when it completed then only i can type other character..
But the necessity is the functionality like finding the friends on facebook
Since no code provided by you, as far as I understand your ques, you can get realtime text changing events from EditText like this:
yourEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//on Text change event. s is the text that is entered
// put your code here. Make a hit to your api and pass `s` as your parameter.
}
#Override
public void afterTextChanged(Editable s) {
}
});

How to disable button in android?

I have a login page,having 2 textfields username,password and button "login" button.
when the username or password not entered alert is poping up,
when i suddenly clicks on this login button 2 alerts are poping up,
since more than click is taken.
I need to disable the button and enable during next click. What should i do?
in order to disable a button, you have to call
yourButton.setEnabled(false)
on a Button instance. To re enable it call:
yourButton.setEnabled(true)
create a method as below
boolean checkit= true;
private void showHide1() {
if (checkit) {
// first click
} else {
// second click
}
checkit= !checkit;
}
and on you button click add the method showHide1();
You can combine blackbelt's solution with mine.
Add a TextWatcher to your EditText and enable or disable the button when text is entered in the EditText.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
charCount = Integer.valueOf(s.length());
if (charCount > 0) {
// ENABLE THE BUTTON
boolean blnButtonStatus = YOUR_BUTTON.isEnabled();
if (blnButtonStatus== false) {
YOUR_BUTTON.setEnabled(true);
}
} else if (charCount == 0) {
// DISABLE THE BUTTON
YOUR_BUTTON.setEnabled(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// DO NOTHING HERE
}
#Override
public void afterTextChanged(Editable s) {
// DO NOTHING HERE
}
This way, the code will automatically disable the Button if there is nothing typed in the Password and /or Username fields. No accidental clicks.

OnKeyListener problem enabling a button

Thanks in advance for your answers. I am working on a basic tax calculator to help me better understand the fundamentals to the Android SDK. The user enters a subtotal and a tax rate (.08) and they press calculate to calculate the solution. I disable the calculate button until both fields have contents. My problem is that when the user enters numbers for both of the fields, the calculate button is still disabled. I posted the code I currently am running (the OnKeyListener). The edittext fields are registered to the listener.
private OnKeyListener mKeyListener = new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
switch(v.getId())
{
case R.id.txtSub:
cmdSubmit.setEnabled(txtTax.getText().length()>0
&& txtSub.getText().length() > 0);
break;
case R.id.txtTax:
cmdSubmit.setEnabled(txtTax.getText().length()>0
&& txtSub.getText().length() > 0);
break;
}
return false;
}
};
If you look at the spec for OnKeyListener you'll notice that it gets invoked BEFORE the KeyEvent is given to the view. Other than that this looks right: I would suspect you aren't binding the listener to the right view. Put a break point in the method and make sure it is getting invoked correctly.
I've personally found the TextWatchers a much nicer way of keeping track of whether an EditText has data or not:
TextWatcher tw = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
cmdSubmit.setEnabled(!Utils.isStringBlank(txtTax.getText().toString()) &&
!Utils.isStringBlank(txtSub.getText().toString()));
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
};
txtTax.addTextChangedListener(tw);
txtSub.addTextChangedListener(tw);

Categories

Resources