Setting up mipmap images on ImageButton from xml array in Kotlin?

510 views Asked by At

So I have an array defined in a xml values containing @mipmap icons. I am trying to get the icon int values from them and set them up on an ImageButton. But whenever I try to get the int value from them, I get 0 if I make it an 'integer-array' or Resources$NotFoundException if it is just an 'array'. Any solution?

mainActivity.kt

private fun setImage() {
    val iconArray = resources.getIntArray(R.array.transaction_icon_array)
    val imageView: ImageView = findViewById(R.id.image_icon)
    imageView.setImageResource(resources.getInteger(iconArray[0]))
    Log.d("ABID", imageView.tag.toString())
}

transaction_icon_array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="transaction_icon_array">
        <item>@mipmap/transaction_icon0</item>
        <item>@mipmap/transaction_icon1</item>
        <item>@mipmap/transaction_icon2</item>
        <item>@mipmap/transaction_icon3</item>
        <item>@mipmap/transaction_icon4</item>
        <item>@mipmap/transaction_icon5</item>
        <item>@mipmap/transaction_icon6</item>
        <item>@mipmap/transaction_icon7</item>
        <item>@mipmap/transaction_icon8</item>
        <item>@mipmap/transaction_icon9</item>
        <item>@mipmap/transaction_icon10</item>
        <item>@mipmap/transaction_icon11</item>
        <item>@mipmap/transaction_icon12</item>
        <item>@mipmap/transaction_icon13</item>
        <item>@mipmap/transaction_icon14</item>
        <item>@mipmap/transaction_icon15</item>
        <item>@mipmap/transaction_icon16</item>
        <item>@mipmap/transaction_icon17</item>
        <item>@mipmap/transaction_icon18</item>
        <item>@mipmap/transaction_icon19</item>
        <item>@mipmap/transaction_icon20</item>
        <item>@mipmap/transaction_icon21</item>
        <item>@mipmap/transaction_icon22</item>
        <item>@mipmap/transaction_icon23</item>
        <item>@mipmap/transaction_icon24</item>
        <item>@mipmap/transaction_icon25</item>
        <item>@mipmap/transaction_icon26</item>
        <item>@mipmap/transaction_icon27</item>
        <item>@mipmap/transaction_icon28</item>
        <item>@mipmap/transaction_icon29</item>
        <item>@mipmap/transaction_icon30</item>
        <item>@mipmap/transaction_icon31</item>
        <item>@mipmap/transaction_icon32</item>
        <item>@mipmap/transaction_icon33</item>
        <item>@mipmap/transaction_icon34</item>
        <item>@mipmap/transaction_icon35</item>
        <item>@mipmap/transaction_icon36</item>
        <item>@mipmap/transaction_icon37</item>
        <item>@mipmap/transaction_icon38</item>
        <item>@mipmap/transaction_icon39</item>
        <item>@mipmap/transaction_icon40</item>
        <item>@mipmap/transaction_icon41</item>
        <item>@mipmap/transaction_icon42</item>
        <item>@mipmap/transaction_icon43</item>
        <item>@mipmap/transaction_icon44</item>
        <item>@mipmap/transaction_icon45</item>
        <item>@mipmap/transaction_icon46</item>
        <item>@mipmap/transaction_icon47</item>
        <item>@mipmap/transaction_icon48</item>
        <item>@mipmap/transaction_icon49</item>
        <item>@mipmap/transaction_icon50</item>
    </integer-array>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="com.example.project_budget_planner.main.transaction.add_transaction.AddTransactionViewModel" />
    </data>
    <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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".main.transaction.add_transaction.AddTransactionActivity">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/AppTheme.Toolbar"
            android:onClick="@{() -> viewModel.navigateUp()}"
            app:navigationIcon="@drawable/back_icon"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <TextView
                android:id="@+id/header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/AppTheme.ToolbarHeader"
                android:text="@string/new_transaction_header"/>

        </androidx.appcompat.widget.Toolbar>

        <ImageButton
            android:id="@+id/image_icon"
            android:tag="18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1

There are 1 answers

0
Chirag Bhuva On BEST ANSWER

You case use TypedArray instated of using getIntArray

val typedArr = getResources().obtainTypedArray(R.array.transaction_icon_array);

//getResourceId(int index, int defValue)
typedArr.getResourceId(index, -1) // You can get resource ID by index

imageView.setImageResource(imgs.getResourceId(index, -1)); // set mipmap to your ImageView