Thursday, 5 September 2013

How to use Android Spinners! (Updated with androidx package)

Spinners are just like radio buttons, but they have some what different look. The user can select only one option among various options.

Lets suppose that you are developing an app for a mobile shop whose objective is to ask user to select their price range. The price range may start from 100$ and ends on 1000$. The user will select his desired amount and the mobile phones near to the selected range will be displayed.



First we write the prices in our string.xml file which is located inside res/values
----------------------------------------------------
<resources>
<string name="app_name">WhatAnAndroid</string>

<!-- Spinner Example -->
<string name="price_string">Price Range</string>
<string-array name="price_string_array">
<item>\u0020 Select a price range</item>
<item>\u0020 100$</item>
<item>\u0020 200$</item>
<item>\u0020 500$</item>
<item>\u0020 700$</item>
<item>\u0020 1000$</item>
</string-array>
<!-- /Spinner Example -->

</resources>
----------------------------------------------------

The following will go in the spinner_activity.xml.
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#CE93D8">

<Spinner
android:id="@+id/sp_priceSP"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
android:prompt="@string/price_string"
android:background="@color/colorPrimaryDark"/>

<TextView
android:id="@+id/sp_resultTV"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:textSize="25sp"
android:text="Select Price Range"
android:textColor="#ffffff"/>
</LinearLayout>
-----------------------------------------------------
Now get into the Java code...
------------------------------------------------------------
package com.app.yourappname;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SpinnerActivity extends AppCompatActivity {

private TextView resultTV;

@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.spinner_activity);
Spinner priceSpinner = findViewById(R.id.sp_priceSP);
setSpinners(priceSpinner, R.array.price_string_array);
resultTV = findViewById(R.id.sp_resultTV);
priceSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

private void setSpinners(Spinner spinner, int spinnerRes) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, spinnerRes, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

class MyOnItemSelectedListener implements OnItemSelectedListener {

private String selectedPrice;

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.sp_priceSP) {
switch(pos) {

case 0: selectedPrice = "Select a price range"; break;
case 1: selectedPrice = "100$"; break;
case 2: selectedPrice = "200$"; break;
case 3: selectedPrice = "500$"; break;
case 4: selectedPrice = "700$"; break;
case 5: selectedPrice = "1000$"; break;
}
}
resultTV.setText(selectedPrice);
}

public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
-------------------------------------------------------------------------

------------------------------ Declare the Activity name in the Manifest file -------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SpinnerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
--------------------------------------------------------------------------


No comments:

Post a Comment