Spinner Tutorial


Spinner widget is used to select item from drop down box.
Like Select Course ,Select State ,Select Country etc


package example.com.uidemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
 
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
public class SpinnerDemo extends AppCompatActivity {
String text = "";
TextView tv;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
setContentView(R.layout.activity_spinner_demo);
Spinner spn = findViewById(R.id.spn);
tv = findViewById(R.id.textdata);
 
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView adapterView, View view, int pos, long l) {
text=adapterView.getItemAtPosition(pos).toString();
}
@Override
public void onNothingSelected(AdapterView adapterView) {
}
});
}
 
public void submit(View v) {
if (!text.equals("")&&!text.equalsIgnoreCase("Select Version")) {
tv.setText(text);
} else {
tv.setText("");
Toast.makeText(this, "Please select item from Spinner", Toast.LENGTH_SHORT).show();
}
}
}

          activity_spinner_demo.xml
< ?xml version="1.0" encoding="utf-8"?> 
< LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
< Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:id="@+id/spn"
android:layout_margin="20dp"
android:entries="@array/names">

<TextView android:layout_width="match_parent"  
android:layout_height="wrap_content" 
android:padding="20dp" 
android:text="SpinnerOutput"  
android:layout_margin="20dp"  
android:id="@+id/textdata"/>
 
<Button android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="Submit" 
android:background="@color/colorAccent" 
android:textColor="#ffffff" 
android:onClick="submit" 
android:layout_margin="20dp" 
android:padding="20dp"/>
< /LinearLayout>
          colors.xml
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< color name="colorPrimary">#900C3F </color>
< color name="colorPrimaryDark">#581845 </color>
< color name="colorAccent">#C70039
 </color>
< /resources>

          strings.xml
< resources>
< string name="app_name">UiDemo</string>

< string-array name="names">
< item>Select Version
< item>Alpha
< item>Beta
< item>Cupcake
< item>Donut
< item>Eclair
< item>Froyo
< item>Gingerbread
< item>Honeycomb
< item>Ice Cream Sandwich
< item>Jelly Bean
< item>KitKat
< item>Lollipop
< item>Marshmallow
< item>Nougat
< item>Oreo
< item>Pie
< /string-array>
< /resources>
           styles.xml
 < resources>
    < !-- Base application theme. -->
    < style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        < !-- Customize your theme here. -->
        < item name="colorPrimary">@color/colorPrimary
        < item name="colorPrimaryDark">@color/colorPrimaryDark
        < item name="colorAccent">@color/colorAccent
    < /style>
< /resources>

Screen Shot
Third Page
First Page
Second Page
Fourth Page



If you want to change background color and Textcolor then we have to make it custom view.




package example.com.uidemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.widget.AdapterView; import android.view.View;


import android.widget.Spinner; import android.widget.ArrayAdapter;


public class SpinnerDemo extends AppCompatActivity { import android.widget.TextView; import android.widget.Toast;


@Override protected void onCreate(Bundle savedInstanceState) { String text = ""; TextView tv; super.onCreate(savedInstanceState);


String names[]=getResources().getStringArray(R.array.names); setContentView(R.layout.activity_spinner_demo); Spinner spn = findViewById(R.id.spn);


tv = findViewById(R.id.textdata);
spn.setAdapter(new ArrayAdapter(this,R.layout.item,names));
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override public void onItemSelected(AdapterView adapterView, View view, int pos, long l) {
text=adapterView.getItemAtPosition(pos).toString();
} @Override public void onNothingSelected(AdapterView adapterView) { }

}); } public void submit(View v) {


if (!text.equals("")&&!text.equalsIgnoreCase("Select Version")) { tv.setText(text); } else { tv.setText("");

}} Toast.makeText(this, "Please select item from Spinner", Toast.LENGTH_SHORT).show(); }




< ?xml version="1.0" encoding="utf-8"?><
LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> < Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:id="@+id/spn" android:layout_margin="20dp"></Spinner> < TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:text="Spinner Output" android:layout_margin="20dp" android:id="@+id/textdata"/> < Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Submit" android:background="@color/colorAccent" android:textColor="#ffffff" android:onClick="submit" android:layout_margin="20dp" android:padding="20dp"/> < /LinearLayout>




xml version="1.0" encoding="utf-8"?><TextView android:background="@color/colorAccent" android:textColor="#ffffff" android:padding="20dp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" />

 







No comments: