Open In App
Related Articles

Android | Display multiplication table of a number

Like Article
Save Article
Report issue

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.


Filename: activity_main.xml


<!-- First make the layout file xml and add button, edit text, text view -->
<?xml version="1.0" encoding="utf-8"?>
    <!-- Add the button for run table logic and print result-->
    <!-- give id "button"-->
        app:layout_constraintTop_toTopOf="parent" />
    <!-- Text view for result view-->
    <!-- give the id TextView-->
        app:layout_constraintTop_toBottomOf="@+id/editText" />
    <!-- edit Text for take input from user-->
    <!-- give the id editText-->
        tools:ignore="UnknownId" />


Filename: MainActivity.Java


// Build the java logic for multiplication table
// using button, text view, edit text
package com.example.windows10.table;
import android.content.Intent;
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)
        // by ID we can use each component
        // whose id is assigned in the XML file
        editText = (EditText)findViewById(;
        button = (Button)findViewById(;
        result = (TextView)findViewById(;
        // set clickListener on button
    public void onClick(View v)
        switch (v.getId()) {
            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




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