Android set height and width of Custom view programmatically

android, height, parameters, view, width

Solution

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.

Problem

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?

Original source

Related problems