Last active
March 25, 2017 21:02
-
-
Save amandaroos/23a9e3064deec35effbfdbf33d7118db to your computer and use it in GitHub Desktop.
Code for creating a simple Android counter app
This file contains hidden or 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 code for creating the count TextView | |
| <TextView | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:gravity="center" | |
| android:textSize="120sp" | |
| android:id="@+id/count" | |
| android:text="0" /> | |
| //Java code for increasing the count and displaying it | |
| int count = 0; | |
| //Note: in this video, https://www.youtube.com/watch?v=DeutHs8Bz5w, | |
| //I initialize countText in the displayCount method, | |
| //but it's actually better to do it outside the method | |
| TextView countText = (TextView) findViewById(R.id.count); | |
| public void displayCount(int count) { | |
| countText.setText(String.valueOf(count)); | |
| } | |
| public void increaseCount (View view) { | |
| count +=1; | |
| displayCount(count); | |
| } | |
| //Java code to put inside the Floating Action Button's onClick method | |
| //This will increase the count everytime the button is tapped | |
| increaseCount(view); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment