Create an UI such that, one screen have list of all the types of cars. On selecting of any car name, next screen should show Car details like: name, launched date, company name

first create resource file, right click on values folder > new > Values Resource file and name it car_names.xml.

car_names.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="car_names">
<item>Maruti 800</item>
<item>Renault duster</item>
<item>Scorpio</item>
</string-array>
</resources>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/rlVar1"
>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Select your car"/>

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:entries="@array/car_names"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Go!"/>

</RelativeLayout>

MainActivity.java

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.graphics.Color;


public class MainActivity extends AppCompatActivity {
Spinner sp1;
int carType;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sp1 = findViewById(R.id.spinner1);
b1 = findViewById(R.id.button1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
i.putExtra("cartype", carType+"");
startActivity(i);
}
});

sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
carType = arg2;
}

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

}
});
}
}

activity_main2.xml

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

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:text="sample"/>

<TextView
android:layout_margin="20dp"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
</RelativeLayout>

MainActivity2.xml

package com.example.helloworld;

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

public class MainActivity2 extends AppCompatActivity {
TextView tvCarName, tvCarDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

tvCarName = findViewById(R.id.textView1);
tvCarDetails = findViewById(R.id.textView2);

int carType = Integer.parseInt(getIntent().getStringExtra("cartype"));

switch (carType){
case 0:
tvCarName.setText("Maruti 800");
tvCarDetails.setText("Car was launched on 14 December 1983 and manufactured by Maruti Suzuki in India");
break;
case 1:
tvCarName.setText("Renault duster");
tvCarDetails.setText("Car was launched in year 2012 and Renault India Private Limted is a wholly owned subsidiary of Renault S.A., France");
break;
case 2:
tvCarName.setText("Scorpio");
tvCarDetails.setText("Car was launched in June 2022 and manufactured by Mahindra");
break;
}
}
}

Output

select car and click on Go button



Yeah, I'm bad at selecting cars... oops