skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. build.gradle(Module)
    android{
    ....
    buildFeatures{
    viewBinding true
       }
    }
    

    for me this works

    class MainActivity : AppCompatActivity() {
    private var _binding ActivityMainBinding?=null
    private val binding
    get() = _binding!!
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.apply{
        imageView.setImageResource(R.drawable.new_image)
       }
       
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search