Loading your workspace…
A small app with a big first step. Connect views, handle clicks, and preserve state.
Choose a file to explore. Copy it into your own project when you’re ready.
package com.example.counter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private int count = 0;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_main);
if (state != null) count = state.getInt("count", 0);
TextView label = findViewById(R.id.counter);
Button button = findViewById(R.id.increment);
label.setText(String.valueOf(count));
button.setOnClickListener(view -> {
count++;
label.setText(String.valueOf(count));
});
}
@Override
protected void onSaveInstanceState(Bundle state) {
state.putInt("count", count);
super.onSaveInstanceState(state);
}
}