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.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

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

 @Override
 protected void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.activity_main);
   b=(Button)findViewById(R.id.click);
   tv=(TextView)findViewById(R.id.display);
   tv.setMovementMethod(new ScrollingMovementMethod())
 
   b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      etname=(EditText)findViewById(R.id.name);
      etemail=(EditText)findViewById(R.id.email);
      etpassword=(EditText)findViewById(R.id.password);
      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:

image4233


Posted

in

,

by

Comments

5 responses to “Android : How to Read and Display User Input.”

  1. Gangadhar S Avatar
    Gangadhar S

    Sir what is the complete android source code to “read a username with submit button and display the username in the next page”?

    Like

  2. tdazzly27 Avatar

    This a great tutorial thank sir 🙂

    Like

  3. Sanju Avatar
    Sanju

    How to send user input as notification as well as Save it in databases

    Can you please tell a how to do it Sir

    Like

  4. Gary Avatar

    I am building app to send SMS to user on payment intent. Been looking all over for this code. Seems the easiest way is yours. Will put into my code and let you know.

    Like

  5. luthfishatara Avatar

    I was helped by this code, Thank you so much

    Like

Leave a Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

%d bloggers like this: