Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:52:47+00:00 2026-05-30T15:52:47+00:00

I have drawn a bar chart without xml.I have to add a button below

  • 0

I have drawn a bar chart without xml.I have to add a button below the graph.Can anybody suggest solution.Thanks in Advance!!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!";
    // draw bounding rect before rotating text
    Rect rect = new Rect();
    paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
    canvas.translate(x, y);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(1);
    
    paint.setStyle(Paint.Style.STROKE);

    paint1.setColor(Color.RED);
    canvas.drawText("!Rotated", 0, 0, paint1);
    
    
    mDrawable = context.getResources().getDrawable(R.drawable.previousi);
    mDrawable.setBounds(getWidth()/2-40,getHeight()-40, getWidth()/2+30, getHeight()-20);
    mDrawable.draw(canvas);
    
    mDrawable.scheduleSelf(in, 0);
    
    //....................
    in = new Runnable() {
        public void run() {
        
                try {
                    
                    
                    //canvas.restore();
                     mDrawable.setBounds(getWidth()/2,getHeight(), getWidth(), getHeight()-20);
                        mDrawable.draw(canvas);
                
       
                                                

                } 
                 catch (Exception e) { 
                        e.printStackTrace();
                    }
                
                

            } 
        
    };

    Thread thread = new Thread(null, in, "graphview");
    thread.start();
 

    
    
    
    
    if (max != min) {
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        
        if (type == BAR) {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);                     
            
            }
        } else {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            float halfcol = colwidth / 2;
            float lasth = 0;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                lasth = h;
            }
        }
    }
}



private float getMax() {
    float largest = Integer.MIN_VALUE;
    for (int i = 0; i < values.length; i++)
        if (values[i] > largest)
            largest = values[i];
    return largest;
}

private float getMin() {
    float smallest = Integer.MAX_VALUE;
    for (int i = 0; i < values.length; i++)
        if (values[i] < smallest)
            smallest = values[i];
    return smallest;
}


}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:52:49+00:00Added an answer on May 30, 2026 at 3:52 pm

    Try this:

     btn = new Button(this);
     btn.setText("Hello Button");
     RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONT ENT,LayoutParams.WRAP_CONTENT);
     paramsd.height = 60;
     paramsd.width = 60;
     btn.setLayoutParams(paramsd);
     addContentView(btn,paramsd);
    

    Edit:

    import android.widget.Button;
    

    Add button

        Button b = new Button(this);
        b.setText("Button added dynamically!");
        b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        b.setId(MY_BUTTON);
        b.setOnClickListener(this);
        ll.addView(b);
    

    Edit2:
    You could create LinearLayout linearLayout = new LinearLayout(this); and add Button

     Button btn = new Button(this);
     btn.setText("Just another button");
     linearLayout.addView(btn);
    

    Edit3: another example:

      final Button button = new Button(this);
        button.setText("Press me!");
        setContentView(button);
    
        button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bar graph in my web app (drawn using javascript). It can
I have drawn an object on NSView subclass. How can the object know that
I have a matplotlib horizontal bar drawn as follows: import matplotlib.pyplot as plt from
I have this Google Bar Chart: function drawChart() { var data = new google.visualization.DataTable();
So I've made this range bar chart with the MS Chart Control. I have
I want to draw horizontal stacked bar chart which will have two section. Is
Is there android chart library available by which I can easily draw bar char,
I have the script below which works well, i.e. it generates a chart on
I have drawn an icon in Inkscape , but would now like to programmatically
I have drawn something like this: How to remove the paths and points?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.