Android set height and width of Custom view programmatically

dev_android picture dev_android · Feb 18, 2011 · Viewed 249.5k times · Source

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?

Answer

Eric Nordvik picture Eric Nordvik · Feb 18, 2011

You can set height and width like this:

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