onEditActionListener android defined - java

I have been getting into the android app world as of late. I understand that you want to place what variables you can into the onCreate method so that you can start using your variables right away. However, I am at a loss for a part of my code where I need to define onEditActionListener's to listen for user input but then also once enter is pressed get their text and send it to another activity. My research has shown I want to define these using the EditText and onEditActionListeners. however, I am at a loss as to how to go about this. Do I need to create a class that extends onEditActionListener?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horde_calculator);
EditText myText = (EditText) findViewById(R.id.numToSend);
OnEditorActionListener onEditorActionListener = new myClass();
myText.setOnEditorActionListener(onEditorActionListener);
}
that is a copy of my current code and I am unsure if this is the correct way to go about doing it. If I was to create a new onEditActionListener wouldn't that make my onCreate more complex than having a few lines of variables? I feel that there might be a more clear way of going about this. However, after looking through stack overflow I couldnt' seem to find something that made sense. I didn't want to just copy and paste code, but really understand how it all flows together.

Just another idea of how you can achieve this:
public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
editText = (EditText) findViewById(R.id.numToSend);
editText.setOnEditorActionListener(this);
}
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_DOWN) {
//both attempt are the same
Log.d("onEditorAction", view.getText().toString());
Log.d("onEditorAction", editText.getText().toString());
}
return true;
}
}

Use TextView.OnEditorActionListener :
TextView.OnEditorActionListener listener = new TextView.OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_DOWN) {
//call your new activity here
}
return true;
}
};
myText.setOnEditorActionListener(listener);

Related

Keyboard not showing up when EditText is clicked

I've created a method that is called when my EditText is clicked (or touched, to be specific), which simply takes the current time and displays it exactly as it has been passed by System.currentTimeMillis() in a TextView below. The method is this one:
public void captureTime(View view) {
currentTime = System.currentTimeMillis();
String currentTimeStr = currentTime+ "";
TextView textView = findViewById(R.id.textView);
textView.setText(currentTimeStr);
}
To do that, I've had to add the code below in the onCreate method. It uses setOnTouchListener instead of setOnClickListener since otherwise the captureTime method wasn't always called. Now, this method does what I wanted, but now when I click or touch the EditText, although it does this as required, the keyboard doesn't show up anymore. I have looked into other questions in which the keyboard doesn't show up either but they're not related to my case, although I have tried them. So, what is that is making the keyboard not show up and how can I fix this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_time = (EditText)findViewById(R.id.editText);
add_time.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
captureTime(add_time);
return true;
}
return false;
}
});
}
Try removing return true (in the if block). In this case, the "captureTime" method will work and the keyboard will open.

Android onBackPressed() is not being called?

in my MainActivity, which extends from AppCompatActivity, I want to override the onBackPressed method like so:
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
but onBackPressed does not get called. How ever if I do not override onBackPressed, the application closes, when I press the backbutton and if I do override it it doesn't.
The rest of my activity looks like this:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Drawer drawer;
private FloatingActionButton fab_test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab_test = (FloatingActionButton) findViewById(R.id.fab_test);
fab_test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"FAB Test pressed",Toast.LENGTH_SHORT).show();
}
});
buildDrawer();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer,page).commit();
}
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
EDIT: I'm talking about the hardware-backbutton(not the actionbar one)
This question is already answered, but I feel to clear something here in this topic. Most comments and answeres point out to use super.onBackPressed() and that this is the cause of the not working method onBackPressed(). But that is not correct and important to let other beginners know. The method onBackPressed() does not need to use super.onBackPressed() . onBackPressed()also works if somebody, for example, comment super.onBackPressed() out.
As the questionier has written, he won´t use super.onBackPressed() because it will close the activity. So, the cause of this why it isn´t working, could be seperated into three possible causes:
The Log doesn´t work because of a wrong filter in the logcat console
The Toast dosn´t work because of the wrong passed context
The OS is implemented wrong by the supplier.
Usually, the toast works by passing the correct context. In the case of questioner, simply passing this .
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(this,"onBackPressed",Toast.LENGTH_SHORT).show();
}
For the Log, simply set the correct filter on logcat.
I don´t care if somebody give downvotes now, but it must be clear for other beginners, that super.onBackPressed() must not be used.
Anyway, the use of onKeyDown() also is a solution.
The onBackPressed() is a default action called from onKeyDown() in API < 5 and a default action called from onKeyUp() from API level 5 and up. If onKeyUp() does not call super.onKeyUp(), onBackPressed() will not be called.
Documentation onKeyDown()
Documentation onKeyUp().
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
/*
* without call to super onBackPress() will not be called when
* keyCode == KeyEvent.KEYCODE_BACK
*/
return super.onKeyUp(keyCode, event);
}
Also another reason that onBackPressed() may not be called is because you are using the soft back button on the actionbar, it that case the following is needed:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You are missing, super.onBackPressed();
#Override
public void onBackPressed() {
super.onBackPressed();
}
or you can use
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK) {
// something here
finish();
}
return true;
}
thanks
make sure you are not calling onkeydown in your super view as it handles the back button clicking first.
working fine onKeyDown function return type false;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
For whoever is wondering, as most functionality is deprected API 30>, the following will surely help you a lot.
public class MainActivity extends AppCompatActivity {
private OnBackPressedCallback onBackPressedCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onBackPressedCallback = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
// Your business logic to handle the back pressed event
Log.d(TAG, "onBackPressedCallback: handleOnBackPressed");
}
};
getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
}
}
Just Remove super.onBackPressed() it will work

