Created
April 5, 2022 20:20
-
-
Save wakoliVotes/ac80bcf738ba142746642d9a6ccb8730 to your computer and use it in GitHub Desktop.
Adds numbers by taking input from 2 EditTexts and displays result in TextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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" | |
android:background="#FAFA" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/textview1" | |
android:layout_width="wrap_content" | |
android:layout_height="46dp" | |
android:text="Adding Two Numbers" | |
android:textAlignment="center" | |
android:textSize="30sp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toTopOf="@+id/num1" | |
app:layout_constraintHorizontal_bias="0.495" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.366" /> | |
<EditText | |
android:id="@+id/num1" | |
android:layout_width="300dp" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="60dp" | |
android:hint="Enter First Number" | |
android:inputType="numberDecimal" | |
android:textAlignment="center" | |
android:textColorHint="#78909C" | |
android:textSize="25sp" | |
android:textStyle="bold" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.513" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/textview1" /> | |
<EditText | |
android:id="@+id/num2" | |
android:layout_width="300dp" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="144dp" | |
android:hint="Enter Second Number" | |
android:inputType="numberDecimal" | |
android:textAlignment="center" | |
android:textSize="25sp" | |
android:textStyle="bold" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.504" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/num1" /> | |
<TextView | |
android:id="@+id/result" | |
android:layout_width="297dp" | |
android:layout_height="63dp" | |
android:layout_marginTop="108dp" | |
android:hint="Result" | |
android:textAlignment="center" | |
android:textColorHint="#78909C" | |
android:textSize="25sp" | |
android:textStyle="bold" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/num2" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="182dp" | |
android:layout_height="50dp" | |
android:text="Click to Sum" | |
android:textAllCaps="false" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/result" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.addtwonumbers; | |
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; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Declaring variables | |
EditText num1 = findViewById(R.id.num1); | |
EditText num2 = findViewById(R.id.num2); | |
TextView result = findViewById(R.id.result); | |
Button button = findViewById(R.id.button); | |
button.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View view){ | |
double value1 = Double.parseDouble(num1.getText().toString()); | |
double value2 = Double.parseDouble(num2.getText().toString()); | |
double sum = value1 + value2; | |
result.setText(Double.toString(sum)); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.addtwonumbers"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.AddTwoNumbers"> | |
<activity | |
android:name=".MainActivity" | |
android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment