Add String Android

Add String  Android 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="First Name"
        android:inputType="text"
        android:id="@+id/text2"
        android:layout_below="@+id/text1"
        android:layout_marginTop="10dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Last Name"
        android:inputType="text"
        android:id="@+id/text3"
        android:layout_below="@+id/text2"
        android:layout_marginTop="10dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show"
    android:id="@+id/btn"
    android:layout_marginLeft="120dp"
    android:layout_below="@+id/text3"/>
</RelativeLayout>


Mainactivity.java

package com.example.addstring;

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 text2,text3;
TextView text1;
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1=(TextView) findViewById(R.id.text1);
        text2=(EditText) findViewById(R.id.text2);
        text3=(EditText) findViewById(R.id.text3);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String t1=text2.getText().toString();
                String t2=text3.getText().toString();
                text1.setText(t1+" "+t2);

            }
        });
    }

}

0 Comments