Search code examples
xmlflutterkotlinimageviewresources

change ImageView src in Kotlin


I am trying to change my home widget ImageView in Kotlin and define my resources like this. but nothing show to me.By the way, I work with Flutter and not familiar with Kotlin so much

the xml file is :

    <ImageView
    android:id="@+id/imageView"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:scaleType="fitCenter"
    android:layout_marginLeft="10dp"
    android:src="@drawable/defaultcond"
    />

the WidgetProvider.kt is :

    val ivCond = widgetData.getString("ivCond","defaultcond")
    var ivId = "$ivCond"
    val drawableId: Int = context.getResources().getIdentifier(ivId, null, null)
    setImageViewResource(R.id.imageView,drawableId)

all images are inside the res/drawable folder.

and the code inside the future function is :

    await HomeWidget.saveWidgetData(
    'ivCond', code(widget.weather.currentWeather!.weathercode));

I search a lot and try to fix this but any changes I got an error the code that found for change the resource of ImageView not work fine for me.

I try to fix this by searching but all the codes that others send to fix problem not work for me and shows error for Kotlin coding.


Solution

  • Thanks all for your Time I figure out the problem and solve that with refractor this line

    val drawableId: Int = context.getResources().getIdentifier(ivId, "drawable", context.getPackageName())

    I guessed that the problem was from this part. ‌Best Regards