𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
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.
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.
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) {
}
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.
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

Thank You
