DEV Community

abhi b
abhi b

Posted on

Attendence management grid layout and spinner (Java & xml)

Activity_main.xml

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:padding="10dp"
    android:background="#F5F5F5"
    android:alignmentMode="alignMargins"
    android:rowOrderPreserved="false">

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

    <!-- First Student -->
    <TextView android:text="101" android:padding="8dp"/>
    <EditText
        android:id="@+id/editTextName1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Name"
        android:padding="8dp"/>
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <!-- Second Student -->
    <TextView android:text="102" android:padding="8dp"/>
    <EditText
        android:id="@+id/editTextName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Name"
        android:padding="8dp"/>
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</GridLayout>

Enter fullscreen mode Exit fullscreen mode

Mainactivity.java

package com.example.gridlayoutspinner;

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

public class MainActivity extends AppCompatActivity {

    String[] attendanceOptions = {"Present", "Absent", "Late"};
    EditText editTextName1, editTextName2;
    Spinner spinner1, spinner2;

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

        // Initialize EditTexts
        editTextName1 = findViewById(R.id.editTextName1);
        editTextName2 = findViewById(R.id.editTextName2);

        // Initialize Spinners
        spinner1 = findViewById(R.id.spinner1);
        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) {
                String studentName = editTextName1.getText().toString();
                Toast.makeText(MainActivity.this, studentName + " is " + 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) {
                String studentName = editTextName2.getText().toString();
                Toast.makeText(MainActivity.this, studentName + " is " + attendanceOptions[position], Toast.LENGTH_SHORT).show();
            }

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

Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

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

šŸ‘‹ Kindness is contagious

Please leave a ā¤ļø or a friendly comment on this post if you found it helpful!

Okay