Eric Bergman-Terrell's Blog

Android Programming Tip: How to React to Enter typed in an EditText Field
April 19, 2012

I am updating Vault 3 for Android to initiate a search when the user types Enter in the search text box. Here's how I did this:

  1. I created a Key Listener on the search EditText field.
  2. When Enter is typed, the perfomClick is called on the search button, but only if the search button is visible and enabled.

Here's the code:

searchText = (EditText) findViewById(R.id.SearchText);
		
...

// Initiate a search when user types ENTER.
searchText.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
	    	boolean result = false;
		    	
	// If the event is a key-down event on the "enter" button
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER && searchButton.isEnabled() && searchButton.getVisibility() == View.VISIBLE) {
        	searchButton.performClick();
        	result = true;
        }
		        
        return result;
    }
});

antique typewriter

Keywords: Android, EditText, View, performClick, setOnKeyListener, KeyEvent, getVisibility

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Node.js + Express: How to Block Requests by User-Agent HeadersJanuary 7, 2026
Vault 3 is Now Available for Windows on ARM Machines!December 13, 2025
Vault 3: How to Include Outline Text in Exported PhotosOctober 26, 2025
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023