Open In App

Android | Display multiplication table of a number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number, the task is to display the multiplication table of this number using the Android App.

Steps to build app:

  • STEP-1: Open activity_main.xml file and add TextView, EditText, and a Button
  • STEP-2: Assign ID to each component
  • STEP-3: Now, open up the MainActivity file and declare the variables.
  • STEP-4: Read the values entered in the EditText boxes using an id that has been set in the XML code above.
  • STEP-5: Add a click listener to the Add button
  • STEP-6: When the Add button has been clicked we need to Multiply the values and store it in Buffer
  • STEP-7: Then show the resultant output in the TextView by setting the buffer in the TextView.

Implementation:

Filename: activity_main.xml

XML




<!-- First make the layout file xml and add button, edit text, text view -->
 
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="25dp">
 
    <!-- Add the button for run table logic and print result-->
    <!-- give id "button"-->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="TABLE"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent" />
 
    <!-- Text view for result view-->
    <!-- give the id TextView-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="18dp"
        android:layout_marginEnd="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginStart="36dp"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />
 
    <!-- edit Text for take input from user-->
    <!-- give the id editText-->
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="9dp"
        android:layout_marginRight="9dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="UnknownId" />
</android.support.constraint.ConstraintLayout>


Filename: MainActivity.Java

Java




// Build the java logic for multiplication table
// using button, text view, edit text
 
package com.example.windows10.table;
 
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.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
    implements View.OnClickListener {
 
    // define the global variable
 
    // variable number1, number2 for input input number
    // Add_button, result textView
 
    EditText editText;
    Button button;
    TextView result;
    int ans = 0;
 
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // by ID we can use each component
        // whose id is assigned in the XML file
 
        editText = (EditText)findViewById(R.id.editText);
        button = (Button)findViewById(R.id.button);
        result = (TextView)findViewById(R.id.textView);
 
        // set clickListener on button
        button.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v)
    {
 
        switch (v.getId()) {
 
        case R.id.button:
            StringBuffer buffer = new StringBuffer();
            int res;
 
            // get the input number from editText
            String fs = editText.getText().toString();
 
            // convert it to integer
            int n = Integer.parseInt(fs);
 
            // build the logic for table
            for (int i = 1; i <= 10; i++) {
                ans = (i * n);
                buffer.append(n + " X " + i
                              + " = " + ans + "\n\n");
            }
 
            // set the buffer textview
            result.setText(buffer);
            break;
        }
    }
}


Output:
 

 



Last Updated : 07 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads