Android App Development | Beginner Course | Lecture#16 | Hive Learners

avatar

๐“–๐“ป๐“ฎ๐“ฎ๐“ฝ๐“ฒ๐“ท๐“ฐ๐“ผ

Hello dear Hive Learners community members, I hope you all are well. Welcome to the 16th Lecture of Android App Development series. Today we will use the button to add two numbers. We are moving toward a simple calculator app. We will add a second screen to hold the arithmetic operations sign.

multi_purpose

GitHub Link

Use this GitHub project to clone into your directory. It will constantly get updated in the following lecture so that you will never miss the latest code. Happy Coding!.

What Should I Learn

  • Add a screen for arithmetic signs
  • Add two numbers using the button (=) on the app

Assignment

  • Add two numbers in your app without the help of a keyboard

Procedure

First, we need to add two signs plus sign and a minus sign. We will add more signs in the next lecture. I edit the design activity main_activity.xml file and set the IDs for the buttons.

image

Now we need to add the equal button, to sum up, the two numbers. A total of three new buttons were added. We also need to declare and initialize them in the MainActivity.java file.

image

Now we need to add a screen2. This screen is used to hold the arithmetic operation sign that we will use in a condition. I use the weight property for the screen. We will discuss the weight in our next lecture in detail,

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

        <TextView
            android:id="@+id/screen1_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/screen2_tv"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:textColor="@color/white"
            android:textSize="24sp" />

    </LinearLayout>

image

Now we need to declare and initialize all the new buttons and other fields. Also, set the on-click listeners for the buttons.

image

Now we write the logic for the plus and minus buttons. When a user clicks on this button it will copy the text on the screen2 and clear the screen.

image
I forget to add an int variable globally that will store the number before clearing the screen 1 on sign buttons click.

image

Now we need to add one more line in the sign buttons to store the text before clearing.

first_num = Integer.parseInt(screen1_tv.getText().toString());

image

On equal button click, we need to add logic to compare the signs with the text on the screen 2 and operate then.
We will use the if-else condition. I store the screen2 text in a variable string to use that variable easily rather than writing the getText() line many times we will use the variable.

 equal_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String stored_sign = screen2_tv.getText().toString();
                if (stored_sign.equals("+")) {
                    int result = first_num + Integer.parseInt(screen1_tv.getText().toString());
                    Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();

                }
                if (stored_sign.equals("-")) {
                    int result = first_num - Integer.parseInt(screen1_tv.getText().toString());
                    Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();

                }


            }
        });

image


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
5 comments
avatar

Almost a calculator! Good job!! Thanks for the teaching
!1UP

0
0
0.000
avatar
King-1UP-Cartel-250px.png

You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
@stem-curator
And they will bring !PIZZA ๐Ÿ•, !PGM ๐ŸŽฎ and !LOLZ ๐Ÿคฃ

Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.

0
0
0.000
avatar

Sent 0.1 PGM - 0.1 LVL- 1 STARBITS - 0.05 DEC - 15 SBT tokens to @curation-cartel, @faisalamin

remaining commands 4

BUY AND STAKE THE PGM TO SEND A LOT OF TOKENS!

The tokens that the command sends are: 0.1 PGM-0.1 LVL-2.5 BUDS-0.01 MOTA-0.05 DEC-15 SBT-1 STARBITS-[0.00000001 BTC (SWAP.BTC) only if you have 2500 PGM in stake or more ]

5000 PGM IN STAKE = 2x rewards!

image.png
Discord image.png

Support the curation account @ pgm-curator with a delegation 10 HP - 50 HP - 100 HP - 500 HP - 1000 HP

Get potential votes from @ pgm-curator by paying in PGM, here is a guide

I'm a bot, if you want a hand ask @ zottone444


0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.ย 
ย 

0
0
0.000