Change an ImageView height in Android Studio

Viewed 11

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?

1 Answers

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:

imagea.requestLayout();
Related