Android : How to Read and Display User Input.


I have been doing Android programming for about a month now and I thought it would be cool to share this experience along with you. So, when I started Android Programming this is the first app example I started with. In here we simply ask the user to enter some data (Name, Email and Password). Then we display that data in a TextView when the user clicks the button. Now if you are totally new to Android and don’t know a thing about Android Programming then you should really check this post out Android Programming Basics .

Let’s get on with the XML (Android UI). For this app, we use the LinearLayout (vertical). Then we “Drag n Drop” three EditText for text, email and password input. The we insert Button below them. To display these content insert a TextView below them. In Android, it is very important how you place your UI elements with respect to other elements; their sizes and styles. Also, keep in mind to put a vertical scrollbar so that your UI elements don’t fall out of the view. This can be done by using a ScrollView or ScrollBars. Also, keep the height and width of the text view to “warp_content” so that it adjusts according to the text.Android: How to Read and Display User Input

Now in our Android coding we declare our package com.shirish.hello .Then we import few necessary packages like app, os, view, widget. Now in our class, we declare a Button, three EditText and a TextView. Try hiding most of your code if possible (access specifiers). In our onCreate method, we will write most of our operational code. Its method called when the activity is starting. This is where most initialization goes; to inflate the activity’s UI, programmatically interacting with widgets in the UI, etc. Actually, this activity has a Bundle parameter.If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Otherwise, it is null.We are not gonna use it now to make things simpler.

package com.shirish.hello;

import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {
  private Button b;
  EditText etname,etemail,etpassword;
  TextView tv;

 protected void onCreate(Bundle savedInstanceState) { 
   tv.setMovementMethod(new ScrollingMovementMethod())
   b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      tv.setText("Your Input: \n"+etname.getText().toString()+"\n"+etemail.getText().toString()+"\n"+etpassword.getText().toString()+"\nEnd.");

We now set the content view for the activity from a layout resource and inflate it. Then we define our Button; find and assign its view. A click listener has to be set up on the button to listen to the user’s click activity and trigger the action if clicked. In the method onClick which is called when the view is clicked, we code the operations to be performed when the button is clicked. In this example, we have to read user’s inputs and display the data. (Define the TextViews and EditTexts). To read the data from the user we use getText() method which returns the text in those EditTexts. To display them we need to convert them to String. Using the setText() method we can display the text in the TextView.

Now, try your app on a device see if it works fine. If you notice carefully when you rotate your device your whole activity and view reloads and your data is lost. To avoid this onSaveInstanceState(Bundle) is used but we will see that later. Try it with different UI and modifications. Download the source code below:



Android Development Introduction


So, you have decided to develop an Android app. Here is how you would start this operation. This post is a kind of a linking one. I will not be describing things which are already described great on other websites; I’ll just link them here:

  1.  First of all you will need the Android Tools. Download Android Studio and the Android SDK. Download and Install Eclipse ADT plugin if you decide to develop in the Eclipse IDE. Here are the Download links: Android Studio, Android SDK, Android ADT. If you are looking how to install them follow these Android Developer links. Vogella link.Android SDK Manager _009
  2. Let’s get familiar with a few things. Create a new Android Project. To know all these folder’s function follow here. Android Projects.
  3. Now create at least one Android Virtual Device. It is where your android projects will run for testing purposes.
  4. Before you actually being with Android Programming I suggest you read this first. So, that you know how to develop icons and layout and their development guidelines: IconographyColors and Layouts.Selection_008
  5. Android Devices have a lot of variations in screen sizes. It is important to support most of them. Supporting Different Screen SIzes, Supporting Different Screens and Supporting Different Densities.
  6. Let us begin with our first Hello World Project in Android.

So, I have decided that I will start mini-Android series and we will try several Android examples.


  • If you are programming in Eclipse then enable the ‘LogCat’ feature to debug your app.Selection_011
  • Before Publishing your app check your Manifest file thoroughly.Selection_007
  • Android Support and help can be found on various Android Forums.