Android set height and width of Custom view programmatically

283,364

Solution 1

If you know the exact size of the view, just use setLayoutParams():

graphView.setLayoutParams(new LayoutParams(width, height));

Or in Kotlin:

graphView.layoutParams = LayoutParams(width, height)

However, if you need a more flexible approach you can override onMeasure() to measure the view more precisely depending on the space available and layout constraints (wrap_content, match_parent, or a fixed size). You can find more details about onMeasure() in the android docs.

Solution 2

You can set height and width like this:

myGraphView.setLayoutParams(new LayoutParams(width, height));

Solution 3

you can set the height and width of a view in a relative layout like this

ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = 130;
view.setLayoutParams(params);

Solution 4

On Kotlin you can set width and height of any view directly using their virtual properties:

someView.layoutParams.width = 100
someView.layoutParams.height = 200

Solution 5

If you're using Kotlin, you can also use the following code which applies your given lambda on the current layout params:

someView.updateLayoutParams {
    height = 200
}
Share:
283,364

Related videos on Youtube

dev_android
Author by

dev_android

Updated on March 15, 2022

Comments

  • dev_android
    dev_android over 2 years

    I have created a custom view named Graphview . Here is the structure for the GraphView class.

    public class GraphView extends View {
    
        public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
            super(context);
                    ........
       }
    
       ..................
       .................
    }
    

    I have added the view in a tablerow using addview(). It is working fine. Now I want to set height and width for the GraphView. How to do that?

  • yincrash
    yincrash over 12 years
    You should make sure the LayoutParam class you use is from the correct parent Layout class. For example, if your GraphView is contained in a LinearLayout, you need to use the LinearLayout.LayoutParams class.
  • sulai
    sulai over 11 years
    This also does work and ensures you use the correct LayoutParams class: simply do myGraphView.getLayoutParams().height = 100;.
  • Ken
    Ken over 11 years
    Generally making absolute statements with hardcoded meaningless numbers leads to trouble. Better to make relative statements as much as possible, e.g. with fill_parent etc.
  • Achal Dave
    Achal Dave about 11 years
    If you do this.getLayoutParams().height = 100, make sure to follow it by this.setLayoutParams(this.getLayoutParams()), otherwise it will not do anything (useful).
  • gardarh
    gardarh about 11 years
    I think it's odd that this answer is getting more points. While it's easier it's much less modular; if you'd like to use your view in a different place you'd need to hardcode those width/height values there as well. Be careful about using this.
  • ericn
    ericn almost 11 years
    Thanks @yincrash for pointing that out. It seems odd that I need to use the parent view' class (RelativeLayout in my case) instead of the parent class of my custom view (SurfaceView in my case) though
  • rml
    rml over 10 years
    We do need to calculate the size of a view programmatically when the default params do not fit the need of program. That why we need this answer. Thank you Eric Nordvik, you helped a lot.
  • Dipendra
    Dipendra almost 9 years
    you do not need to set layoutparam again. It gets reflected automatically. This is the advantage of object oriented approach.
  • William
    William over 7 years
    @Dipendra the call to setLayoutParams() is required because it invokes other methods such as resolveLayoutParams() and requestLayout()
  • user3690202
    user3690202 over 7 years
    Would be helpful to know which LayoutParams you are importing there.
  • Sibasish
    Sibasish about 7 years
    giving exception as in onCreate getLayoutParams() is null value.So params.height cause exception
  • Rakshi
    Rakshi about 7 years
    You should refer the view for which you have to set the height. Check if you are pointing to the same view
  • Vic
    Vic over 5 years
    The LayoutParams should be from the layout where the graphView is placed. For example if in the XML layout file graphView is placed inside RelativeLayout, then you should use new RelativeLayout.LayoutParams(width, height)
  • John Sardinha
    John Sardinha almost 5 years
    Getting the layoutParams at a view's initiation will be a NPE, please explain at what point to you run this?
  • Jevon
    Jevon over 3 years
    What if the graphView inside a ScrollView in Kotlin?