In Android, a View is a fundamental building block of the user interface (UI) of an app. It represents the basic element on the screen that the user interacts with. Everything that you see and interact with on an Android app screen—such as buttons, text fields, images, and layouts—is a type of View or a subclass of it. Views are used to create the app's interface and respond to user input.
A View is a UI element that occupies a rectangular area on the screen. It could represent anything from a button, an image, or even an entire layout. Views are the building blocks for user interface elements. In Android, almost everything visible to the user is a View.
Here are some of the most common types of Views that are used in Android development:
Example:
TextView textView = new TextView(this);
textView.setText("Hello, Android!");
setContentView(textView);
OnClickListener to respond to button clicks.Example:
Button button = new Button(this);
button.setText("Click Me");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
Example:
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.my_image);
setContentView(imageView);
Example:
EditText editText = new EditText(this);
editText.setHint("Enter your name");
setContentView(editText);
Example:
CheckBox checkBox = new CheckBox(this);
checkBox.setText("Accept Terms and Conditions");
setContentView(checkBox);
Example:
RadioButton radioButton = new RadioButton(this);
radioButton.setText("Option 1");
setContentView(radioButton);
Example:
ImageButton imageButton = new ImageButton(this);
imageButton.setImageResource(R.drawable.icon);
setContentView(imageButton);
Example (Indeterminate):
ProgressBar progressBar = new ProgressBar(this);
setContentView(progressBar);
Example (Determinate):
ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setMax(100);
progressBar.setProgress(50);
setContentView(progressBar);
Example:
Spinner spinner = new Spinner(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_items, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
setContentView(spinner);
In Android, Views are often organized in a hierarchical structure. This structure is necessary for grouping related views together to form a complete layout.
LinearLayout Example:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("Click Me");
linearLayout.addView(button);
setContentView(linearLayout);
RelativeLayout Example:
RelativeLayout relativeLayout = new RelativeLayout(this);
TextView textView = new TextView(this);
textView.setText("This is a text.");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(params);
relativeLayout.addView(textView);
setContentView(relativeLayout);
ConstraintLayout Example:
ConstraintLayout layout = new ConstraintLayout(this);
TextView textView = new TextView(this);
textView.setText("ConstraintLayout Example");
layout.addView(textView);
setContentView(layout);
Views in Android can respond to user interactions like taps, clicks, gestures, and touch events. You can attach Listeners to a View to handle these events.
Button button = new Button(this);
button.setText("Click Me");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
}
});
EditText editText = new EditText(this);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable editable) {
// Code to handle text change
}
});
You can create custom views by extending the View class. This allows you to create completely new UI elements with custom behavior and appearance.
Example:
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Custom drawing code here
}
}
onDraw(Canvas canvas): Override this method to draw custom graphics (e.g., shapes, lines, text).onMeasure(int widthMeasureSpec, int heightMeasureSpec): Override this method to measure the size of your custom view.In Android, Views are the foundation of the user interface. They are used to display information and accept input from the user. From simple UI elements like TextView and Button to complex containers like LinearLayout and RelativeLayout, Views and ViewGroups are used together to create Android’s UI.
Understanding how to work with views and how to listen for user interactions through listeners is essential for building interactive and responsive Android apps. You can also extend the View class to create custom components tailored to your specific needs.
Open this section to load past papers