Tuesday, 25 August 2015

EditText in Android

activity_main.xml
---------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:orientation="vertical"

    android:layout_height="match_parent"

    android:id="@+id/d"

    tools:context=".MainActivity">



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editText"

        android:hint="enter name" />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_marginTop="25dp"

        android:text="Show"

        android:id="@+id/button" />

</LinearLayout>
MainActivity.java
---------------------
package com.android.anil.edittext;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText e1;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText)findViewById(R.id.editText);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1=e1.getText().toString();
   Toast.makeText(getApplicationContext(),"Name is : "+s1,5000).show();
            }
        });

    }

}

Output is :
----------------
                     








No comments:

Post a Comment