sorry, major noob here. i have a settings fragment in my app and i want a dropdown menu to be in there. however, no matter what i try the dropdown menu has no items. my code is mostly based on this. here is my settings fragment:
class SettingsFragment: Fragment(),AdapterView.OnItemSelectedListener {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentSettingsBinding.inflate(inflater)
val spinner : Spinner = binding.translationSelector
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter.createFromResource(
requireActivity(),
R.array.planets_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// 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.adapter = adapter
}
return inflater.inflate(R.layout.fragment_settings, container, false)
}
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id: Long) {
val toastNotice = "you selected choice # " + position
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(this.context,toastNotice,duration)
toast.show()
}
override fun onNothingSelected(arg0: AdapterView<*>) {
}
you may notice i am using "requireActivity()" as a parameter to createFromResource instead of "this", since "this" gives a type mismatch...not sure if i should be doing this, but i saw it somewhere in stackoverflow. here is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.example.edtma.ScriptureModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SettingsFragment"
android:background="#E4E1E1">
<Spinner
android:id="@+id/translationSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/SpinnerLabel"
android:minHeight="48dp" />
</LinearLayout>
</layout>
when i run this, the settings fragment has a little arrow for a spinner, but it has no items. in case you are curious this is my strings.xml
<resources>
<string name="app_name">edtma</string>
<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>
any help would be much appreciated. i held out as long as possible trying other solutions posted on here but could not get anything to work. heres a screenshot of my fragment with empty dropdown
in case it helps anyone else, i finally found the issue. my return statement for onCreateView should be returning binding.root, ie change this:
return inflater.inflate(R.layout.fragment_settings, container, false)
to this:
return binding.root