Send message to another window upon Enter key press - java

I want my test data goes to another window after Enter key press, like a click on the send button.
I'm using this code:
text1 = ((TextView) findViewById(R.id.textMsg));
text1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event){
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(IMSendData.this, text1.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Using this code after I press the Enter key, its goes on send button, and after second Enter key press the test data goes to the window.
I want to press Enter button only once and then the text go to the window.

you go through this..
sendData = (EditText) findViewById(R.id.message);
sendData.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode ==
KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
adapter.add(new OneComment(false,
sendData.getText().toString()));
sendData.setText("");
return true;
}
return false;
}
});

Related

When i use type keyguard dialog then my screen stops working

I am developing a Lock Screen App that will lock the screen as well as apps that the user want to.
Lock can be pin code or pattern lock and so far now when I activate a pin code or pattern lock then a lock screen appears on the screen when user Reboot or off the screen, the problem I am facing right now is that, I can't disable my Home button and Task manager button.
What I have tried soo far is
I have added the
getWindow().addFlags(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
after adding this, Home button and Task Manager button is still enabled and my screen freezes, if I touch something on the screen its not performing anything and when I press Home button the lock screen disappear.
I also have added this too
#Override
public void onBackPressed() {
return;
}
// Handle button clicks
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_POWER)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|| (keyCode == KeyEvent.KEYCODE_CAMERA)) {
return true;
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
// handle the key press events here itself
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
|| (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
return false;
}
if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
after adding this, back button is disabled successfully working but Home button is still enabled and funtioning.

Custom hardware button events/intents

I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}

KEYCODE_BACK doesn't work when pressed for the very first time

I'm having trouble overriding the Back Button press on Android.
The thing is, everything works perfectly except for the very first time.
When I load the app and press the back button for the first time, it pauses the app, which is NOT what I want. Other than that, it works as expected.
My code:
private void setupDeviceButtons(){ // this is ran at the very beginning (onViewCreated())
// setting up a listener to close the menus when the back button is pressed
View view = getView();
Log.e(TAG, "This happens when I load the app" );
if (view != null) {
Log.e(TAG, "This also happens when I load the app");
view.setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e(TAG, "But this doesn't happen when I press the back button for the first time.");
// we filter all actions that are not key down
if (event.getAction() != KeyEvent.ACTION_DOWN)
return true;
...
}
return false;
});
} else {
Log.e(TAG, "ERROR on setupDeviceButtons(): Unable to set back button behaviour. View is null.");
}
}
Any thoughts?
Thank you in advance
Use the below code working perfectly in fragments.
//on fragment back pressed
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()== KeyEvent.ACTION_DOWN) {
// do your code on back pressed
return true;
}
return false;
}
});

Why KeyListener.onKey triggers twice?

I want to handle the click on the "ok" key of the onscreen keyboard. For that purpose I added a KeyListener to the text field:
textField = (EditText) view.findViewById(R.id.text_field);
textField.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
boolean handled = false;
if (keyCode == KeyEvent.KEYCODE_ENTER) {
okPressed(view);
handled = true;
}
return handled;
}
});
And in the okPressed method I'm checking the content:
private void okPressed(View view) {
String value = textField.getText().toString().trim();
if (value.equals("")) {
Toast.makeText(view.getContext(), "Error", Toast.LENGTH_SHORT).show();
return;
}
}
And now for the case that my text field is not empty, everything works fine. But in the case the field contains no text, my okPressed method is executed twice. But why?
Per KeyEvent's documentation:
Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN.
The last key event is a ACTION_UP for the key up.
You should check the result of getAction() to filter for only the key action you want (i.e., ACTION_UP if you only want to trigger when the user releases or ACTION_DOWN if you want to trigger as soon as they touch the button).

Button return = action button home

I'm doing an Android app and I would know if this was possible:
When the user press the return button, it does the same action as the home button.
Following the question I linked: Override back button to act like home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Should be the solution.
This overrides the backbutton, and puts the task to the back. Which emulates hiding like the home button. Read the other question for more details, and solutions.

Categories