Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
You can add a spinner to your layout with the
Spinner
object. You should usually do so in your XML layout with a <Spinner>
element. For example:<androidx.cardview.widget.CardView
android:id="@+id/SelectVehicleCv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
app:cardBackgroundColor="@color/bgWhite"
app:cardCornerRadius="5dp"
app:cardElevation="8dp">
<Spinner
android:id="@+id/sp_Company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Cab"
android:textColor="#090909"
android:visibility="gone" />
</androidx.cardview.widget.CardView>
in Java class
Globle Diclare variable
int SelectCabId;
CabList = (Spinner) findViewById(R.id.sp_Company);
CabList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Snackbar.make(view, "Select " + id, Snackbar.LENGTH_LONG).show();
SelectCabId = world.get(position).getId();
// Toast.makeText(Home.this, ""+SelectCabId, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
All Vahicle Api using volley
public static final String GET_ALLVEHICLELIST = "https://www.surabhicab.com/backend/api/vehicle";
StringRequest stringRequest = new StringRequest(Request.Method.GET, Urls.GET_ALLVEHICLELIST,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObject1 = new JSONObject(jsonObject.getString("response"));
if (jsonObject1.getString("status").equals("success")) {
LogStatusChecker.setMinBookingDistance(prefs, new JSONObject(jsonObject1.getString("settings")).getString("min_booking_distance"));
JSONArray array = new JSONArray(jsonObject1.getString("result"));
for (int i = 0; i < array.length(); i++) {
JSONObject product = array.getJSONObject(i);
String name = product.getString("name");
String image = product.getString("image");
int id = product.getInt("id");
SpinnerProduct spinnerProduct = new SpinnerProduct(id, name, image);
world.add(spinnerProduct);
students.add(product.getString("name"));
}
}
CabList.setAdapter(new ArrayAdapter<String>(Home.this, android.R.layout.simple_list_item_activated_1, students));
CabList.setVisibility(View.VISIBLE);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Authorization", "Bearer " + PrefManager.getInstans(getApplicationContext()).getTOKEN());
return params;
}
@Override
protected Map<String, String> getParams() {
// Creating Map String Params.
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
// Creating RequestQueue.
// Adding the StringRequest object into requestQueue.
queue.add(stringRequest);