Saturday, 19 January 2013

Working with EditText

Demo:



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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Enter your Name"
        android:textColor="#ffffff"
        android:textSize="25dp" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#000000" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Show Entered Name"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textColor="#ffffff"
        android:textSize="25dp" />

</LinearLayout>


2.Activity:
 
package com.venkool.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 {
      /** Called when the activity is first created. */

      TextView textview;
      EditText edittext;
      Button button;

      String enteredName;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            edittext = (EditText) findViewById(R.id.edittext);

            // 2nd TextView Reference
            textview = (TextView) findViewById(R.id.textview);

            // set Listener for Button
            button = (Button) findViewById(R.id.button);

            button.setOnClickListener(new OnClickListener() {

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

                        // Getting entered data in EditText
                        enteredName = edittext.getText().toString();

                        // Set the EditText data to TextView
                        textview.setText(enteredName);
                  }
            });

      }
}


3)Download this Project Click Here

No comments:

Post a Comment