Add this dependencies in build.gradle
file
compile 'com.github.rey5137:material:1.2.2'
In Xml write this code.
<com.rey.material.widget.Spinner
android:id="@+id/spinner_label"
style="@style/LightSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minWidth="128dp"
android:padding="8dp"
app:spn_label="Spinner with arrow" />
In Java class write this code.
Spinner spn_label = (Spinner) findViewById(R.id.spinner_label);
String[] items = new String[20];
for (int i = 0; i < items.length; i++) {
items[i] = "Item " + String.valueOf(i + 1);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.row_span, items);
spn_label.setAdapter(adapter);
Create row_span.xml in your layout
folder.
<?xml version="1.0" encoding="utf-8"?>
<com.rey.material.widget.TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_spn_tv"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textDirection="locale"
style="@style/LightSpinner"/>
When you run the below output is generated.
for more detail visit this : https://github.com/rey5137/material
Without using Any Library :
Write this in your xml
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Create Array data in your resources file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
And use this in Java
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…