Tuesday 4 March 2014

Use of Basic Controls of Android

This article will give an example of the three basic controls of Android (EditText , TextView and Button).
We will use EditText for taking inputs from user. We will use TextView for displaying result to user.And we will use Button to perform action by taking input from Edittext and display it to user in TextView. We will also use a Toast to give an alert message to user.

Below is our XML file for adding all the three controls


<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/editText_hint" >
</EditText>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="@string/button_txt_show" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

To listen the click we will implement click listener as:
               button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

}
});


To get the text from EditText below is the code snippet 
String userInput = editText.getText().toString().trim();

And to set the text on TextView below is the code snippet:
textView.setText("Hello!" );

To show Toast messages below is the code snippet. Toast are the short duration alert popups.
Toast.makeText(context,"hi", Toast.LENGTH_LONG).show();

The out put of the three basic controlls would be smiliar to below picture.

The Source Code For the above example is available at BasicControls.

  





No comments:

Post a Comment

About Me

INDORE, madhya pradesh, India
Hi, I am Sumit Patel From Indore Madhya Pradesh .Working as an Android Developer Since 2011 . This blog is just to share my experience, as Margaret Fuller say's “If you have knowledge, let others light their candles in it. ”