Junior — Middle
Can you give examples of methods used for working with View in development?
sobes.tech AI
Answer from AI
In Android, the following methods are often used to work with Views:
findViewById(int id)— to find a View by its identifier.setOnClickListener(View.OnClickListener listener)— to set a click handler.setVisibility(int visibility)— to control the visibility of a View (VISIBLE,INVISIBLE,GONE).setText(CharSequence text)— to change the text in a TextView or Button.setBackgroundColor(int color)— to change the background color of a View.invalidate()— to redraw the View.
Example:
Button button = findViewById(R.id.my_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setText("Pressed");
button.setBackgroundColor(Color.RED);
}
});