Android tutorial for beginner part 14, Spinner

  • 23 April 20
  • Posted By : Admin
  • 79 Hits
  • 10 Comment

See video for total guidline

}

Java code

Adnroid Spnner

package com.salam.androidspinner; import androidx.appcompat.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 { public Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = findViewById(R.id.spinenr); ArrayAdapter<CharSequence> mAdapter = ArrayAdapter.createFromResource(MainActivity.this, R.array.planets_array,android.R.layout.simple_spinner_item); mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(mAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, " "+i+" "+l, Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } }

Create xml file

Android spinner

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Planets list" /> <Spinner android:layout_width="80dp" android:layout_height="wrap_content" android:id="@+id/spinenr"/> </LinearLayout>