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>

 



Android program to perform four basic Arithmetic Operations(addition, subtraction, multiplication and division)

 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"
       
android:orientation="vertical"
       
tools:context=".MainActivity">

        <
TextView
           
android:layout_width="fill_parent"
           
android:layout_height="100dp"
           
android:layout_marginTop="30dp"
           
android:gravity="center_horizontal"
            
android:text="ARITHMETIC OPERATIONS"
           
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/tableRow1"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">

                <
TextView
                   
android:id="@+id/fno_tv"
                   
android:layout_width="match_parent"
                   
android:layout_height="40dp"
                   
android:text="ENTER FIRST NUMBER   "
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
android:textColor="@color/color2" />

                <
EditText
                   
android:id="@+id/fno_et"
                   
android:layout_width="match_parent"
                   
android:layout_height="45dp"
                   
android:ems="10"
                   
android:inputType="number"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
android:textColor="@color/color3" />
            </
TableRow>
            <
TableRow
               
android:id="@+id/tableRow2"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">

                <
TextView
                   
android:id="@+id/sno_tv"
                   
android:layout_width="match_parent"
                   
android:layout_height="75dp"
                   
android:text="ENTER SECOND NUMBER   "
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
android:textColor="@color/color2" />

                <
EditText
                   
android:id="@+id/sno_et"
                   
android:layout_width="match_parent"
                   
android:layout_height="45dp"
                   
android:ems="10"
                   
android:inputType="number"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    
android:textColor="@color/color3" />
            </
TableRow>

            <
Button
               
android:id="@+id/buttonsum"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:background="@color/colorAccent"
               
android:gravity="center"
               
android:text="ADD"
               
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
android:layout_width="match_parent"
                   
android:layout_height="10dp" />
            </
TableRow>

            <
Button
                
android:id="@+id/buttonsub"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:background="@color/colorAccent"
               
android:gravity="center"
               
android:text="SUBTRACT"
               
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <
TableRow

               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView

                   
android:layout_width="match_parent"
                   
android:layout_height="10dp"
                   
/>
            </
TableRow>

            <
Button
               
android:id="@+id/buttonmul"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:background="@color/colorAccent"
               
android:gravity="center"
               
android:text="MULTIPLY"
               
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
android:layout_width="match_parent"
                    
android:layout_height="10dp" />
            </
TableRow>

            <
Button
               
android:id="@+id/buttondiv"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:background="@color/colorAccent"
               
android:gravity="center"
               
android:text="DIVIDE"
               
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
android:layout_width="match_parent"
                   
android:layout_height="75dp" />
            </
TableRow>

            <
TableRow
               
android:id="@+id/tableRow3"
               
android:layout_width="match_parent"
               
android:layout_height="40dp">

                <
TextView
                   
android:id="@+id/result_tv"
                   
android:layout_width="wrap_content"
                   
android:layout_height="50dp"
                   
android:text="RESULT"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                   
android:textColor="@color/color2" />

                <
TextView
                   
android:id="@+id/result_value"
                   
android:layout_width="match_parent"
                   
android:layout_height="50dp"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                   
android:textColor="@color/color3" />
            </
TableRow>
        </
TableLayout>
    </
LinearLayout>

MainAcitivity.java

package com.example.arithmeticoperations;

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

public class MainActivity extends Activity {

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

        final Button btnsum = (Button) findViewById(R.id.buttonsum);
        final Button btnsub = (Button) findViewById(R.id.buttonsub);
        final Button btndiv = (Button) findViewById(R.id.buttondiv);
        final Button btnmul = (Button) findViewById(R.id.buttonmul);
        final EditText etv = (EditText) findViewById(R.id.fno_et);
        final EditText etv2 = (EditText) findViewById(R.id.sno_et);
        final TextView result = (TextView) findViewById(R.id.result_value);

        btnsum.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnsum.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double sum = x + y;
                result.setText(Double.toString(sum));
            }
        });

        btnsub.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnsub.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double sub = x - y;
                result.setText(Double.toString(sub));
            }
        });

        btndiv.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btndiv.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double div = x / y;
                result.setText(Double.toString(div));
            }
        });

        btnmul.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnmul.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double mul = x * y;
                result.setText(Double.toString(mul));
            }
        });
    }

}

 

 

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">#FFE4E1</color>
    <color name="color2">#191970</color>
    <color name="color3">#FF0000</color>
</resources>

 

String.xml

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