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

in Hive Learners • 9 months ago

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello, dear Hive Learner, I hope you all are well. Welcome to the 17th lecture on Android App development. I hope you are learning something new every day with our lectures. Today we will implement a new button in our simple calculator that will work like a backspace. So let's jump into it.

multi_purpose_1_

GitHub Link

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

What Should I Learn

  • How to set text in TextView
  • Concat the number

Assignment

  • Design a layout of numbers and textview
  • Concat number in TextView on button click

Procedure

First, we need to add the Button in our design activity activity_main.xml file. Give a unique id to this button and set text as per your choice to make it look like a backspace.

image

Then We need to add this button to our MainActivity.java file. Declare and initialize the button and also set the On-click listener for it.

   trim_btn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

       }

image

Here we need to write the logic to trim one number from the screen1_tv. We also need to care if there is no number means the screen1_tv is empty then we need to stop trimming the number else it will give us an error and crash the app.

 if (screen1_tv.getText() != "") {
                    screen1_tv.setText(screen1_tv.getText().toString().substring(0, screen1_tv.getText().length() - 1));
                }

Here we are using the substring() method. We need to provide one or two parameters in it. Forst is for the string start index and second to the string end index. I use the end index as the length of the text in screen1_tv and a minus 1 from it to remove the last digit.

image

Now let's run the app and check whether it is working.

So we are getting errors. I set the button text to <-- which is resulting in an error We need to change the text. Now I am putting it to CE and rerun the app. Now our app is working fine

image


hl_divider.png

Thank You

hl_footer_banner.png

Sort:  

Thanks for this another lesson, so now we can see the numbers to be able to calculate!!
!1UP

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. 
 

1UP-PIZZA.png

You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
____
And they will bring !PIZZA 🍕.

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

Yay! 🤗
Your content has been boosted with Ecency Points, by @faisalamin.
Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for new Proposal
Delegate HP and earn more