Android program to display the entered value as toast

 

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"
   
android:background="@color/color1"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
TextView
       
android:layout_width="fill_parent"
       
android:layout_height="120dp"
       
android:layout_marginTop="40dp"
       
android:gravity="center_horizontal"
       
android:text="DISPLAYING NAME as TOAST"
       
android:textColor="@color/color2"
       
android:textAppearance="@style/TextAppearance.AppCompat.Display1"></TextView>

    <
TableLayout
       
android:id="@+id/tableLayout1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_margin="10dp"
       
android:stretchColumns="1">
        <
TableRow
           
android:id="@+id/tableRow0"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content">
        </
TableRow>
    <
TableRow
       
android:id="@+id/tableRow1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">

        <
TextView
           
android:id="@+id/textView"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:text="FIRST NAME    "
           
android:textColor="@color/color2"
           
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <
EditText

           
android:id="@+id/fname"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:ems="10"
           
android:inputType="textPersonName"
           
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
           
android:textColor="@color/color3" />
    </
TableRow>
    <
TableRow
       
android:id="@+id/tableRow2"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">

        <
TextView
           
android:id="@+id/textView2"
           
android:layout_width="match_parent"
           
android:layout_height="75dp"
           
android:text="LAST NAME    "
           
android:textColor="@color/color2"
           
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <
EditText
           
android:id="@+id/lname"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:ems="10"
           
android:inputType="textPersonName"
           
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
           
android:textColor="@color/color3" />
    </
TableRow>
</
TableLayout>

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="SUBMIT"
       
android:textColor="@color/color2"
       
android:background="@color/colorAccent"
       
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

</
LinearLayout>

 

MainActivity.java

package com.example.assignment3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText
name1, name2;
    Button
b1;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
name1 = (EditText) findViewById(R.id.fname);
       
name2 = (EditText) findViewById(R.id.lname);
       
b1=(Button) findViewById(R.id.button1);
       
b1.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {

                Toast toast=Toast.makeText(getApplicationContext(),
"WELCOME "+ name1.getText().toString()+" "+name2.getText().toString(),Toast.LENGTH_LONG);
                toast.setGravity(Gravity.
CENTER |Gravity.CENTER_HORIZONTAL ,5, 5);
                toast.show();
            }
        });
    }
}

 

strings.xml

<resources>
    <
string name="app_name">Toast</string>
</
resources>

 

 

Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <
color name="colorPrimary">#6200EE</color>
    <
color name="colorPrimaryDark">#3700B3</color>
    <
color name="colorAccent">#03DAC5</color>
    <
color name="color1">#DA70D6</color>
    <
color name="color2">#191970</color>
    <
color name="color3">#FF0000</color>
</
resources>

 



1 comment: