Square a Number App

Screenshot_1499702736

Java
package com.wordpress.joelkingsley.projectx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    public void calculate(View v){

        EditText input = (EditText) findViewById(R.id.etInput);
        TextView output = (TextView) findViewById(R.id.tvOutput);

        int base = Integer.valueOf(input.getText().toString());
        int result = base*base;

        output.setText("Result:" + result);
    }
}

XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wordpress.joelkingsley.projectx.MainActivity">

    <TextView
        android:id="@+id/input_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter an integer"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintLeft_toLeftOf="@+id/tvOutput"
        app:layout_constraintRight_toRightOf="@+id/tvOutput"
        app:layout_constraintBottom_toTopOf="@+id/etInput"
        app:layout_constraintTop_toBottomOf="@+id/tvOutput" />

    <EditText
        android:id="@+id/etInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Number"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toTopOf="@+id/btnCalculate"
        app:layout_constraintLeft_toLeftOf="@+id/input_label"
        app:layout_constraintRight_toRightOf="@+id/input_label"
        app:layout_constraintTop_toBottomOf="@+id/input_label" />

    <Button
        android:id="@+id/btnCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="calculate"
        android:text="Calculate"
        app:layout_constraintRight_toRightOf="@+id/etInput"
        app:layout_constraintLeft_toLeftOf="@+id/etInput"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etInput" />

    <TextView
        android:id="@+id/tvOutput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result:"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/input_label"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.501" />

</android.support.constraint.ConstraintLayout>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.