PR:12
activity_main.xml :
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvDisplay"
android:text="0"
android:textSize="32sp"
android:gravity="end"
android:padding="12dp"
android:background="#EEEEEE"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<GridLayout
android:columnCount="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">
<Button android:text="7" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="8" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="9" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="/" android:onClick="onOperator"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="4" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="5" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="6" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="*" android:onClick="onOperator"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="1" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="2" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="3" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="-" android:onClick="onOperator"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="0" android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="." android:onClick="onClick"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="=" android:onClick="onEqual"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button android:text="+" android:onClick="onOperator"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_columnWeight="1"/>
<Button
android:text="C"
android:onClick="onClear"
android:layout_columnSpan="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</GridLayout>
</LinearLayout>
[Link] :
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
TextView tvDisplay;
double first = 0, second = 0;
String operator = "";
boolean isNewInput = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tvDisplay = findViewById([Link]);
}
public void onClick(View v) {
Button b = (Button) v;
if (isNewInput) {
[Link]("");
isNewInput = false;
}
[Link]([Link]().toString());
}
public void onOperator(View v) {
try {
first = [Link]([Link]().toString());
operator = ((Button) v).getText().toString();
isNewInput = true;
} catch (Exception e) {
[Link](this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
public void onEqual(View v) {
try {
second = [Link]([Link]().toString());
double result = 0;
switch (operator) {
case "+": result = first + second; break;
case "-": result = first - second; break;
case "*": result = first * second; break;
case "/":
if (second == 0) {
[Link](this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
result = first / second;
break;
}
[Link]([Link](result));
isNewInput = true;
} catch (Exception e) {
[Link](this, "Error", Toast.LENGTH_SHORT).show();
}
}
public void onClear(View v) {
[Link]("0");
first = second = 0;
operator = "";
isNewInput = true;
}
}
Output Screenshot :