Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Setting Dimensions at Runtime

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 336
    Comment on it

    Set View Dimensions at runtime

    Welcome to another tutorial of setting view dimensions at runtime.

    Following is a simple way to set your view dimensions at runtime (there is another way also for doing same but will discuss in another blog).

    • First of all get the height and width of the screen once your activity starts (oncreate)
     DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;

    In this way you will get the height and width of the screen at runtime.

    Now calculate the percentage height of your view corresponding to the height and width of the screen.

     

     

    int image_one_ht = (int)(height * 0.1);
            int image_two_ht = (int)(height * 0.3);
            int image_three_ht = (int)(height * 0.2);
            int card_height = (int)(height * .95);
            int card_width = (int)(width * .95);
            int fields_ht = (int)(height * 0.5);
    

     

    Now you have the percentage height and width of the view.

    Set the height and width of the view

      img_one.getLayoutParams().height = image_one_ht;
            img_one.getLayoutParams().width = image_one_ht;
    
            img_two.getLayoutParams().height = image_two_ht;
            img_two.getLayoutParams().width = image_two_ht;
    
            img_three.getLayoutParams().height = image_three_ht;
            img_three.getLayoutParams().width = image_three_ht;
    
            cardView.getLayoutParams().height = card_height;
            cardView.getLayoutParams().width = card_width;
    
            layout_parent.getLayoutParams().height = fields_ht;

    Last but not the least override the onConfigurationChange method and call this method from there also.

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            setDimensions();
        }

    Thats it now you don't have to write multiple dimension files for the height and width of the views it will be automatically set at runtime, you may also can change the behaviour of the view, like where it should lie on configuration change by adding rules to layout params.

    A full working link of the demo is available here

    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: