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
+8.4k Golang : Implementing class(object-oriented programming style)
+9.7k Golang : How to extract video or image files from html source code
+9.5k Golang : Create unique title slugs example
+22.7k Generate checksum for a file in Go
+11.7k Golang : Display a text file line by line with line number example
+6.1k Golang : Measure execution time for a function
+4.7k JavaScript : Rounding number to decimal formats to display currency
+27.5k Golang : Convert CSV data to JSON format and save to file
+13.7k Android Studio : Password input and reveal password example
+5k JQuery : Calling a function inside Jquery(document) block
+13.9k Golang : Gin framework accept query string by post request example
+5k Python : Find out the variable type and determine the type with simple test