LOGO1_03

EditText example in android

Today we are going to learn about EditText in android, now open eclipse and create an android project first then drag EditText as previous tutorial .

Source Code


 

java file:- EditTextActivity.java


 

package com.androidfromhome.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditTextActivity extends Activity implements OnClickListener {

private TextView tv_result;
private Button btn_click;
private EditText et_input;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);

//call function initalizedView()
initalizedView();
}

public void initalizedView(){
//all Object initialization here
tv_result=(TextView)findViewById(R.id.tv_result);
btn_click=(Button)findViewById(R.id.btn_click);
//register button to OnClickListener
btn_click.setOnClickListener(this);
et_input=(EditText)findViewById(R.id.et_input);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

switch (v.getId()) {
case R.id.btn_click:
String input_text=et_input.getText().toString();
//null checking other it may crash for NullPointerException exception
if (input_text!=null) {

if (!input_text.equalsIgnoreCase("")) {
//set text to the TextView
tv_result.setText("You Have Enter: "+input_text);
}else{
//set text to the TextView
tv_result.setText("No Value inserted");
}

}else{

//Give some message
}
break;

default:
break;
}

}
}


xml file code( activity_edit_text.xml)


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dfdfdf"
android:gravity="center"
android:orientation="vertical"
tools:context="com.androidfromhome.edittext.EditTextActivity" >

<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCEBF9"
android:ems="10"
android:hint="Type Some Text"
android:padding="10dp"
android:textColor="#000000" >

</EditText>

<Button
android:id="@+id/btn_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#019E5A"
android:padding="10dp"
android:text="Click Button"
android:textColor="#ffffff" />

<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000" />

</LinearLayout>


Download Source Code

Leave a Reply

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

Get Widget