DEV Community

Varun Vikyat b
Varun Vikyat b

Posted on

Vehicle Parking(Spinner)(Java & XML)

Main (XML)

<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center"
>

<Spinner
    android:id="@+id/vehicleTypeSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<EditText
    android:id="@+id/vehicleNumberEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Vehicle Number"
    />

<EditText
    android:id="@+id/rcNumberEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter RC number"
    />

<Button
    android:id="@+id/SubmitButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    />
Enter fullscreen mode Exit fullscreen mode

Main.java

package com.example.vehicleparking;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

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

public class MainActivity extends AppCompatActivity {

private EditText vehicleNumberEditText, rcnumberEditText;

private Spinner vehicleTypeSpinner;

private Button SubmitButton;

private String selectedtype;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);

    vehicleNumberEditText = findViewById(R.id.vehicleNumberEditText);
    rcnumberEditText = findViewById(R.id.rcNumberEditText);
    SubmitButton = findViewById(R.id.SubmitButton);
    vehicleTypeSpinner = findViewById(R.id.vehicleTypeSpinner);

    String[] vehicletypes = {"Car","Bike","Truck","Van"};
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_spinner_dropdown_item,vehicletypes);
    vehicleTypeSpinner.setAdapter(adapter);

    vehicleTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedtype = vehicletypes[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            selectedtype = "Car";
        }
    });

    SubmitButton.setOnClickListener(v -> {
        String VehicleNumber = vehicleNumberEditText.getText().toString();
        String RCNumber = rcnumberEditText.getText().toString();

        Intent intent = new Intent(MainActivity.this, SummaryActivity.class);
        intent.putExtra("vehicleType",selectedtype);
        intent.putExtra("VehicleNumber",VehicleNumber);
        intent.putExtra("RCNumber",RCNumber);
        startActivity(intent);
    });

}
Enter fullscreen mode Exit fullscreen mode

}

Summary (XML)

<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SummaryActivity"
android:orientation="vertical"
android:gravity="center"
>

<TextView
    android:id="@+id/TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<Button
    android:id="@+id/ConfrimBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Confirm"
    />
<Button
    android:id="@+id/EditBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Edit"
    />
Enter fullscreen mode Exit fullscreen mode

Summary.java

package com.example.vehicleparking;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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 org.w3c.dom.Text;

import java.util.Random;

public class SummaryActivity extends AppCompatActivity {

private TextView Textview;
private Button ConfrimBtn, EditBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);

    Textview = findViewById(R.id.TextView);
    ConfrimBtn = findViewById(R.id.ConfrimBtn);
    EditBtn = findViewById(R.id.EditBtn);

    Intent intent = getIntent();
    String VehicleNumber = intent.getStringExtra("VehicleNumber");
    String RCNumber = intent.getStringExtra("RCNumber");
    String vehicleType = intent.getStringExtra("vehicleType");

    String details= "Vehicle Type: "+vehicleType+"\nVehicle Number: "+VehicleNumber+"\nRC Number: "+RCNumber;
    Textview.setText(details);

    ConfrimBtn.setOnClickListener(v ->{
        int SerialNumber = new Random().nextInt(999999);
        Toast.makeText(this,"Your Token Number is: "+SerialNumber,Toast.LENGTH_SHORT).show();
    });

    EditBtn.setOnClickListener(v -> finish());
}
Enter fullscreen mode Exit fullscreen mode

}

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay