Simple App To Get Text And Show

MainActivity.Java

package com.example.firstapp;

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 {

    private Button showButton;

    private TextView nameText;

    private EditText enterName;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        showButton = findViewById(R.id.button);

        nameText = findViewById(R.id.textView);

        enterName = findViewById(R.id.editTextName);


        showButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                String name = enterName.getText().toString();

                if (name.isEmpty()) {

                    nameText.setText("Hello Please Write Your Name");

                } else {

                    nameText.setText("Hello," + name);


                }

            }


        });

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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">


    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="SHOW NAME"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />


    <TextView

        android:id="@+id/textView"

        android:layout_width="229dp"

        android:layout_height="30dp"

        android:layout_marginTop="44dp"

        android:textSize="20sp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/button" />


    <EditText

        android:id="@+id/editTextName"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="44dp"

        android:ems="10"

        android:hint="Enter Your Name"

        android:inputType="textPersonName"

        android:minHeight="48dp"

        app:layout_constraintBottom_toTopOf="@+id/button"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.497"

        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Comments