Issue
This is my first time using the spinner so I was following tutorials. I was making spinner that will showcase the objects name.
Edit_Form Activity :
class Edit_Form : AppCompatActivity() {
private lateinit var mySpinner: Spinner
private lateinit var adapter: ArrayAdapter<Plant_Category>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit__form)
mySpinner = findViewById(R.id.spinner3)
val customObjects = getCustomObjects()
adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, customObjects)
mySpinner.adapter = adapter
}
private fun getCustomObjects(): ArrayList<Plant_Category> {
val customObjects = ArrayList<Plant_Category>()
customObjects.apply {
add(Plant_Category(1, "Strawberry","2 times a day","Water more in summer, Can cause irritation to cats"))
add(Plant_Category(2, "Aloe","2 times a week","Water more in summer, Is not safe to digest for pets"))
add(Plant_Category(3, "Money tree", "1-2 times a week","Needs medium to bright indirect light, Safe for pets"))
}
return customObjects
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.Edit_Form" >
<EditText
android:id="@+id/plant_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="135dp"
android:layout_marginEnd="101dp"
android:layout_marginRight="101dp"
android:layout_marginBottom="551dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/edit_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="454dp"
android:dropDownHeight="100dp"
android:dropDownWidth="match_parent"
android:prompt="@string/edit_type"
android:spinnerMode="dropdown"
android:theme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/plant_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
Data Class:
data class Plant_Category(
var id: Int =0,
var type:String ="" ,
var water_time: String ="",
var details: String = ""){
override fun toString(): String {
return type
}
}
The problem is that Spinner shows in the Design mode but it doesn't appear in emulator only the input field appears. I also changed the background color to see perhaps the spinner went to the corner or something but it's still doesn't show.
Perhaps there is a problem in measurements in Constraint Layout or how I implement it?
I will be grateful for your help.
Solution
You need to remove layout_margin
on both <EditText>
and <Spinner>
. Then the sprinner should appear in Constraint Layout
Answered By - MayWheather
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.