In this section, we will explore the fundamental UI components available in Android development. Understanding these components is crucial for building interactive and user-friendly applications. We will cover the following topics:

  1. TextView
  2. EditText
  3. Button
  4. ImageView
  5. CheckBox
  6. RadioButton
  7. Switch
  8. ProgressBar

  1. TextView

TextView is a UI component that displays text to the user. It is one of the most commonly used components in Android applications.

Example:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"
    android:textSize="18sp"
    android:textColor="#000000"/>

Explanation:

  • android:id: Unique identifier for the TextView.
  • android:layout_width and android:layout_height: Define the width and height of the TextView.
  • android:text: The text to be displayed.
  • android:textSize: The size of the text.
  • android:textColor: The color of the text.

  1. EditText

EditText is a UI component that allows the user to enter and edit text.

Example:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter your name"
    android:inputType="text"/>

Explanation:

  • android:hint: A hint to be displayed when the EditText is empty.
  • android:inputType: The type of input (e.g., text, number, email).

  1. Button

Button is a UI component that triggers an action when clicked.

Example:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"/>

Explanation:

  • android:text: The text to be displayed on the button.

  1. ImageView

ImageView is a UI component that displays an image.

Example:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sample_image"/>

Explanation:

  • android:src: The source of the image to be displayed.

  1. CheckBox

CheckBox is a UI component that allows the user to select or deselect an option.

Example:

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I agree"/>

Explanation:

  • android:text: The text to be displayed next to the CheckBox.

  1. RadioButton

RadioButton is a UI component that allows the user to select one option from a set of mutually exclusive options.

Example:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1"/>
    
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2"/>
</RadioGroup>

Explanation:

  • RadioGroup: A container for RadioButtons to ensure only one can be selected at a time.

  1. Switch

Switch is a UI component that allows the user to toggle between two states (on/off).

Example:

<Switch
    android:id="@+id/switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enable Notifications"/>

Explanation:

  • android:text: The text to be displayed next to the Switch.

  1. ProgressBar

ProgressBar is a UI component that indicates the progress of an operation.

Example:

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"/>

Explanation:

  • android:indeterminate: If true, the ProgressBar shows an indeterminate progress.

Practical Exercise

Task:

Create a simple Android layout that includes a TextView, EditText, Button, and CheckBox. When the button is clicked, display the text entered in the EditText in the TextView and show whether the CheckBox is checked.

Solution:

  1. XML Layout (activity_main.xml):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter your name"/>
    
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I agree"/>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"/>
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
    </LinearLayout>
    
  2. Java Code (MainActivity.java):

    package com.example.basicuicomponents;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        private EditText editText;
        private CheckBox checkBox;
        private Button button;
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText = findViewById(R.id.editText);
            checkBox = findViewById(R.id.checkBox);
            button = findViewById(R.id.button);
            textView = findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = editText.getText().toString();
                    boolean isChecked = checkBox.isChecked();
                    textView.setText("Name: " + name + "\nAgreed: " + isChecked);
                }
            });
        }
    }
    

Explanation:

  • The EditText allows the user to enter their name.
  • The CheckBox allows the user to agree to something.
  • The Button triggers an action when clicked.
  • The TextView displays the entered name and the state of the CheckBox.

Conclusion

In this section, we covered the basic UI components in Android development, including TextView, EditText, Button, ImageView, CheckBox, RadioButton, Switch, and ProgressBar. We also provided a practical exercise to reinforce the concepts learned. Understanding these components is essential for creating interactive and user-friendly Android applications. In the next section, we will delve into Activities, which are the building blocks of an Android application.

© Copyright 2024. All rights reserved