Demonstration of using widgets (ToggleButton, ImageButton, ListView, Spinner, Checkbox, RadioButton, RadioGroup, and ImageView)



Code Display with Syntax Highlighting

Android Widgets Example

Java Code


package com.vipul.p6;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.os.Bundle;
import android.view.View;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;

public class WidgetsActivity extends AppCompatActivity {

    ToggleButton toggleButton;
    ImageButton imageButton;
    ImageView imageView;
    Spinner spinner;
    ListView listView;
    CheckBox checkBox;
    RadioGroup radioGroup;
    RadioButton selectedRadioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_widgets);

        // Initialize Widgets
        toggleButton = findViewById(R.id.toggleButton);
        imageButton = findViewById(R.id.imageButton);
        imageView = findViewById(R.id.imageView);
        spinner = findViewById(R.id.spinner);
        listView = findViewById(R.id.listView);
        checkBox = findViewById(R.id.checkBox);
        radioGroup = findViewById(R.id.radioGroup);

        // Setup Spinner (Country Selection)
        String[] countries = {"Select Country", "USA", "UK", "India", "Canada"};
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
        spinner.setAdapter(spinnerAdapter);

        // Setup ListView (Skills)
        String[] skills = {"Java", "Android", "Kotlin", "Python"};
        ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, skills);
        listView.setAdapter(listAdapter);

        // ToggleButton to enable/disable form
        toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
            spinner.setEnabled(isChecked);
            listView.setEnabled(isChecked);
            radioGroup.setEnabled(isChecked);
            checkBox.setEnabled(isChecked);
        });

        // ImageButton to change profile picture (simulated)
        imageButton.setOnClickListener(v -> {
            imageView.setImageResource(R.drawable.img); // Change image
            Toast.makeText(this, "Profile Picture Updated", Toast.LENGTH_SHORT).show();
        });

        // RadioGroup to get selected gender
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            selectedRadioButton = findViewById(checkedId);
            Toast.makeText(this, "Selected: " + selectedRadioButton.getText(), Toast.LENGTH_SHORT).show();
        });

        // CheckBox to accept Terms & Conditions
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked)
                Toast.makeText(this, "Accepted Terms & Conditions", Toast.LENGTH_SHORT).show();
        });
    }
}

XML Code


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#21AABB"
        android:gravity="center"
        android:orientation="vertical">

        <!-- Profile Picture Section -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/card_background"
            android:elevation="4dp"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="8dp"
                android:text="Profile Picture"
                android:textSize="18sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:background="@drawable/circle_background"
                android:padding="16dp"
                android:src="@android:drawable/ic_menu_gallery" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_button"
                android:padding="10dp"
                android:src="@android:drawable/ic_menu_camera" />
        </LinearLayout>

        <!-- ToggleButton -->
        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/rounded_button"
            android:padding="10dp"
            android:textColor="@color/white"
            android:textOff="Enable Form"
            android:textOn="Disable Form" />

        <!-- Gender Selection -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Select Gender:"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"
                android:textColor="@color/white" />

            <RadioButton
                android:id="@+id/radioFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                android:textColor="@color/white" />
        </RadioGroup>

        <!-- Spinner -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Select Country:"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/spinner_background"
            android:padding="12dp" />

        <!-- ListView -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Skills:"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/listview_background"
            android:padding="8dp" />

        <!-- Checkbox -->
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="I agree to the Terms & Conditions"
            android:textColor="@color/white"
            android:textSize="14sp" />

    </LinearLayout>
</ScrollView>





Post a Comment

0 Comments