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

  • SEARCH
  • Home
  • 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 8632055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:17:59+00:00 2026-06-12T09:17:59+00:00

I am using a View extended from View group and the onDraw is not

  • 0

I am using a View extended from View group and the onDraw is not getting called when I call invalidate.Can any one please explain?
The code is given below

public class BarGraph extends View {    
private int viewHeight;
private int viewWidth;                          // height and width of bar graph dynamically calculated.
private int mGraphColor;                        // the colour of the bar graph.

private ArrayList<Integer> mTodays;             // The array list that handles the input to draw the graph.
private final int barCount  =   20;             // Number of bars in the bar graph...here set as 20.

/* 
 * The maximum action value a bar can take.
 * This is calculated based on the action array
 * passed to the chart.
 */

private int yMax    =   0;

private Paint graphColor;

public BarGraph(Context context, int graphColor) {
    super(context);
    this.setWillNotDraw(false);
    mGraphColor     =   graphColor;
    setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    initializePaintObjects();
}

public BarGraph(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setWillNotDraw(false);
    initializePaintObjects();
}

private void initializePaintObjects(){
    graphColor = new Paint();
    graphColor.setColor(mGraphColor);       
    graphColor.setStyle(Style.FILL);
}

@Override 
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(mTodays == null)                     // If the array list is empty.
        return;
    if(yMax <= 0)                       
        return;
    int left    =   0;

    viewHeight  = getHeight();
    viewWidth   = getWidth();

    if((viewWidth % barCount) > 0){     //adjusting the view width so that bars correctly fits in 
        int newWidth    =   (int) (Math.floor(viewWidth / barCount) * barCount);
        left            =   (int) Math.floor(((viewWidth - newWidth)/2));
        viewWidth       =   (int) Math.floor((viewWidth / barCount) * barCount);
    }

    int columnWidth     =   2;
    columnWidth         =  (int) Math.floor(viewWidth / barCount);

    int xFactor         =   1;    
    xFactor             =   (int) Math.ceil(columnWidth * 0.33);

    int barWidth        =   1;
    barWidth            =   columnWidth - xFactor;

    graphColor.setStrokeWidth(barWidth);

    int j = 0;

    for(int i = 0; i < mTodays.size() ; i++){
        int todayValue      =   mTodays.get(i);
        float todaysHeight;
        if(todayValue == 0){
            todaysHeight    =   (float) (viewHeight-viewHeight*(.001));
        }else{
            todaysHeight    =   getYValue(todayValue);
        }

        canvas.drawLine(((j*columnWidth)+xFactor + left) , viewHeight, ((j*columnWidth)+xFactor + left), todaysHeight, graphColor);
        j++;
    }

}

public void setData(ArrayList<Integer>todays){

    mTodays         =   todays;

    yMax            =   0;
    for (int val : mTodays){
        yMax        =   yMax > val ? yMax : val;
    }
    invalidate();
}

private int getYValue(int item){
   int percent = (item * 100)/yMax;
   return (viewHeight) - ((percent * viewHeight)/100);
}

}

  • 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-06-12T09:18:00+00:00Added an answer on June 12, 2026 at 9:18 am

    Thanks for all your attempts.The problem was actually in the Main Activity.
    The following is the correct code

    final Handler handler = new Handler(); 
                    Timer timer2 = new Timer(); 
                    TimerTask testing = new TimerTask() {
                        public void run() { 
                            handler.post(new Runnable() {
                                public void run() {
                                    for(int j=0;j<19;j++){
                                        todays.set(j, todays.get(j+1));
                                    }
                                    Random diceRoller = new Random();
                                    todays.set(19,  diceRoller.nextInt(100)*10+1);
                                    MainActivity.this.runOnUiThread(new Runnable() {
    
                                        @Override
                                        public void run() {
                                            // TODO Auto-generated method stub
                                            bargraph.setData(todays);    
                                        }
                                    });
                                }
    
                            });
    

    I had not written the runOnUiThread at first…My bad…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using image view and gallery in my xml file. i am getting
I'm using an extended version of BaseAdapter based on the EfficientAdapter example from the
I've been working on a very simple little application using an extended view. The
Is using view in db design right method or we should handle it code
I'm trying to create a page by using view 2. This page list all
In my View (using Zend_View so the the view is an object), I make
When using doc-view in Emacs, I'd like to jump to different locations (e.g., move
In my MVC view (using VS 2012 RC), I'm attempting to use jQuery to
I am using MVC-View Model, EF Model first I am having problems with two
I am using a View / Presenter structure for my project. The project is

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.