How to set ImageView width and height programmatically

In this android programming code example, we are going to illustrate how to set ImageView width and height programmatically.

The easiest way to set the width and height of an ImageView is in XML layout file.

But there are situation where we might need to change the width and height of already created ImageView or to dynamically create a new ImageView, then the solution below can become handy.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:src="@drawable/camera" />


import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    protected void onCreate(Bundle savedInstanceState) {

        ImageView imageView = findViewById(;
        imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

As can be seen in the code above, the width and height of the ImageView is set to MATCH_PARENT.

imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


Instead of using MATCH_PARENT, you can also use WRAP_CONTENT

imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Finally, if you want to specify a numeric number as the value of the width and height then you can achieve it as shown below.

imageView.setLayoutParams(new LinearLayout.LayoutParams(200, 200));

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.


Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.