Thursday, 6 October 2016

EditText

Activity main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin" 
   android:paddingRight="@dimen/activity_horizontal_margin" 
   android:paddingTop="@dimen/activity_vertical_margin" 
   android:paddingBottom="@dimen/activity_vertical_margin"     
   tools:context="com.kiranapp.MainActivity">


    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Name" 
     android:ems="10" 
     android:id="@+id/editText" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" />

    <Button         
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show" 
     android:id="@+id/button" 
     android:layout_marginTop="56dp" 
     android:layout_below="@+id/editText"         
     android:layout_centerHorizontal="true" />
</RelativeLayout>


MainActivity.java :-

package com.kiranapp;

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

public class MainActivity extends AppCompatActivity {

  EditText editText;
  Button b1;

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

 }
}


Output :-

EditText
EditText figure

No comments:

Post a Comment