Android : How to Read and Display User Input.

Standard

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

Advertisements

2 thoughts on “Android : How to Read and Display User Input.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s