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
+5.6k Unix/Linux : How to test user agents blocked successfully ?
+13.3k Golang : Qt progress dialog example
+5.8k Fontello : How to load and use fonts?
+9.5k Golang : Populate slice with sequential integers example
+8.6k Yum Error: no such table: packages
+4.5k Chrome : How to block socketloop.com links in Google SERP?
+15.5k Golang : How to login and logout with JWT example
+5.8k Golang : Extract unicode string from another unicode string example
+9k Golang : Write multiple lines or divide string into multiple lines
+19.7k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+6.3k Golang : How to determine if request or crawl is from Google robots
+30.7k Golang : Calculate percentage change of two values