[SOLVED] Change an ImageView height in Android Studio


This Content is from Stack Overflow. Question asked by user1114881

I have an app I am building in Android Studio. I get a value from MQTT and want to use the value to change the height of an imageView.
My imageView is

val imagea = view.findViewById<ImageView>(R.id.imageView2)

and the value I get from my MQTT feed is

val msgl = "${message.toString()}"

How can I change the string to a int and then how do I change the height of the imageView2?


To convert a string to an integer, use parseInt. You’ll want to wrap it in a try/catch block to handle any issues with the input string.

try {
    imageHeight = Integer.parseInt(msgl);
} catch(NumberFormatException nfe) {
   [Error handling here]

To set the height of an ImageView programmatically:

imagea.getLayoutParams().height = imageHeight

Note: if you have already initiated the layout of the activity before you have the input string, you will need to re-inflate it with this:


This Question was asked in StackOverflow by user1114881 and Answered by shagberg It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?