MatiasMerkuri
4/12/2019 - 5:14 PM

EVENT LISTENER AND CALLBACK METHOD

EVENT HANDLING IN AN ANDROID APP

// Declaring a button, treating it as an object and finding the button that should be listened to by ID
Button button = (Button)findViewById(R.id.button);

// Event handling method (click)
        button.setOnClickListener(
          // Adding the listener
                new Button.OnClickListener(){
                  // Event executed method
                    public void onClick(View v) {
                      // Declaring a textView, treating it as an object and finding the textView that should change value when event is executed
                        TextView textView = (TextView)findViewById(R.id.textView);
                        // Changing the value of the textView
                        textView.setText("Clicked");
                    }
                }
        );
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="button">Click</string>
    <string name="textView">Text</string>
</resources>