android - getting soft keyboard key presses - java

I am trying to get the key pressed on soft keyboard but am unable to do so.
Currently i am using the following code
#Override
public boolean dispatchKeyEvent(KeyEvent KEvent)
{
int keyaction = KEvent.getAction();
if(keyaction == KeyEvent.ACTION_DOWN)
{
int keycode = KEvent.getKeyCode();
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
char character = (char) keyunicode;
System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" + keycode);
}
return super.dispatchKeyEvent(KEvent);
}
It is catching key events for the hardware keyboard but not for the virtual one. Can someone help me out

From the Android Official Page
Note: When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard).
So you should use TextWatcher Interface to observe the characters pressed on the SoftKeyboard, example:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

This should be your solution:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

Android disable text watcher when there are letters

I need help on my code.
I have an editText where the user enters the opening and closing time of the store and there is a checkbox with "Close".
The user enters the opening time 09:00 and then with the Text Watcher I add a space and a dash and the user enters the closing time 19:00, so that at the end the time is 09:00 - 19:00.
For example, if the store is closed on Saturday, the user must click on the checkbox and when the checkbox is clicked, the editText has the text set and it says "Closed".
The problem is that when I use the Text Watcher, if the length is equal to 6, it adds the hyphen and then when the user clicks on the checkbox instead of writing "Closed" it says "Closed-".
How can I remove that dash?
There is a condition where the Text Watcher turns off when there are only letters in the editText.
Does anyone have a solution to my problem, do you have any advice for me?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert__orari);
lunedi_inizio_uno = (EditText)findViewById(R.id.editText_uno_lunedi_inizio);
lunediUno = (CheckBox)findViewById(R.id.checkBox_uno_lunedi);
lunedi_inizio_uno.addTextChangedListener(new TextWatcher() {
int keyDell;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lunedi_inizio_uno.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDell = 1;
int prevL = 0;
return false;
}
});
if (keyDell == 0) {
int len = lunedi_inizio_uno.getText().length();
if(len == 5) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 6) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + "-");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 7) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}
} else {
keyDell = 0;
}
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
lunediUno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
lunedi_inizio_uno.setText("Chiuso");
}
}
});
}
I solved the problem, it was enough to add "-" in the length equal to 5 and eliminate the conditions of length equal to 6 and 7.

Prevent onChangeListener from firing when form is prepouplated

I have a text field called VIN Number that when the user types in, if they reach 17 characters, I want to fire off a LookUpVin() method.
The problem is, when the user navigates to the screen, if the VIN is pre-poplated the it calls LookUpVin(), erasing anything the user may have typed in other fields (Make, Model, Year, etc..).
How do I only fire the change listener when the user is typing, not if the form is being populated via a data source?
editVIN = (EditText) view.findViewById(R.id.editVIN);
editVIN.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS);
editVIN.setImeOptions(EditorInfo.IME_ACTION_DONE);
editVIN.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Utilities.hideSoftKeyboard(getActivity());
if (editVIN.getText().length() == 17) {
doVINLookup();
}
return true;
}
return false;
}
});
editVIN.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
editVIN.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 (editVIN.getText().length() == 17) {
doVINLookup();
}
}
});

Android app reading in multiple variables from one String input

I am trying to read in multiple variables (int, double, String) from one input. However, the input has to be a specific way, i.e. the user should enter either distance in miles by entering “X miles” (either in decimals or as an integer) or time by entering “Y mins” (only as an integer). Here is an example of what I coded. However, enter a double does not work. I also need to use these values in other methods.
public boolean hintWalkTracker() {
// tracks whether the input for walking/running activity is correct
String text = String.valueOf(hintEditText.getText());
String s = text.replaceAll("\\d","");
int i = Integer.parseInt(text.replaceAll("[\\D]", ""));
double d = Double.parseDouble(text.replaceAll("[\\D]", ""));
if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && s.equals(" miles")) {
d = d * 88.9;
return true;
} else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && s.equals(" mins")) {
d = i * 4.78;
return true;
} else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && ((!s.equals(" mins")) || !s.equals(" miles"))) {
// create a new AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// set dialog's message to display
builder.setMessage(R.string.walk_missing_message);
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.OK, null);
// create AlertDialog from the AlertDialog.Builder
AlertDialog errorDialog = builder.create();
errorDialog.show(); // display the modal dialog
return false;
}
return false;
}
You just need to use a TextWatcher on your EditText in order to check what the user is writing and to react as you want when he write what you need.
Code sample :
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
hintWalkTracker();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

