deejay
deejay

Reputation: 572

Set ImageView max height using code

I want to set Max Height to Imageview that it can have when the image is loaded. Initial I've set android:height="wrap_content" in XML to make room for other views. But after the image is loaded sometimes it covers the whole screen of mobile and i can't see other views and i want to limit the height to some certain value. Please!! any help is appreciated.. cheers

Upvotes: 0

Views: 912

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

A structure describing general information about a display, such as its size, density, and font scaling.

     DisplayMetrics metrics = getResources().getDisplayMetrics();

        int DeviceTotalWidth = metrics.widthPixels;
        int DeviceTotalHeight = metrics.heightPixels;


       /*
        *
        * Adding  Height Respect To Device
        * */

     ImageView  ImageViewObj=(ImageView)findViewById(R.id.ImageViewId);
     ImageViewObj.getLayoutParams().height= (int) (DeviceTotalHeight);

Upvotes: 2

Ashish Gaurav
Ashish Gaurav

Reputation: 265

Get the screen height, and use it to set some ratio like Sunny said.

/* get the screen height in pixels */
public static int getScreenHeight(WindowManager windowManager) {
    Display display = windowManager.getDefaultDisplay();
    Point point = new Point();
    display.getSize(point);
    return point.y;
}
/* take an imageview, and set its height to desired float ratio */
public static void setImageHeight(Activity activity, ImageView imageView, float ratio) {
    int h = getScreenHeight(activity.getWindowManager());
    imageView.getLayoutParams().height = (int)(h*ratio);
}

Then in onCreate(....), try doing

ImageView imageView = (ImageView) findViewById(R.id.whatever);
setImageHeight(imageView, 0.4f); /* 40% */

Upvotes: 0

Related Questions