Last active
October 9, 2022 07:54
-
-
Save afifabroory/386fd95fa37e88756f0cb3ee6f8eff25 to your computer and use it in GitHub Desktop.
HelloConstraint Homework
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" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/show_count" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:layout_marginTop="16dp" | |
android:layout_marginEnd="16dp" | |
android:background="#FFFF00" | |
android:gravity="center" | |
android:text="@string/label_count_initial" | |
android:textAlignment="center" | |
android:textColor="@color/purple_500" | |
android:textSize="160sp" | |
android:textStyle="bold" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/button_toast" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/button_count" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:backgroundTint="@color/purple_700" | |
android:onClick="countUp" | |
android:text="@string/button_label_count" | |
android:textColor="@color/white" | |
app:layout_constraintBottom_toBottomOf="@+id/show_count" | |
app:layout_constraintStart_toStartOf="parent" /> | |
<Button | |
android:id="@+id/button_toast" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:backgroundTint="@color/purple_200" | |
android:onClick="showToast" | |
android:text="@string/button_label_toast" | |
android:textColor="@color/white" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="@+id/show_count" /> | |
<Button | |
android:id="@+id/button_zero" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:backgroundTint="@android:color/darker_gray" | |
android:onClick="countReset" | |
android:text="@string/button_label_zero" | |
android:textColor="@color/white" | |
app:layout_constraintBottom_toTopOf="@+id/button_count" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/button_toast" /> | |
</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.helloconstraint; | |
import android.content.res.ColorStateList; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import com.example.helloconstraint.databinding.ActivityMainBinding; | |
public class MainActivity extends AppCompatActivity { | |
private int mCount = 0; | |
private int mCountBackgroundTint = 0xFF3700B3; | |
private ActivityMainBinding binding; | |
private boolean zeroFlag = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// findViewById Replacement | |
// https://developer.android.com/topic/libraries/view-binding | |
binding = ActivityMainBinding.inflate(getLayoutInflater()); | |
setContentView(binding.getRoot()); // Set Activity (UI) yang akan digunakan. | |
} | |
private void setShowCountText() { | |
binding.showCount.setText(Integer.toString(mCount)); | |
} | |
private void changeCountButton() { | |
mCountBackgroundTint = (mCount%2 == 0) ? 0xFF3700B3 : 0xFF018786; | |
binding.buttonCount.setBackgroundTintList(ColorStateList.valueOf(mCountBackgroundTint)); | |
} | |
public void showToast(View view) { | |
Toast toast = Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT); | |
toast.show(); | |
} | |
public void countUp(View view) { | |
mCount++; | |
if (mCount > 0 && zeroFlag) { | |
binding.buttonZero.setBackgroundTintList(ColorStateList.valueOf(0xFFCC0000)); | |
zeroFlag = false; | |
} | |
changeCountButton(); | |
setShowCountText(); | |
} | |
public void countReset(View view) { | |
mCount = 0; | |
if (!zeroFlag) { | |
view.setBackgroundTintList(ColorStateList.valueOf(0xFFAAAAAA)); | |
zeroFlag = true; | |
} | |
changeCountButton(); | |
setShowCountText(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment