Android set height and width of Custom view programmatically

AndroidViewHeightWidthParams

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

You can set height and width like this:

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

Solution 2 - Android

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 3 - Android

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 - Android

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 - Android

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
}

Solution 6 - Android

spin12.setLayoutParams(new LinearLayout.LayoutParams(200, 120));

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

Solution 7 - Android

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.

Solution 8 - Android

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondev_androidView Question on Stackoverflow
Solution 1 - AndroidEric NordvikView Answer on Stackoverflow
Solution 2 - AndroidDalmasView Answer on Stackoverflow
Solution 3 - AndroidRakshiView Answer on Stackoverflow
Solution 4 - AndroidgundraburView Answer on Stackoverflow
Solution 5 - AndroidAdib FaramarziView Answer on Stackoverflow
Solution 6 - AndroidUjjwalView Answer on Stackoverflow
Solution 7 - AndroidMorgan WildeView Answer on Stackoverflow
Solution 8 - AndroidJoseph Alex ChakolaView Answer on Stackoverflow