Android Simple EditText Example - ANDROID - Helper

Thursday, April 28, 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/AVvXsEiq_uE6x084mwLs7zlbar-xFzaDj44JqyD3w-xa_d8A5u1zmDzuYs-EnQSaAlXdFgXt0svH6LY0ZHyxtYOb9uprKNt4132h_jB1vskqT2ljOS9Z0aFzz3ygMpSzhu9iz-qKg3qXrNRFzqI/


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5HptQnbbr9aOqF9a1lzrqZdGdIBM4nHfIA9tQt4j0seqLmvx1hcDXuX0w7nUuz6EqObukkJ1xqJ62FS85qjaQgnQuQ_yGWDViVLih3071hJPzsdZJdF2NsXzR7d9p5UAfcRK_WopBIE/

11 comments:

  1. Hi I am realy new to Android. I am trying basic now and just copied you code and xml part and run it on the emlulator but I get the following error:

    "11-06 13:37:04.868: E/AndroidRuntime(769): Caused by: java.lang.NullPointerException
    11-06 13:42:23.849: E/AndroidRuntime(817): at my.test.Test.onCreate(Test.java:25)
    "


    the error points to the following line in the code:


    b.setOnClickListener(new OnClickListener()


    Below is the whole code:

    package my.test;


    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 Test 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();
    }
    });


    Hope someone can help me.

    kar_wod@wp.pl

    }
    }

    ReplyDelete
  2. Check your android manifest.xml file. The starting activity should be "Test". just check it out.

    ReplyDelete
  3. your code error line is :b.setOnClickListener(new OnClickListener()
    apart from that try this : b.setOnClickListener(new View.OnClickListener()

    ReplyDelete
  4. hi sivakrishna.

    b.setOnClickListener(new OnClickListener() will also work because i have imported only one class having the name "OnClickListener".

    import android.view.View.OnClickListener;

    But, You said is also right.

    ReplyDelete
  5. Hi i am new to Android. This example is very much useful for learners. Thank you.

    ReplyDelete
  6. how to get the edittext value in activity to another in android

    ReplyDelete
  7. Thanks, it is a very nice blog!

    ReplyDelete
  8. let me know if we press button without enter anything into edit box ....I think it will goes to force close.....can you tell me what I will do

    ReplyDelete
  9. Im a new child to this field..
    this example is really helpful to me, to understand how to get the text values from edit text and toast it ...
    Thanks a lot...

    ReplyDelete