Tuesday, February 15, 2011

Android Simple EditText Example


SIMPLE EDITTEXT

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<EditText android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

    <Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show the Text" />

</LinearLayout>

SOURCE CODE [EditTextExample.java] is

package com.EditTextExample;

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

public class EditTextExample extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
                               
final EditText et;
final Button b;
       
et = (EditText) findViewById(R.id.edittext);
b = (Button) findViewById(R.id.button);
       
                                b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
                                                                String text=et.getText().toString();
                                                               
                                                Toast msg = Toast.makeText(getBaseContext(),
                                                                                text, Toast.LENGTH_LONG);
msg.show();
                                                }
                                });
}
}

The OUTPUT will be


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJPqIXQ8HGPWjiSGd7a8hesov_68jEMv4Xaw6SYrYhyWnHuUhEoB0uXPv7-2QaUA30HgNwYsxDl_QtW3wUXXpjL8kvilWG6rFVbA5CLcp71QejAUcVFnqIv9qwci2D7c3X2AMRo94IF7k/


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizl7WUo2u-TzEciZfYHZZqJI-3fsJrgUb-1kX4NgXKwUMN6EAaHHjyzC7aNjsyj99j6OTWln1vsRjc8bP5G0Gw9a8t34z-UuipzDaazJ2EIc3lvThXieMlfwohvX4KdwmmdovBWkE5oSg/