Fetch data from EditText and display it in a TextView |Android Program|


Activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
EditText
       
android:id="@+id/editText1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:ems="10"
       
android:inputType="textPersonName"
       
android:hint="enter college name" />

    <
EditText
       
android:id="@+id/editText2"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:ems="10"
       
android:inputType="textPersonName"
       
android:hint="enter place"/>

    <
Button
       
android:id="@+id/button"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="Fetch" />

    <
TextView
       
android:id="@+id/textView"
       
android:textSize="24dp"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="" />
</
LinearLayout>


MainActivity.java


package com.example.fetchdata;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText
e1,e2;
    Button
button;
    TextView
textView;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
e1=findViewById(R.id.editText1);
       
e2=findViewById(R.id.editText2);
       
button=findViewById(R.id.button);
       
textView=findViewById(R.id.textView);

       
button.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String cname=
e1.getText().toString();
                String place=
e2.getText().toString();

                
textView.setText(cname+"\n"+place);

            }
        });
    }
}

Post a Comment

0 Comments