i want to validate multiple edittexts on click of device keypads done button

I have four edittext (atm pin kind of thing) and I want to validate all these four edittext on click of keypads done button(validations like - empty or wrong pin). so far, I could get the validations but it happens only if I click the done buttn twice. not able to figure out the issue, please help. I am a beginner. thanks!
Below is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pin_new);
setTextFocus(edtpin1, edtpin2);
setTextFocus(edtpin2, edtpin3);
setTextFocus(edtpin3, edtpin4);
setTextFocus(edtpin5, edtpin6);
setTextFocus(edtpin6, edtpin7);
setTextFocus(edtpin7, edtpin8);
edtpin1.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int arg1, KeyEvent arg2) {
switch (v.getId()) {
case R.id.btnBackPin:
Intent i = new Intent(getApplicationContext(),RegistrationProfileEmail.class);
startActivity(i);
break;
case R.id.btnCancelPin:
Intent in = new Intent(getApplicationContext(),Exit.class);
finish();
startActivity(in);
break;
case R.id.btnConfirmPin:
String s = new String();
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+edtpin3.getText().toString() + edtpin4.getText().toString();
if ((edtpin5.getText().toString().trim().isEmpty())
|| (edtpin1.getText().toString().trim().isEmpty())
|| (edtpin2.getText().toString().trim().isEmpty())
|| (edtpin4.getText().toString().trim().isEmpty())
|| (edtpin3.getText().toString().trim().isEmpty())
|| (edtpin6.getText().toString().trim().isEmpty())
|| (edtpin7.getText().toString().trim().isEmpty())
|| (edtpin8.getText().toString().trim().isEmpty())) {
Toast.makeText(getApplicationContext(), "Enter the Pin",
Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
else if (edtpin1.getText().toString().trim().equalsIgnoreCase(edtpin5.getText().toString().trim())&& edtpin2.getText().toString().trim().equalsIgnoreCase(
edtpin6.getText().toString().trim()) && edtpin3.getText().toString().trim().equalsIgnoreCase( edtpin7.getText().toString().trim()) && edtpin4.getText().toString().trim().equalsIgnoreCase( edtpin8.getText().toString().trim()))
{
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+ edtpin3.getText().toString()
+ edtpin4.getText().toString();
RegistrationPin.this.setPin(s);
}
// TODO move to payment option
else {
Toast.makeText(getApplicationContext(), "Pin is not matching",Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
break;
default:
break;
}
return false;
}
});
}
Create a class and implement the textwatcher like this:
private class CheckTextifEmpty implements TextWatcher {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (isEmptyCharacters(serverNumber)
|| isEmptyCharacters(textview1)
|| isEmptyCharacters(textview2)
|| isEmptyCharacters(textview3)) {
//do something here
} else {
//do something here
}
}
}
and add this method here:
private boolean isEmptyCharacters(TextView v) {
return v.getText().toString().trim().isEmpty();
}
and use it in your textview like this:
textview1.addTextChangedListener(new CheckTextIfEmpty());
You can use your validation like this without using a button
Refactor your code like this:
private boolean checkifValidate(TextView view1,View TextView2){
return view1.getText().toString().trim().contentEquals(view2.getText().toString());
}
Hope it helps,
edtpin1.setOnKeyListener(onSoftKeyboardDonePress);
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edtpin1.getText().toString();
edtpin2.getText().toString();
edtpin3.getText().toString();
edtpin4.getText().toString();
}
return false;
}
};

Live Search Mechanism in Edittext in Android?

I have e project where I have a Edittext from where i have to get input on every keypress or something like every character input.And Have to perform a livesearch. What kind of Listener i have to use to get input for every character input .
Do this way
editSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable edit) {
if (edit.length() != 0) {
// Business logic for search here
}
}
});
An editable text view that shows completion suggestions automatically
while the user is typing. The list of suggestions is displayed in a
drop down menu from which the user can choose an item to replace the
content of the edit box with.
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Here is a good tutorial:
https://developers.google.com/places/training/autocomplete-android
http://android-er.blogspot.com.au/2010/07/example-of-autocompletetextview.html
You need to use 'TextWatcher' as I have already answered here
It will give you result as shown in below images:
Try this..
edittext.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String textl = edittext.getText().toString().trim();
}
});

Categories