Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:21:49+00:00 2026-05-25T15:21:49+00:00

Would someone kindly suggest why the code in the OnKey() method is not executing.

  • 0

Would someone kindly suggest why the code in the OnKey() method is not executing. Please note that I am attempting to trigger this my hitting the “Search” button on the android key board.

Here is the main code:

package com.onkeyexample1;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;

public class OnKeyExample1Activity extends Activity {
EditText editText;
OnKeyListener onKeyListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText = (EditText) findViewById(R.id.editText1);
    editText.setOnKeyListener(onKeyListener);
    editText.addTextChangedListener(inputTextWatcher);

onKeyListener = new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
           System.out.println("Clicked");
        return true;
                 }
   };
}

I tried adding the block of code below as a suggested fix but it didn’t help:

    private TextWatcher inputTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) { }
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
            { }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
           // Log.d(TAG, s.charAt(count-1) + " character to send");;          
        }
    };
}

Here is my layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<EditText android:imeOptions="actionSearch" android:layout_width="fill_parent" 
   android:id="@+id/editText1" android:layout_height="wrap_content">
    <requestFocus></requestFocus>
</EditText>
</LinearLayout>

Any suggestions are greatly appreciated!

Here is the first implementation of Ted’s suggestion:

public class OnKeyExample1Activity extends Activity {
EditText editText;
OnKeyListener onKeyListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(onKeyListener = new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
           System.out.println("Clicked");
        return true;
                 }  
});
}
}

Here’s the fix to the double trigger problem mentioned in my comment:

editText.setOnKeyListener(onKeyListener = new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_DOWN) {  
             System.out.println("Argh");
             return false;
         }
        return true;
    } 
});
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:21:50+00:00Added an answer on May 25, 2026 at 3:21 pm

    You need to assign a value to onKeyListener before you call editText.setOnKeyListener(onKeyListener);. Just move the assignment up ahead of the method call and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
Would someone please review this code and tell me why the for loops are
Would someone please take a look at the following code fragments (all from one
Would someone please explain the difference? Are these the same product, and if not
Would someone kindly assist me with the following? I have two DataGridView objects that
A simple question, but could someone provide sample code as to how would someone
I'm quite confused about several books in .NET that I have read. Would someone
Would someone please be able to explain to me how to properly install JavaMail
I am new to Python and would like to know if someone would kindly
Why would someone prefer blocking writes over non-blocking writes? My understanding is that you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.