Commit 82a72def authored by Christopher League's avatar Christopher League
Browse files

Working with reset and rand buttons too

parent 876b8cf7
......@@ -6,12 +6,15 @@ import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final private String COUNTER = "counter";
private Random rng = new Random();
private TextView textView;
private Button decrButton;
private Button decrButton, resetButton;
private int counter = 0;
@Override
......@@ -20,6 +23,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
setContentView(R.layout.activity_main);
this.textView = findViewById(R.id.textView);
this.decrButton = findViewById(R.id.decrement);
this.resetButton = findViewById(R.id.reset);
if(savedInstanceState == null) {
System.out.println("No existing bundle");
setCounter(0);
......@@ -36,6 +40,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
outState.putInt(COUNTER, counter);
}
public void anotherClick(View view) {
}
@Override
public void onClick(View view) {
switch(view.getId()) {
......@@ -45,12 +53,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
case R.id.decrement:
setCounter(counter-1);
break;
case R.id.reset:
setCounter(0);
break;
case R.id.random:
setCounter(rng.nextInt(1000));
break;
}
}
private void setCounter(int newValue) {
counter = newValue;
textView.setText(getString(R.string.counter_is, counter));
textView.setText(Integer.toString(counter));
decrButton.setEnabled(counter > 0);
resetButton.setEnabled(counter != 0);
}
}
......@@ -12,6 +12,8 @@
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/greeting"
android:textSize="54sp"
app:fontFamily="sans-serif-black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
......@@ -20,17 +22,18 @@
<LinearLayout
android:id="@+id/linearLayout"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/decrement"
style="?android:attr/borderlessButtonStyle"
android:layout_width="105dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/decrement"
......@@ -43,13 +46,28 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
style="?android:attr/borderlessButtonStyle"
android:text="@string/increment"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/decrement"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/reset" />
<Button
android:id="@+id/random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/random" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<resources>
<string name="app_name">FloodGame</string>
<string name="greeting">Hello class, this is a test.</string>
<string name="greeting">ABC</string>
<string name="counter_is">Counter is ((%d))</string>
<string name="decrement">Decrement</string>
<string name="increment">Increment</string>
<string name="decrement">Decr</string>
<string name="increment">Incr</string>
<string name="reset">Reset</string>
<string name="random">Rand</string>
</resources>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment