I have created a custom calendar viewgroup that encompasses, among other elements, a spinner to allow the user to select the year. This custom view will be used in multiple apps, so it makes sense to create something reusable like an AAR. I have some specific styling I need to do so the view looks the same no matter where I deploy it.
My custom viewgroup finds and renders the main layout of the viewgroup. But, when the user taps the year spinner and wants to change the year, the adapter can't find the text ID of the layout for the dropdown. I've checked the merged manifest in my APK and can see both the layout and the text id from my AAR, but it crashes upon tap with a error of :
java.lang.RuntimeException: Failed to find view with ID us.martypants.mycustomviewgroup:id/current_year in item layout
Interestly enough,if instead of a custom layout and textId I use android's layout (i.e. android.R.layout.simple_spiner_item and android.R.id.text1) the APK can find the resources, show the list of selections and set the result - although the styling I need is absent.
package com.algtskr.algtskrcommon
class DropdownAgeSelectView (context: Context, attrs: AttributeSet): RelativeLayout(context, attrs),
AdapterView.OnItemSelectedListener {
private var mCounterColor = 0
private var mAge = 0
init {
LayoutInflater.from(context)
.inflate(R.layout.dropdown_ageselect_layout, this, true)
attrs.let {
val typedArray = context.obtainStyledAttributes(it,
R.styleable.DropdownAgeSelectView, 0, 0)
mCounterColor = typedArray.getColor(R.styleable.DropdownAgeSelectView_counter_color, 0)
mAge = typedArray.getInteger(R.styleable.DropdownAgeSelectView_initial_value, 0)
typedArray.recycle()
}
val adapter = ArrayAdapter(context,R.layout.year_layout_text,R.id.current_year, childAgeList)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
spinner.onItemSelectedListener = this
initLayout()
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/current_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
tools:text="2019"
android:textColor="@color/blue_passenger"
android:textSize="24sp"/>
val adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, android.R.id.text1, childAgeList)
val adapter = ArrayAdapter(context,R.layout.year_layout_text,R.id.current_year, childAgeList)
Any idea why the AAR correctly finds and renders the overall viewgroup layout but individual pieces within the layout are not? Why are all the layouts and resources not getting found in the APK, even though the AAR shows show them in the res/ folder and in my APK the build/intermediates/res/ shows them as well.
The problem is here:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
When you pass R.id.current_year
to the ArrayAdapter
constructor, that will be used for both the collapsed view and for each row in the popup window (the "dropdown" views). Since your dropdown layout doesn't include a view with id R.id.current_year
, you crash.
Change this setDropDownViewResource()
call to use your own custom layout including an R.id.current_year
TextView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With