DEV Community

abhi b
abhi b

Posted on

Attendence management table layout and spinner (Java & xml)

activity_main.xml

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:padding="10dp"
    android:background="#F5F5F5">

    <!-- Table Header -->
    <TableRow>
        <TextView android:text="Roll No" android:padding="8dp" android:textStyle="bold"/>
        <TextView android:text="Student Name" android:padding="8dp" android:textStyle="bold"/>
        <TextView android:text="Status" android:padding="8dp" android:textStyle="bold"/>
    </TableRow>

    <!-- First Student -->
    <TableRow>
        <TextView android:text="101" android:padding="8dp"/>
        <TextView android:text="John Doe" android:padding="8dp"/>
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TableRow>

    <!-- Second Student -->
    <TableRow>
        <TextView android:text="102" android:padding="8dp"/>
        <TextView android:text="Jane Smith" android:padding="8dp"/>
        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TableRow>

</TableLayout>

Enter fullscreen mode Exit fullscreen mode

Mainactivity.java

package com.example.tablelayoutspinner;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    String[] attendanceOptions = {"Present", "Absent", "Late"};

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

        // Initialize Spinners
        Spinner spinner1 = findViewById(R.id.spinner1);
        Spinner spinner2 = findViewById(R.id.spinner2);

        // Set Adapter for Spinners
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, attendanceOptions);
        spinner1.setAdapter(adapter);
        spinner2.setAdapter(adapter);

        // Handle selection events
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "John Doe: " + attendanceOptions[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Jane Smith: " + attendanceOptions[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }
}

Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs