Largest Of Three Numbers

file -> new project

App name

res -> values -> string.xml => App name: Largest of Three Numbers

Color:

res -> values -> color.xml

<color name="white">#DC0A51</color>

Drawable:

to create a shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000000" />

    <stroke android:width="10dp" android:color="#00FF00"/>

    <corners android:radius="5dp" />
</shape>

Design

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">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textview1"
            android:text="@string/app_name"
            android:layout_margin="10dp"
            android:textSize="38dp"
            android:textAlignment="center"
            android:textColor="@color/pink"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="15dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/textview2"
            android:text="Enter First Number: "
            android:textSize="20dp"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/num1"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:textColor="@color/pink"
            android:textSize="30dp"
            android:background="@drawable/shape" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="15dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/textview3"
            android:text="Enter Second Number: "
            android:textSize="20dp"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/num2"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:textColor="@color/pink"
            android:textSize="30dp"
            android:background="@drawable/shape" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="15dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/textview4"
            android:text="Enter third Number: "
            android:textSize="20dp"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/num3"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:textColor="@color/pink"
            android:textSize="30dp"
            android:background="@drawable/shape" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="0dp">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/large"
            android:textColor="@color/white"
            android:textSize="20dp"
            android:text="LARGEST"
            android:textAlignment="center"
            android:layout_marginLeft="70dp"
            android:layout_marginRight="70dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/result"
            android:textColor="@color/purple_500"
            android:textSize="20dp"
            android:textAlignment="center"/>

    </LinearLayout>

</LinearLayout>

Logic

MainActivity.java

package com.example.largestnumber;

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 EditText a;
    private EditText b;
    private EditText c;
    private TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a = (EditText) findViewById(R.id.num1);
        b = (EditText) findViewById(R.id.num2);
        c = (EditText) findViewById(R.id.num3);
        Button large = (Button) findViewById(R.id.large);
        result = (TextView) findViewById(R.id.result);

        large.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num1 = Integer.parseInt(a.getText().toString());
                int num2 = Integer.parseInt(b.getText().toString());
                int num3 = Integer.parseInt(c.getText().toString());

                if((num1 > num2) && (num1 > num3))
                    result.setText(num1+ " Number 1 is greater.");
                else if ((num2 > num1) && (num2 > num3))
                    result.setText(num2+" Number 2 is greater.");
                else
                    result.setText(num3+" Number 3 is greater.");

            }
        });

    }
}

Last updated