Tuesday, December 3, 2013

changing edittext text from button click listener: Android tutorial

This post will try to cover how to set the text to a edit text on button click, through android programming.

This application has only one activity with one button and one edittext. Edittext will be initially empty. Once user clicks on the button, then we will change the text of edittext to "button is clicked".

Here we will use setText() method of EditText class to change the text of the edittext.
To handle button clicks, we will use setOnClickListener() method of Button class.

 First Activity (Before clicking button)  First Activity (after clicking button)




First Activity
package com.techpalle.b15_first;

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.Toast;

public class MainActivity extends Activity {
 //step1 : create edit text variable
 private EditText et;
 private Button b;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //step2 : fetch edit text from xml file.
  et = (EditText) findViewById(R.id.editText1);
  b = (Button) findViewById(R.id.button1);
  
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
                                //step3 : set the text to edit text by using setText function
    et.setText("Button is clicked..");    
   }
  });
 }

}



xml layout file for First Activity 
File name: activity_main.xml
 <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"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView1"  
     android:layout_below="@+id/textView1"  
     android:text="Click Here" />  
   <EditText  
     android:id="@+id/editText1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@+id/button1"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="28dp"  
     android:ems="10">  
     <requestFocus />  
   </EditText>  
 </RelativeLayout>  


Download complete code : Click to download

Similar basic activity programs:
How to start an activity and pass data to second screen. 
Simple calculator program to add and subtract 2 numbers 
How to start an activity from main activity on clicking button, and pass data 
Starting second activity based on login and password details 
Getting results from child activity

Tags : edittext, settext, button click listener, button setonclicklistener, android tutorial

No comments:

Post a Comment