Created
October 25, 2020 20:20
-
-
Save slimani-dev/1ab4698715d506e1413d5c5c823b2560 to your computer and use it in GitHub Desktop.
A simple android calculator (XML + JAVA)
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"?> | |
<LinearLayout 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="#000000" | |
android:orientation="vertical" | |
tools:context=".MainActivity" | |
tools:ignore="HardcodedText"> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_marginBottom="1pt" | |
android:layout_weight="1" | |
android:background="#000000" | |
android:ems="10" | |
android:gravity="right" | |
android:inputType="textPersonName" | |
android:text="0" | |
android:textColor="#ffffff" | |
android:textSize="20pt" /> | |
<TableLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
tools:ignore="ButtonStyle"> | |
<TableRow | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginBottom="1pt"> | |
<Button | |
android:id="@+id/bu7" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="7" /> | |
<Button | |
android:id="@+id/bu8" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="8" /> | |
<Button | |
android:id="@+id/bu9" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="9" /> | |
<Button | |
android:id="@+id/buAC" | |
android:layout_width="5pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="ACOnClick" | |
android:background="#C0C0C0" | |
android:text="AC" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginBottom="1pt"> | |
<Button | |
android:id="@+id/bu4" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="4" /> | |
<Button | |
android:id="@+id/bu5" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="5" /> | |
<Button | |
android:id="@+id/bu6" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="6" /> | |
<Button | |
android:id="@+id/buPlus" | |
android:layout_width="5pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginEnd="1pt" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="+" | |
android:textColor="#000000" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginBottom="1pt"> | |
<Button | |
android:id="@+id/bu1" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="1" | |
android:layout_marginEnd="1pt" /> | |
<Button | |
android:id="@+id/bu2" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="2" | |
android:layout_marginEnd="1pt" /> | |
<Button | |
android:id="@+id/bu3" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="3" | |
android:layout_marginEnd="1pt" /> | |
<Button | |
android:id="@+id/buMinus" | |
android:layout_width="5pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="-" | |
android:textColor="#000000" | |
android:layout_marginEnd="1pt" /> | |
</TableRow> | |
</TableLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<TableRow | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:ignore="UselessParent"> | |
<Button | |
android:id="@+id/bu0" | |
android:layout_width="60pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="2" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="0" | |
android:layout_marginEnd="1pt" /> | |
<Button | |
android:id="@+id/buDot" | |
android:layout_width="30pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:onClick="padButtonOnClick" | |
android:background="#C0C0C0" | |
android:text="." | |
android:layout_marginEnd="1pt" /> | |
<Button | |
android:id="@+id/buEqual" | |
android:layout_width="5pt" | |
android:layout_height="20pt" | |
android:layout_margin="5dp" | |
android:layout_marginRight="1pt" | |
android:layout_weight="1" | |
android:background="#C0C0C0" | |
android:text="=" | |
android:textColor="#000000" | |
android:layout_marginEnd="1pt" /> | |
</TableRow> | |
</LinearLayout> | |
</LinearLayout> |
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.calculator; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class MainActivity extends AppCompatActivity { | |
String text = "0"; | |
EditText screenText; | |
public static String TAG = "FirstFragment"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
screenText = findViewById(R.id.editText); | |
} | |
public void padButtonOnClick(View source) { | |
Log.d(TAG, "padButton: " + ((Button) source).getText()); | |
updateText(((Button) source).getText().toString()); | |
} | |
public void updateText(String text) { | |
if (this.text.trim().equals("0")) { | |
this.text = text; | |
} else { | |
this.text = this.text.concat(text); | |
} | |
screenText.setText(this.text); | |
} | |
public void ACOnClick(View view) { | |
this.text = "0"; | |
screenText.setText(this.text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment