Working With EditText,Button,TextView in Android – UI Widgets

editextimage

Working with EditText ,TextView and Buttons is simple as simple in Java. How to get values from EditText and how to set OnClickListener to Our Button and set on TextView is simple.How? Lets code –

Download Code

1). First of all create new project and then open its activity_main.xml file and take two Edittext ,TextView with one Button .

2). Now I’am going to show you how to get values from EditText and Onclick of Button and will Toast those Strings and also set those values over TextView.

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" >
    
    <EditText android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:hint="enter name1"
        android:id="@+id/e1"/>
	<EditText android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:hint="enter name2"
        android:id="@+id/e2"
        android:layout_below="@id/e1"/>
	<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click"
    android:id="@+id/b1"
    android:layout_below="@id/e2"/>
	
	<TextView android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:id="@+id/t1"
	    android:text="Result"
	    android:textSize="30sp"
	    android:layout_below="@+id/b1"/>
</RelativeLayout>

Result –

edittext1

Now MainActivity.java file what we have to do is get the values from  EditText via e1.getText() method where e1 is our EditText etc. And set onClickListener on button b1.

MainActivity.java

package androidarena.androideditbutton;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

	
	Button b1;
	EditText e1,e2;
	String s1,s2;
	TextView t1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1= (Button)findViewById(R.id.b1);
		e1 = (EditText)findViewById(R.id.e1);
		e2 = (EditText)findViewById(R.id.e2);
		t1= (TextView)findViewById(R.id.t1);
		
		b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				s1=e1.getText().toString();
				s2 = e2.getText().toString();
				
				Toast.makeText(getApplicationContext(), s1+""+s2, 3000).show();
				t1.setText(s1+s2);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Output –

editext_androidarena

You can customize your java code according to your need like if you want to take this string values from one Activity to another Activity you can use Intent .

For more Queries and if you found any bug do Post here . Happy Coding nerds :p

 

Leave a Reply

Your email address will not be published. Required fields are marked *