How to handle android keyboard actions

I am new to Android. I am trying to make a text box and on pressing done key, it should take the value to the java code. For this I am using setOnEditorActionListener.. I searched on how to do this and got many answers on how to implement it. Example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
I need to ask where should I write this thing? In which method? I tried doing it in onCreate but it threw some error. I somehow made it work using this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock);
Log.i(TAG, "onCreate");
editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(this);
}
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed");
Toast.makeText(this, "Hey you just clicked the DONE button", Toast.LENGTH_SHORT).show();
handled = true;
}
return handled;
}
Here I used this keyword, and I don't understand why have I used it.
Question 1. Please help me understand, why have we used this keyword..
Question 2. Why wasn't it working in the below code?
public void checkInput() {
Log.i(TAG, "Enter checkInput method");
final EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(TAG, "Enter onEditorAction");
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed")
handled = true;
}
return handled;
}
});
}
I called this checkInput method from onCreate.
To answer Question 1:
Here I used this keyword, and I don't understand why have I used it. Question 1. Please help me understand, why have we used this keyword..
You're telling Java to look into the Activity class for implementations of methods required by the TextView.OnEditorActionListener interface. So for all interactions with your soft keyboard, Java would look into your class for the method: onEditorAction
In order for the above to work, your activity needs to defined like:
public class MyActivity implements TextView.OnEditorActionListener {}
For question 2:
Question 2. Why wasn't it working in the below code?
To check for the "Done" action, your if statement should be:
if (actionId == EditorInfo.IME_ACTION_DONE) { ... }
Hope that helps.

Change Action of 'Done' Button on Virtual Keyboard Android

All- I looked at other questions relating to this topic and found out that according to the android development website: "the action key performs a "done" operation, typically meaning the IME will be closed." My question is how do I edit the action of the done button to make it so it calls one of my methods instead of closing the IME?
Thanks for your time.
Best possible way
Example for handling Enter key event
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (EditText)findViewById(R.id.txt);
txt.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)
{
Log.d(TAG, "enter_key_called");
}
return false;
}
});
}

Trouble triggering onKey event with Android

I'm having difficulty getting my main View's onKey event to trigger. I'm not sure what I'm doing wrong, I have correctly implemented the onClick event but cannot seem to figure out the onKey event.
Here's the relevant code:
public class MyActivity extends Activity {
private RelativeLayout main;
private ApplicationToolbar toolbar;
public void onCreate(Bundle savedInstanceState) {
...
this.main = (RelativeLayout) this.findViewById(R.id.main);
this.toolbar = new ApplicationToolbar(this);
// toolbar is added to main later on in the code...
this.main.setOnClickListener(mClickListener);
this.main.setOnKeyListener(mKeyListener);
}
private OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
toolbar.setVisibility(View.VISIBLE); // Works correctly.
}
};
private OnKeyListener mKeyListener = new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
toolbar.setBackgroundColor(0xFF0000FF); // Does not work.
return true;
}
};
}
In fact, no matter what code I put within mKeyListener it does not execute, which leads me to believe that the event itself is never being triggered, even when I pressed a bunch of keys on my physical keyboard (Motorola Droid, Android 2.1).
You can try overriding onKeyDown(int keyCode, KeyEvent event)
Check:
Back and other hard keys: three stories
is there a default back key(on device) listener in android?

Categories

Resources