Breaking

Friday 14 December 2018

Spinner Tutorial With Examples In Android Studio | Android Solution | Spinner Tutoriyal


Spinner Tutorial With Examples In Android Studio

In Android, Spinner provides a fast method to choose one incentive from a lot of esteems. Android spinners are only the drop down-list seen in other programming dialects. In a default express, a spinner demonstrates its presently chosen esteem. It provides a simple method to choose an incentive from a rundown of qualities.

In Simple Words we can state that a spinner resembles a combo box of AWT or swing where we can choose a specific thing from a rundown of things. Spinner is a sub class of AsbSpinner class.

Important Note: Spinner is related with Adapter see so to fill the information in spinner we have to utilize one of the Adapter class.

Here is the XML essential code for Spinner:

<Spinner
android:id="@+id/simpleSpinner "
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

Important Note: To fill the information in a spinner we have to execute an adapter class. A spinner is basically used to show just content field so we can execute Array Adapter for that. We can likewise utilize Base Adapter and other custom adapters to show a spinner with more alter list. Assume on the off chance that we have to show a textview and an imageview in spinner thing list at that point exhibit adapter isn't sufficient for that. Here we need to execute custom adapter in our class. Beneath picture of Spinner and Custom Spinner will make it all the more clear.


ArraryAdapter:

An adapter is an extension between UI segment and information source that encourages us to fill information in UI part. It holds the information and send the information to adapter see at that point view can takes the information from the adapter view and demonstrates the information on various perspectives like as rundown see, matrix see, spinner. At whatever point you have a rundown of single things which is upheld by an exhibit, you can utilize Array Adapter.

Here is code of ArrayAdapter in Android:

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

For more details read ArrayAdapter Tutorial as here we will use it in the below example to explain how Spinner is created in Android.

Table Of Contents [hide]
·         1 Example of Spinner In Android Studio:
·         2 Custom Spinner:

Example of Spinner In Android Studio:

Example 1: Below is the example in which we show a rundown of bank names in a spinner and at whatever point you select a thing the esteem will be shown utilizing toast on Mobile screen. The following is the last yield and code:
Step 1: Create a new project in Android Studio and name it SpinnerExample.

Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code. Here we will create a Spinner inside Relative Layout.

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />

</RelativeLayout>
Step 3: Now open application > java - > bundle - > MainActivity.java and include the accompanying code. Here we will utilize ArrayAdapter to fill the information in Spinner. Likewise we are utilizing Toast to show when the thing in Spinner is chosen.
package example.abhiandriod.spinnerexample;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
 
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
 
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
 
 
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}
 
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
 
}
}




No comments:

Post a Comment