Skip to content

Instantly share code, notes, and snippets.

@loeschg
Last active August 29, 2015 14:01
Show Gist options
  • Save loeschg/7e3390234da86df2a083 to your computer and use it in GitHub Desktop.
Save loeschg/7e3390234da86df2a083 to your computer and use it in GitHub Desktop.
ALG - Session 1 - MainActivity.java
package com.weddingwire.android.learninggroup.session1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Activity to increment a textfield on button press.
*/
public class MainActivity extends Activity {
/** Member variable keeping track of the count. */
private int counter = 0;
/** Member variable keeping reference to textview. */
private TextView textView;
/** onCreate is called before onResume(). */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Link this activity with the xml layout file.
setContentView(R.layout.activity_main);
// Get a reference to the TextView we'll update on button tap.
textView = (TextView) findViewById(R.id.textView);
textView.setText(Integer.toString(counter));
// Get a reference to the button
Button button = (Button) findViewById(R.id.button);
// Add a listener to the button.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Increment the counter and set the text.
counter++;
textView.setText(Integer.toString(counter));
}
});
}
/** onResume() is called after onCreate() on first app launch and when
* resuming an app from the background/sleep.
*/
@Override
protected void onResume() {
super.onResume();
// To demonstrate when onResume() is called in the Activity lifecycle.
textView.setText("on resume");
}
}
@loeschg
Copy link
Author

loeschg commented May 21, 2014

activity_main.xml can be found here: https://gist.github.com/loeschg/74d6b372b3802dec6727

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment