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