Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Christopher League
FloodGame
Commits
876b8cf7
Commit
876b8cf7
authored
Jul 03, 2019
by
Christopher League
Browse files
Increment/decrement counter
parent
adc95a40
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/edu/liu/floodgame/MainActivity.java
View file @
876b8cf7
...
...
@@ -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
);
}
}
app/src/main/res/layout/activity_main.xml
View file @
876b8cf7
...
...
@@ -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_toTopOf=
"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
app/src/main/res/values/strings.xml
View file @
876b8cf7
<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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment