Android Studio : Simple input textbox and intercept key example
A GUI(Graphical User Interface) such as Android OS has input text box to allow users to enter text and able to capture the input for further processing. For this tutorial, we will learn how to create an editable text box widget in Android Studio and create a listener to process the input text string. We will also learn how to add a function to intercept certain key stroke entered by the user. For this example, we will intercept the space
key.
Below is an image of the finished application:
First, fill up the layout designer in Android Studio with the following code and save in the activity_main.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sweetlogic.inputtextbox.MainActivity">
<EditText
android:id="@+id/editTextInput"
android:layout_width="368dp"
android:layout_height="48dp"
android:text="Color"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.107" />
<Button
android:id="@+id/buttonGetEditTextInput"
android:layout_width="368dp"
android:layout_height="48dp"
android:text="Process input"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="231dp" />
<TextView
android:id="@+id/textView"
android:layout_width="368dp"
android:layout_height="48dp"
android:text="What is your favorite color?"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.004" />
</android.support.constraint.ConstraintLayout>
and fill in the Java codes at MainActivity.java
with:
package com.example.sweetlogic.inputtextbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.KeyEvent;
public class MainActivity extends AppCompatActivity {
private EditText editTextInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonListener();
}
// intercept user input if space button is pressed
public void interceptSpaceKey() {
editTextInput = (EditText) findViewById(R.id.editTextInput);
editTextInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if the user press space button, display a toast message
if ((keyCode == KeyEvent.KEYCODE_SPACE)) {
// display a floating message
Toast.makeText(MainActivity.this, "Cannot have space in input text!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
public void addButtonListener() {
Button button = (Button) findViewById(R.id.buttonGetEditTextInput);
editTextInput = (EditText) findViewById(R.id.editTextInput);
// check for space key
interceptSpaceKey();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, editTextInput.getText(), Toast.LENGTH_LONG).show();
}
});
}
}
Save all the files and hit the Run
button and you should be able to see the application running on your selected Android Virtual Device.
Here is an image of the application processing user input:
and intercept the space
key:
Hope you find this tutorial useful for learning Android Studio and happy coding!
See also : Android Studio : Checkbox for user to select options example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+18.9k Mac OSX : Homebrew and Golang
+16.2k Golang : Delete files by extension
+13.3k Generate salted password with OpenSSL example
+6.6k Android Studio : Hello World example
+21.3k SSL : How to check if current certificate is sha1 or sha2
+23.7k Golang : Upload to S3 with official aws-sdk-go package
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+47.2k Golang : Convert int to byte array([]byte)
+5.9k Golang : Extract XML attribute data with attr field tag example
+6.5k Golang : Find the longest line of text example
+8.3k Golang : Another camera capture GUI application with GTK and OpenCV