Android Tutorial – Using EditText, TextView and Button with Example





t2-1

EditText – This is use to take input from the user.

TextView – This is used to show the text.

Button – This is used to implement something or used to perform a particular action.

Excuse me, here are the best reads for you

Movie Hall For Couples : Ebony Lounge Review Movie Hall For Couples 5 Best Places To Visit With Girlfriend In Delhi Places To Visit With Gf Top Private, Secret Places For Couples In Delhi Secret Places | Couples Oysters Water Park Gurgaon Entry Ticket Price Oysters Water Park 5 Most Haunted Places In Delhi You Can Visit Most Haunted Places Best Nightout Places In Delhi/NCR Best Night Out Places Top 4 places To Hang out With Friends In Delhi Places To Hang Out

Example showing the use of EditText, TextView and Button

In this example we will create an application and it perform following tasks

  1. EditView take input from the user.
  2. TextView show the text enter by the user when user click on the button.
  3. Button is used to perform task of showing text in TextView.

Now create a new project named as ‘ShowText’, give the name ‘ShowActivity’ to your activity and give the name ‘activity_show’ to your layout.

Edit your activity_show.xml file

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.ShowActivity” >

 

<EditText

android:id=“@+id/editText1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“64dp”

android:ems=“10” />

 

<Button

android:id=“@+id/button1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/editText1”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“39dp”

android:text=“Button” />

 

<TextView

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/button1”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“52dp”

android:text=“Large Text”

android:textAppearance=“?android:attr/textAppearanceLarge” />

 

</RelativeLayout>

 

Edit your ShowActivity.java file

package com.example.showtext;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class ShowActivity extends Activity implements OnClickListener {

 

EditText e;

TextView t;

Button b;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_show);

e = (EditText)findViewById(R.id.editText1);

t = (TextView)findViewById(R.id.textView1);

b = (Button)findViewById(R.id.button1);

b.setOnClickListener(this);

 

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_show, menu);

return true;

}

 

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

String a=null;

a = e.getText().toString();

t.setText(a);

 

}

 

}

 

Now run your project on emulator.

t2-1

 

Hope you enjoyed this tutorial and gained information about Android Tutorial – Using EditText, TextView and Button with Example.  For more details stay connected with us. Also, you can put your queries in Comment Box, we will respond quickly. 

Suggestions BY Google





Be the first to comment

Leave a Reply

Your email address will not be published.


*