/*
========================================================================================
Filename: 16_StudentActivity.txt
Program 16: Create an app that launches a new activity on a button click to display student details with checkbox and radio buttons.
Files Included: MainActivity.java, SecondaryActivity.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   In activity_main.xml:
     - 2 EditText (TextBox): name (Name), phone (Phone)
     - 1 RadioGroup (radioGroupDept) with 3 RadioButtons: rbCS, rbCivil, rbMechanic
     - 1 RadioGroup (radioGroupGender) with 2 RadioButtons: rbMale, rbFemale
     - 4 CheckBox: cbAI, cbCloud, cbShip, cbAuto
     - 1 Button: Text: 'Insert', onClick: 'insert'
     - 4 TextView labels
   In activity_secondary.xml:
     - 6 TextView: tvName, tvPhone, tvDepartment, tvGender, tvCourses, heading

2. Layout Configuration:
   - LinearLayout / ScrollView

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Default theme.

5. AndroidManifest.xml:
   Add SecondaryActivity registration:
   <activity android:name=".SecondaryActivity" android:exported="false" />

6. Additional Files & Resources Required:
   - Create SecondaryActivity.java to retrieve extras via getIntent().
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.studentactivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    EditText etName,etPhone;
    RadioGroup rgDept,rgGender;
    CheckBox cbInputAI,cbInputCloud,cbInputShip,cbInputAutocad;
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);
        etName=findViewById(R.id.name);
        etPhone=findViewById(R.id.phone);
        rgDept=findViewById(R.id.radioGroupDept);
        rgGender=findViewById(R.id.radioGroupGender);
        cbInputAI=findViewById(R.id.cbAI);
        cbInputCloud=findViewById(R.id.cbCloud);
        cbInputShip=findViewById(R.id.cbShip);
        cbInputAutocad=findViewById(R.id.cbAuto);
    }

    public void insert(View v) {
        String name=etName.getText().toString().trim(),phone=etPhone.getText().toString().trim();
        if(name.isEmpty()) {
            etName.setError("Enter your name");
            return;
        }

        if(phone.length()!=10) {
            etPhone.setError("Enter a valid 10-digit phone number");
            return;
        }

        int d=rgDept.getCheckedRadioButtonId(),g=rgGender.getCheckedRadioButtonId();
        if(d==-1) {
            Toast.makeText(this,"Please select a department",Toast.LENGTH_SHORT).show();
            return;
        }

        if(g==-1) {
            Toast.makeText(this,"Please select a gender",Toast.LENGTH_SHORT).show();
            return;
        }

        String dept=((RadioButton)findViewById(d)).getText().toString(),gender=((RadioButton)findViewById(g)).getText().toString();
        StringBuilder courses=new StringBuilder();
        if(cbInputAI.isChecked())courses.append(cbInputAI.getText()).append(", ");
        if(cbInputCloud.isChecked())courses.append(cbInputCloud.getText()).append(", ");
        if(cbInputShip.isChecked())courses.append(cbInputShip.getText()).append(", ");
        if(cbInputAutocad.isChecked())courses.append(cbInputAutocad.getText()).append(", ");
        if(courses.length()==0)courses.append("None");
        else courses.setLength(courses.length()-2);
        Intent i=new Intent(this,SecondaryActivity.class);
        i.putExtra("name",name);
        i.putExtra("phone",phone);
        i.putExtra("department",dept);
        i.putExtra("gender",gender);
        i.putExtra("courses",courses.toString());
        startActivity(i);
    }
}

// ======================================================================================
// File: SecondaryActivity.java
// ======================================================================================
package com.example.studentactivity;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class SecondaryActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_secondary);
        String name=getIntent().getStringExtra("name"),phone=getIntent().getStringExtra("phone"),dept=getIntent().getStringExtra("department"),gender=getIntent().getStringExtra("gender"),courses=getIntent().getStringExtra("courses");
        ((TextView)findViewById(R.id.tvName)).setText("Name : "+name);
        ((TextView)findViewById(R.id.tvPhone)).setText("Phone : "+phone);
        ((TextView)findViewById(R.id.tvDepartment)).setText("Department : "+dept);
        ((TextView)findViewById(R.id.tvGender)).setText("Gender : "+gender);
        ((TextView)findViewById(R.id.tvCourses)).setText("Preferred Courses : "+courses);
    }
}
