I'm creating an application that checks if the user inputs a letter or a number then a button will pop-out. And if the user didn't enter a single letter a validation will show.
Here's an example of what I'm trying to debug[main activity. PS. not my real code]:
private EditText edittext;
Button checkBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addKeyListener();
}
public void addKeyListener() {
edittext = (EditText) findViewById(R.id.editText);
checkBtn = (Button) findViewById(R.id.button1);
checkBtn.setVisibility(View.INVISIBLE);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
checkBtn.setVisibility(View.VISIBLE);
return true;
else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
// display a floating message
Toast.makeText(MyAndroidAppActivity.this,
"Number 9 is pressed!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
}
Question: Is there a way to make the button show automatically when a user types a letter without clicking the enter key. And if the user didn't input a letter a validation will pop out saying invalid and the button will not display.
I tried looking for the right answer but still I couldn't find it.