Button myButton = findViewById(R.id.button_id); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something when button is clicked } });
TextView myTextView = findViewById(R.id.textview_id); myTextView.setEnabled(false);This code finds a text view element with the ID "textview_id" in the current layout, and disables it so that it cannot accept user input. Based on the package name, the findViewById method is part of the android.view package library.