make it look nice

main
Brett 2024-10-16 22:48:25 -04:00
parent ee26f6b659
commit 372f34ff03
6 changed files with 267 additions and 316 deletions

View File

@ -8,8 +8,34 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class MainCalculatorActivity extends AppCompatActivity { public class MainCalculatorActivity extends AppCompatActivity {
public static String formatDecimal(double value) {
BigDecimal decimalValue = BigDecimal.valueOf(value);
String pattern;
if (decimalValue.stripTrailingZeros().scale() <= 0) {
pattern = "#,##0";
} else {
pattern = "#,##0.##";
}
DecimalFormat df = new DecimalFormat(pattern);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(symbols);
return df.format(value);
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="16dp" />
</shape>

View File

@ -16,6 +16,7 @@
android:clickable="false" android:clickable="false"
android:minHeight="?attr/actionBarSize" android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" android:theme="?attr/actionBarTheme"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextAppearance="@style/TextAppearance.AppCompat.Large"> app:subtitleTextAppearance="@style/TextAppearance.AppCompat.Large">
<TextView <TextView
@ -30,9 +31,6 @@
android:textSize="34sp" /> android:textSize="34sp" />
</com.google.android.material.appbar.MaterialToolbar> </com.google.android.material.appbar.MaterialToolbar>
<!--I know you can use a grid as shown in class but linear layout feels-->
<!--like the more modern solution, plus i just like it more :3-->
<!--it also feels more contained which i like-->
<LinearLayout <LinearLayout
android:id="@+id/alignmentLayout" android:id="@+id/alignmentLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -46,7 +44,7 @@
android:id="@+id/calsbar" android:id="@+id/calsbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="2" android:layout_weight="10"
android:orientation="vertical"> android:orientation="vertical">
<View <View
@ -58,7 +56,7 @@
<TextView <TextView
android:id="@+id/output" android:id="@+id/output"
style="@style/CalculatorButtonTextStyle" style="@style/TextAutoScaleStyle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="10" android:layout_weight="10"

View File

@ -7,339 +7,233 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainCalculatorActivity"> tools:context=".MainCalculatorActivity">
<Button <!--I know you can use a grid as shown in class but linear layout feels-->
android:id="@+id/bs" <!--like the more modern solution, plus i just like it more :3-->
android:layout_width="0dp" <!--it also feels more contained which i like-->
android:layout_height="0dp" <LinearLayout
android:layout_marginStart="4dp" android:id="@+id/alignmentLayout"
android:layout_marginTop="2dp" android:layout_width="match_parent"
android:layout_marginEnd="2dp" android:layout_height="match_parent"
android:text="S" android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent" android:orientation="vertical">
app:layout_constraintEnd_toStartOf="@+id/br"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b0" />
<Button <LinearLayout
android:id="@+id/bc" android:id="@+id/titlebar"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginStart="2dp" android:layout_weight="1"
android:layout_marginTop="2dp" android:orientation="vertical">
android:layout_marginEnd="2dp"
android:text="C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/ba"
app:layout_constraintStart_toEndOf="@+id/br"
app:layout_constraintTop_toBottomOf="@+id/bequals" />
<Button
android:id="@+id/ba"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp"
android:text="A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bc"
app:layout_constraintTop_toBottomOf="@+id/bdiv" />
<Button
android:id="@+id/br"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:text="R"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/bc"
app:layout_constraintStart_toEndOf="@+id/bs"
app:layout_constraintTop_toBottomOf="@+id/bdot" />
<Button
android:id="@+id/b0"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="0"
app:layout_constraintBottom_toTopOf="@+id/bs"
app:layout_constraintEnd_toStartOf="@+id/bdot"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b1" />
<Button
android:id="@+id/bequals"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="="
app:layout_constraintBottom_toTopOf="@+id/bc"
app:layout_constraintEnd_toStartOf="@+id/bdiv"
app:layout_constraintStart_toEndOf="@+id/bdot"
app:layout_constraintTop_toBottomOf="@+id/b3" />
<Button
android:id="@+id/bdiv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="2dp"
android:text="÷"
app:layout_constraintBottom_toTopOf="@+id/ba"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bequals"
app:layout_constraintTop_toBottomOf="@+id/bmul" />
<Button
android:id="@+id/bdot"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="."
app:layout_constraintBottom_toTopOf="@+id/br"
app:layout_constraintEnd_toStartOf="@+id/bequals"
app:layout_constraintStart_toEndOf="@+id/b0"
app:layout_constraintTop_toBottomOf="@+id/b2" />
<Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/bdot"
app:layout_constraintEnd_toStartOf="@+id/b3"
app:layout_constraintStart_toEndOf="@+id/b1"
app:layout_constraintTop_toBottomOf="@+id/b5" />
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/b0"
app:layout_constraintEnd_toStartOf="@+id/b2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b4" />
<Button
android:id="@+id/bmul"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="2dp"
android:text="x"
app:layout_constraintBottom_toTopOf="@+id/bdiv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/b3"
app:layout_constraintTop_toBottomOf="@+id/bminus" />
<Button
android:id="@+id/b3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="3"
app:layout_constraintBottom_toTopOf="@+id/bequals"
app:layout_constraintEnd_toStartOf="@+id/bmul"
app:layout_constraintStart_toEndOf="@+id/b2"
app:layout_constraintTop_toBottomOf="@+id/b6" />
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/materialToolbar" android:id="@+id/materialToolbar"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/colorPrimary" android:layout_weight="1"
android:background="#7986CB"
android:clickable="false" android:clickable="false"
android:minHeight="?attr/actionBarSize" android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" android:theme="?attr/actionBarTheme">
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView <TextView
android:id="@+id/titleText" android:id="@+id/titleText"
android:layout_width="wrap_content" style="@style/TextAutoScaleStyle"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginStart="16dp" android:layout_height="match_parent"
android:layout_marginTop="16dp" android:layout_marginStart="8dp"
android:layout_marginBottom="16dp" android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Balculator" android:text="Balculator"
android:textColor="#FBFBFB" android:textColor="#FBFBFB" />
android:textSize="34sp" </com.google.android.material.appbar.MaterialToolbar>
app:layout_constraintBottom_toBottomOf="@+id/materialToolbar" </LinearLayout>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <LinearLayout
android:id="@+id/calsbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<View <View
android:id="@+id/divider" android:id="@+id/divider"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="4dp" android:layout_height="1dp"
android:layout_marginTop="4dp" android:layout_weight="1"
android:background="?android:attr/listDivider" android:background="?android:attr/listDivider" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialToolbar" />
<TextView <TextView
android:id="@+id/output" android:id="@+id/output"
android:layout_width="0dp" style="@style/TextAutoScaleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_weight="10"
android:layout_marginTop="6dp" android:gravity="end|center_vertical"
app:layout_constraintStart_toStartOf="parent" android:paddingStart="8dp"
app:layout_constraintTop_toBottomOf="@+id/divider" /> android:paddingEnd="8dp"
android:text="" />
<View <View
android:id="@+id/divider2" android:id="@+id/divider2"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="4dp" android:layout_height="1dp"
android:layout_marginTop="6dp" android:layout_weight="1"
android:background="?android:attr/listDivider" android:background="?android:attr/listDivider" />
app:layout_constraintEnd_toEndOf="parent" </LinearLayout>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/output" /> <LinearLayout
android:id="@+id/row1"
android:paddingTop="2dp"
style="@style/LinearLayoutStyle">
<Button <Button
android:id="@+id/b7" android:id="@+id/bpar"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="#4DB6AC"
android:layout_marginStart="4dp" android:text="( )" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="7"
app:layout_constraintBottom_toTopOf="@+id/b4"
app:layout_constraintEnd_toStartOf="@+id/b8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider2" />
<Button <Button
android:id="@+id/b8" android:id="@+id/bac"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/clearButtons"
android:layout_marginStart="2dp" android:text="AC" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="8"
app:layout_constraintBottom_toTopOf="@+id/b5"
app:layout_constraintEnd_toStartOf="@+id/b9"
app:layout_constraintStart_toEndOf="@+id/b7"
app:layout_constraintTop_toBottomOf="@+id/divider2" />
<Button <Button
android:id="@+id/b9" android:id="@+id/bc"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/clearButtons"
android:layout_marginStart="2dp" android:text="C" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="9"
app:layout_constraintBottom_toTopOf="@+id/b6"
app:layout_constraintEnd_toStartOf="@+id/bplus"
app:layout_constraintStart_toEndOf="@+id/b8"
app:layout_constraintTop_toBottomOf="@+id/divider2" />
<Button <Button
android:id="@+id/bplus" android:id="@+id/bplus"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/operatorButtons"
android:layout_marginStart="2dp" android:text="+" />
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp" </LinearLayout>
android:layout_marginBottom="2dp"
android:text="+" <LinearLayout
app:layout_constraintBottom_toTopOf="@+id/bminus" android:id="@+id/row2"
app:layout_constraintEnd_toEndOf="parent" style="@style/LinearLayoutStyle">
app:layout_constraintStart_toEndOf="@+id/b9"
app:layout_constraintTop_toBottomOf="@+id/divider2" /> <Button
android:id="@+id/b7"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="7" />
<Button
android:id="@+id/b8"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="8" />
<Button
android:id="@+id/b9"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="9" />
<Button
android:id="@+id/bdiv"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/operatorButtons"
android:text="÷" />
</LinearLayout>
<LinearLayout
android:id="@+id/row3"
style="@style/LinearLayoutStyle">
<Button <Button
android:id="@+id/b4" android:id="@+id/b4"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/numberButtons"
android:layout_marginStart="4dp" android:text="4" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="4"
app:layout_constraintBottom_toTopOf="@+id/b1"
app:layout_constraintEnd_toStartOf="@+id/b5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b7" />
<Button <Button
android:id="@+id/b5" android:id="@+id/b5"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/numberButtons"
android:layout_marginStart="2dp" android:text="5" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:text="5"
app:layout_constraintBottom_toTopOf="@+id/b2"
app:layout_constraintEnd_toStartOf="@+id/b6"
app:layout_constraintStart_toEndOf="@+id/b4"
app:layout_constraintTop_toBottomOf="@+id/b8" />
<Button <Button
android:id="@+id/b6" android:id="@+id/b6"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/numberButtons"
android:layout_marginStart="2dp" android:text="6" />
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp" <Button
android:layout_marginBottom="2dp" android:id="@+id/bmul"
android:text="6" style="@style/CalculatorButtonTextStyle"
app:layout_constraintBottom_toTopOf="@+id/b3" android:backgroundTint="@color/operatorButtons"
app:layout_constraintEnd_toStartOf="@+id/bminus" android:text="*" />
app:layout_constraintStart_toEndOf="@+id/b5"
app:layout_constraintTop_toBottomOf="@+id/b9" /> </LinearLayout>
<LinearLayout
android:id="@+id/row4"
style="@style/LinearLayoutStyle">
<Button
android:id="@+id/b1"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="1" />
<Button
android:id="@+id/b2"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="2" />
<Button
android:id="@+id/b3"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="3" />
<Button <Button
android:id="@+id/bminus" android:id="@+id/bminus"
android:layout_width="0dp" style="@style/CalculatorButtonTextStyle"
android:layout_height="0dp" android:backgroundTint="@color/operatorButtons"
android:layout_marginStart="2dp" android:text="-" />
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp" </LinearLayout>
android:layout_marginBottom="2dp"
android:text="-" <LinearLayout
app:layout_constraintBottom_toTopOf="@+id/bmul" android:id="@+id/row5"
app:layout_constraintEnd_toEndOf="parent" android:paddingBottom="2dp"
app:layout_constraintHorizontal_bias="1.0" style="@style/LinearLayoutStyle">
app:layout_constraintStart_toEndOf="@+id/b6"
app:layout_constraintTop_toBottomOf="@+id/bplus" /> <Button
android:id="@+id/bdot"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/specialButtons"
android:text="." />
<Button
android:id="@+id/b0"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/numberButtons"
android:text="0" />
<Button
android:id="@+id/bneg"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/specialButtons"
android:text="+/-" />
<!-- i put equals last because it puts it close to the thumb-->
<Button
android:id="@+id/bequals"
style="@style/CalculatorButtonTextStyle"
android:backgroundTint="@color/equalsButton"
android:text="=" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -7,4 +7,9 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="specialButtons">#4DB6AC</color>
<color name="clearButtons">#E57373</color>
<color name="operatorButtons">#81C784</color>
<color name="numberButtons">#7986CB</color>
<color name="equalsButton">#4FC3F7</color>
</resources> </resources>

View File

@ -1,9 +1,33 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<style name="CalculatorButtonTextStyle"> <style name="TextAutoScaleStyle">
<item name="autoSizeTextType">uniform</item> <item name="autoSizeTextType">uniform</item>
<item name="autoSizeMinTextSize">10sp</item> <item name="autoSizeMinTextSize">10sp</item>
<item name="autoSizeMaxTextSize">256sp</item> <item name="autoSizeMaxTextSize">256sp</item>
<item name="autoSizeStepGranularity">2sp</item> <item name="autoSizeStepGranularity">2sp</item>
</style> </style>
<style name="CalculatorButtonTextStyle">
<item name="autoSizeTextType">uniform</item>
<item name="autoSizeMinTextSize">10sp</item>
<item name="autoSizeMaxTextSize">256sp</item>
<item name="autoSizeStepGranularity">2sp</item>
<item name="android:layout_marginStart">4dp</item>
<item name="android:layout_marginEnd">4dp</item>
<item name="android:maxLines">1</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
</style>
<style name="LinearLayoutStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:background">#2F2F2F</item>
<item name="android:orientation">horizontal</item>
<item name="android:paddingLeft">2dp</item>
<item name="android:paddingRight">2dp</item>
</style>
</resources> </resources>