158

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?

Akram
  • 7,510
  • 8
  • 44
  • 71
dev_android
  • 8,348
  • 22
  • 88
  • 145

8 Answers8

229

You can set height and width like this:

myGraphView.setLayoutParams(new LayoutParams(width, height));
Eric Nordvik
  • 14,410
  • 8
  • 41
  • 50
  • 80
    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. – yincrash Feb 17 '12 at 20:30
  • 42
    This also does work and ensures you use the correct LayoutParams class: simply do `myGraphView.getLayoutParams().height = 100;`. – sulai Oct 23 '12 at 15:39
  • 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. – SK9 Dec 22 '12 at 11:45
  • 24
    If you do `this.getLayoutParams().height = 100`, make sure to follow it by `this.setLayoutParams(this.getLayoutParams())`, otherwise it will not do anything (useful). – Achal Dave May 02 '13 at 08:01
  • 4
    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. – gardarh Jun 07 '13 at 13:04
  • 1
    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 – ericn Aug 26 '13 at 02:00
  • 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. – rml Dec 13 '13 at 15:25
173

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.

Dalmas
  • 26,011
  • 9
  • 66
  • 78
  • 21
    Would be helpful to know which LayoutParams you are importing there. – user3690202 Nov 19 '16 at 05:35
  • 1
    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)` – Vic Feb 10 '19 at 14:51
  • What if the `graphView` inside a `ScrollView` in Kotlin? – Jevon Jan 18 '21 at 15:50
90

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);
tvkanters
  • 3,501
  • 4
  • 28
  • 45
Rakshi
  • 6,698
  • 3
  • 24
  • 46
  • 3
    you do not need to set layoutparam again. It gets reflected automatically. This is the advantage of object oriented approach. – Dipendra Jul 30 '15 at 11:19
  • 9
    @Dipendra the call to `setLayoutParams()` is required because it invokes other methods such as `resolveLayoutParams()` and `requestLayout()` – William Sep 25 '16 at 01:39
  • giving exception as in onCreate getLayoutParams() is null value.So params.height cause exception – Sibasish Jun 15 '17 at 06:40
  • You should refer the view for which you have to set the height. Check if you are pointing to the same view – Rakshi Jun 18 '17 at 15:12
  • 1
    Getting the `layoutParams` at a view's initiation will be a NPE, please explain at what point to you run this? – John Sardinha Aug 08 '19 at 09:40
23

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

someView.layoutParams.width = 100
someView.layoutParams.height = 200
gundrabur
  • 703
  • 8
  • 12
7

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
}
Adib Faramarzi
  • 3,242
  • 3
  • 26
  • 39
4
spin12.setLayoutParams(new LinearLayout.LayoutParams(200, 120));

spin12 is your spinner and 200,120 is width and height for your spinner.

ahmed hamdy
  • 4,816
  • 1
  • 45
  • 53
Ujjwal
  • 336
  • 4
  • 11
3

This is a Kotlin based version, assuming that the parent view is an instance of LinearLayout.

someView.layoutParams = LinearLayout.LayoutParams(100, 200)

This allows to set the width and height (100 and 200) in a single line.

Morgan Wilde
  • 16,113
  • 9
  • 50
  • 97
0

Adding on to the solution by @MorganWilde. You can use the following code if you want to use WRAP_CONTENT/MATCH_PARENT.

someView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)