• Latest Code...

    Programmatically set height and width of Relative and Linear Layout in android

    Are you faced to set height and width of Relative layout and Linear layout programmtically in android, here is the simple solution to set height and width of layouts.

    First, we have to get height and width of screen and then do these things
     // getting screen size of mobile or tablet devices
     Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            int width = size.x;
            int height = size.y;

          Display display = getWindowManager().getDefaultDisplay();
          int width = display.getWidth();
          int height = display.getHeight();


         DisplayMetrics metrics = new DisplayMetrics();


    Linear Layout:

        final LinearLayout linearLeft = (LinearLayout) findViewById(R.id.left);
        LayoutParams params = linearLeft .getLayoutParams();
    // Changes the height and width to the specified *pixels*
        params.width = screenWidth / 2;
    Relative Layout
        final RelativeLayout relavtiveLeft = (RelativeLayout) findViewById(R.id.right);
        LayoutParams homeLayoutsparams = relavtiveLeft .getLayoutParams();
        homeLayoutsparams.width = screenWidth / 2;

    Happy coding !!!



    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace