Commit 876b8cf7 authored by Christopher League's avatar Christopher League
Browse files

Increment/decrement counter

parent adc95a40
......@@ -2,12 +2,55 @@ package edu.liu.floodgame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final private String COUNTER = "counter";
private TextView textView;
private Button decrButton;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView = findViewById(R.id.textView);
this.decrButton = findViewById(R.id.decrement);
if(savedInstanceState == null) {
System.out.println("No existing bundle");
setCounter(0);
}
else {
System.out.println("Using existing bundle");
setCounter(savedInstanceState.getInt(COUNTER));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(COUNTER, counter);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.increment:
setCounter(counter+1);
break;
case R.id.decrement:
setCounter(counter-1);
break;
}
}
private void setCounter(int newValue) {
counter = newValue;
textView.setText(getString(R.string.counter_is, counter));
decrButton.setEnabled(counter > 0);
}
}
......@@ -7,12 +7,49 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginTop="8dp"
android:text="@string/greeting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<LinearLayout
android:id="@+id/linearLayout"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/decrement"
style="?android:attr/borderlessButtonStyle"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/decrement"
app:layout_constraintEnd_toStartOf="@+id/increment"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="154dp" />
<Button
android:id="@+id/increment"
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" />
</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="counter_is">Counter is ((%d))</string>
<string name="decrement">Decrement</string>
<string name="increment">Increment</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