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

3 comments

faisalamin3.1 K3 years ago4 min read

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello everyone, Welcome to our 14 lecture of the Android App Development series. These lectures are for beginners and we will slowly move to the next category. Today We will design a simple button layout and write the code for it. We use 0-9 number text buttons in our design with unique IDs. We will arrange them in the next lecture by learning a new way to align the, In today's lecture, we will write code for the buttons and textview.

https://images.ecency.com/DQmTvNV4YRCAQDJ6LbPqTcwSLc2c12CdFKj8Ec7zVFCRijt/multi_purpose_4_.png

GitHub Link

Use this GitHub project to clone into your directory. It will always get updated in the next lecture. So that 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 a TextView at the top of the layout and Button. Here is the code for it. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

    <Button
        android:id="@+id/num1_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <Button
        android:id="@+id/num2_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

    <Button
        android:id="@+id/num3_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3" />

    <Button
        android:id="@+id/num4_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4" />

    <Button
        android:id="@+id/num5_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5" />

    <Button
        android:id="@+id/num6_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6" />

    <Button
        android:id="@+id/num7_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7" />

    <Button
        android:id="@+id/num8_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8" />

    <Button
        android:id="@+id/num9_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9" />

    <Button
        android:id="@+id/num0_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0" />


</LinearLayout>

https://images.ecency.com/DQmU6JTfdkL1J2a3PzM5KUXi9kWZmj9G7G3JaxFz8WKRdGm/image.png

Then we need to declare and initialize these items in the MainActivity.java file.

package com.example.hivelearners;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView screen1_tv;
    Button num1_btn,num2_btn,num3_btn,num4_btn,num5_btn,num6_btn,num7_btn,num8_btn,num9_btn,num0_btn;

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

        screen1_tv = findViewById(R.id.screen1_tv);

        num1_btn = findViewById(R.id.num1_btn);
        num2_btn = findViewById(R.id.num2_btn);
        num3_btn = findViewById(R.id.num3_btn);
        num4_btn = findViewById(R.id.num4_btn);
        num5_btn = findViewById(R.id.num5_btn);
        num6_btn = findViewById(R.id.num6_btn);
        num7_btn = findViewById(R.id.num7_btn);
        num8_btn = findViewById(R.id.num8_btn);
        num9_btn = findViewById(R.id.num9_btn);
        num0_btn = findViewById(R.id.num0_btn);


        num1_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });


    }
}

https://images.ecency.com/DQmR222FbMr3siksfocxUwdQgxvnRw6Act7qVMTxQy9Amhv/image.png

Now we need to set the text in the textview when a user clicks on the button. We will pick the text from the button and show it in screen1_tv. But here is one issue. When we click on any button it will replace the existing text in the screen1_tv. So we need to contact the text from the screen1_tv with the button text even if it is empty. Here is a simple way to do this. This time we will use the concat function to do this.

https://images.ecency.com/DQmS3RvRZcyrb3JpNSMRWukbsKH32rUnLekPVd4moymqVvE/image.png

Now we copy-paste this listener for all the buttons and change the button IDs accordingly.



        num1_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num1_btn.getText().toString()));

            }
        });

        num2_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num2_btn.getText().toString()));

            }
        });

        num3_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num3_btn.getText().toString()));

            }
        });

        num4_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num4_btn.getText().toString()));

            }
        });
        num5_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num5_btn.getText().toString()));

            }
        });
        num6_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num6_btn.getText().toString()));

            }
        });
        num7_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num7_btn.getText().toString()));

            }
        });

        num8_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num8_btn.getText().toString()));

            }
        });
        num9_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num9_btn.getText().toString()));

            }
        });
        num0_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                screen1_tv.setText(screen1_tv.getText().toString().concat(num0_btn.getText().toString()));

            }
        });

https://images.ecency.com/DQmPv8VvHV7ZKJqKUqLK7EkMhk14LyeMKwUtANnowKPPvvk/image.png

Now run the app and click on the button and check if the code is working or not.

https://images.ecency.com/DQmcv9DT754FFUe36jRoGKySJxK5dasGSMf9bGxTZeDFKXz/image.png


https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png

Thank You

https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png

https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif


Hashtags 9
Learn Everything About Hive Thoroughly, Be A Better Hivian

Comments

Sort byBest