I drew a histogram without xml. I need to add a button under the graph. Can someone suggest a solution. Thanks at Advance !! The following is my program.
GraphViewDemo.java
public class GraphViewDemo extends Activity { public static String graphreturn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); float[] values = new float[] { 2.0f,3.0f, 5.0f, 1.0f , 6.0f , 4.0f ,7.0f }; String[] verlabels = new String[] { "7","6","5","4","3", "2", "1" }; String[] horlabels = new String[] {"jan20","jan21","jan22","jan23","jan24", "jan25","jan26"}; GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.BAR); setContentView(graphView);}}
GraphView.java
public class GraphView extends View{ public static boolean BAR = true; public static boolean LINE = false; private Paint paint; private float[] values; private String[] horlabels; private String[] verlabels; private String title; private boolean type; Context context; private Drawable mDrawable; private Runnable in ; public GraphView(Context context, float[] values, String title, String[] horlabels,String[] verlabels, boolean type) { super(context); if (values == null) values = new float[0]; else this.values = values; if (title == null) title = ""; else this.title = title; if (horlabels == null) this.horlabels = new String[0]; else this.horlabels = horlabels; if (verlabels == null) this.verlabels = new String[0]; else this.verlabels = verlabels; this.type = type; paint = new Paint(); } @Override protected void onDraw(final Canvas canvas) { context=getContext(); float border = 20; float horstart = border * 2; float height = getHeight()-50; float width = getWidth(); float max = getMax(); Log.w("max", ""+max); float min = getMin(); Log.w("min", ""+min); float diff = max - min; float graphheight = height - (2 * border); float graphwidth = width - (2 * border); paint.setTextAlign(Align.LEFT); int vers = verlabels.length - 1; for (int i = 0; i < verlabels.length; i++) { paint.setColor(Color.DKGRAY); float y = ((graphheight / vers) * i) + border; canvas.drawLine(horstart, y, width, y, paint); paint.setColor(Color.WHITE); canvas.drawText(verlabels[i], 0, y, paint); } int hors = horlabels.length; for (int i = 0; i < horlabels.length; i++) { paint.setColor(Color.DKGRAY); float x = ((graphwidth / hors) * i) + horstart; canvas.drawLine(x, height - border, x, border, paint); paint.setTextAlign(Align.CENTER); if (i==horlabels.length+5) paint.setTextAlign(Align.RIGHT); if (i==0) paint.setTextAlign(Align.LEFT); paint.setColor(Color.WHITE); canvas.drawText(horlabels[i], x, height - 4, paint); } paint.setTextAlign(Align.CENTER); canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint); int x = 0; int y = 0; Paint paint = new Paint(); Paint paint1 = new Paint(); paint.setStyle(Paint.Style.FILL); String str2rotate = "Rotated!";