Use "OK" key on softkeyboard (numberPassword keyboard) instead of clicking button? - java

I have an EditText which InputType is numberPassword, and a Button"ANYNAME". I'd like to use "OK" key on softkeyboard (numeric keyboard) instead of the Button"ANYNAME" to execute a giving function in my code.
How can I realize that?

mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// call some function
return false;
}
});
if you want to change "Done" button title, use:
mEditText.setImeActionLabel("OK", KeyEvent.KEYCODE_ENTER);

Give some proper explanation...!!!
use Toast Message to know Action is performed or Not.

Related

How to disable a button for the user, until editText is active?

I want to make it impossible for the user to click on a button until he does not 'get out of' or clicks away from an EditText.
Like, the button will be there, but when the user goes to enter text in an EditText, it will be grayed out, and will be clickable again only when the user leaves the editText.
I hope my question is clear. How can I do this?
Your button behaviour depends on the focus of the editext so you can use
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mButton.setEnabled(false)
} else {
mButton.setEnabled(true)
}
}
});
the if is extended, consider that you can solve in just one line
mButton.setEnabled(!hasFocus)
Set OnFocusChangeListener on your EditText:
editView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
button.setEnabled(!hasFocus);
}
});
Use setOnFocusChangeListener and override the onFocusChange. Then, use the button.setEnabled

After click on editText is done when going to another edittext

I'm having a question where I couldn't find the answer online or know how to find it..
I have EditText xml attribute and I made an event listener
to this attribute by changing the color of an underline beneath it. Is there a way when the focus is removed from this EditText (i.e user click on any other element rather than this one) to remove the highlighted color for the line I colored?
On the onclick event listener? It seems weird, but I want the opposite of the onclick like onclickremove or something.
You can use the the setOnFocusChangeListener to your EditText. If lost focus,clear the color filter:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
else{
editText.getBackground().clearColorFilter();
}
}
});
If you want to change your view color, just add the below line in onFocusChange:
view.setBackgroundColor(Color.parseColor("#ffffff"));
Hope this helps.
You need to use setOnFocusChangedListener for this. The hasFocus determines whether the focus is removed or given to a view. It being false indicates that user has left the field.
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus == false){
// change the color
}
}
});

How to clear android textedit field

I am traing to clear textedit field when it is focused. I know about hint option, but I wanna clear textedit everytime when user actives it, also if is filled by user (now user have to clear field manualy everytime he wants change value).
I load fragments in my app so there are a lot of edittext fields which I wanna clear on focus, so is there universal method to do this, or I have to do it to all fields severally?
Should I create another java file to this method or put inside onCreate?
You need to add an onClickListener and set the textfield to an empty string when clicked. You need to add the listener for each textfield but you can reuse the same listener because the functionality is the same. You can set the listener on onCreate, that's fine.
You should you OnFocusChangeListener as is shown in code below:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText)v).setText("");
}
}
});
Simply set an View.OnFocusChangeListener with the method setOnFocusChangeListener(View.OnFocusChangeListener l).
In the callback onFocusChange(View v, boolean hasFocus) you can do the following:
if (hasFocus && v instanceof EditText) {
((EditText) v).setText("");
}

Implementing Search Button on my Keyboard

I'm trying to implement a search button in place of the regular enter button on my inbuilt android keyboard. I tried doing:
resultView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
System.out.println("You searched for this!");
return true;
}
return false;
}
But the regular 'enter' button is still appearing. I do not want to use XML and i'm creating my UI completely on JAVA. What should i do? Help would be appreciated. Thanks!
EditText view = new EditText(this);
view.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
view.setSingleLine(true);
And you are good to go, you set SingleLine attribute to change the new line button "default behavior in multi-line editext" to search button .
Sounds like you need to set the imeOptions programmatically. Try something like this:
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
You might have to play around with the available options to get exactly what you want.

TextView editable onLongClick -- But one small issue when BACK button pressed

I have been search SO for days and have finally compiled enough answers to accomplish what I wanted. First off, it seems to be an often asked question but not really answered (at least not the way I was looking for it). I thought I would share my findings but I also have one small issue left that I would like to ask for help with. Here goes:
I have a TextView which displays a score. It starts at 0 and at an onClick event the score increments and updates the TextView (score is tracked as a byte - valScore).
onLongClick: This was the challenge. I want the user to be able to do a LongClick to correct/change the score. I first found a solution that utilized another layout.xml file with just an EditText element and the OK and CANCEL buttons. This was very cumbersome to change the score as it involved the LongClick, then the dialog opens, then you had to click on the EditText element to open the keyboard, then you enter the value, click DONE and then click OK. I shortened it by figuring out how to open the software keyboard automatically when the dialog opened. However, you still had to click DONE and then OK. I didn't like this action so I continued searching.
Days later I came up with a bit of code and then more and with a lot of playing/hacking around I came up with the following solution:
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
tvScoreHome.setInputType( InputType.TYPE_CLASS_NUMBER );
tvScoreHome.setFocusable(true);
tvScoreHome.setFocusableInTouchMode( true );
tvScoreHome.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(tvScoreHome, InputMethodManager.SHOW_FORCED);
tvScoreHome.setText("");
tvScoreHome.setOnEditorActionListener( new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
valScoreHome = Byte.valueOf( tvScoreHome.getText().toString() );
// This part will hide the keyboard after input
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
tvScoreHome.setFocusable( false );
tvScoreHome.setFocusableInTouchMode( false );
tvScoreHome.setText( Byte.toString(valScoreHome) );
return true;
}
return false;
}
});
return true;
}
});
This works EXACTLY how I want. User performs LongClick the keyboard opens, the user enters the new value and clicks DONE. The TextView is updated and it works great!
The problem arises if the user changes their mind and hits the BACK button on the device. The keyboard closes (GOOD), but then the focus remains on the TextView instead of removing the focus like I do if the DONE button is pressed. So if you cancel out of a change every click after that results in the keyboard opening again instead of just incrementing the score -- until you actually type a value into the keyboard and click DONE (then the regular behavior takes over again. I need to setFocusableInTouchMode to FALSE if the BACK button is pressed.
The other issue is that the setText() method is executed even if the BACK button is pressed if a different value has been typed in. Even though valScoreHome isn't updated the TextView changes. On the next increment it goes to the correct number again, but the setText() should not execute if the BACK button is pressed.
Can someone help me figure this out please?
Both issues can be handled by subclassing TextView.
The back button press that closes the keyboard is handled by overriding onKeyPreIme.
To avoid updating the text when the user closes the keyboard, the score value is saved in the variable mScore, but only if the TextView is currently not focusable. That means, the TextView "remembers" the current value of the score, that was not entered by the user. When the user closes the the keyboard, the text is set back to the saved value.
public class ScoreTextView extends TextView {
private CharSequence mScore;
public ScoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setText(CharSequence text, BufferType type) {
if (!isFocusable()) {
mScore = text;
}
super.setText(text, type);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setFocusable(false);
setFocusableInTouchMode(false);
setText(mScore);
}
return super.onKeyPreIme(keyCode, event);
}
}

Categories