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 6145863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:51:34+00:00 2026-05-23T18:51:34+00:00

I have a custom view public GraphView(Context context, float[] values, String title, String[]horlabels, String[]

  • 0

I have a custom view

  public GraphView(Context context, float[] values, String title, String[]horlabels,  String[] verlabels, boolean type, int color) {
    super(context);
    if (values == null)
        this.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();
    this.color = color;
}

@Override
protected void onDraw(Canvas canvas) {
    float border = 20;
    float horstart = border * 2;
    float height = getHeight();
    float width = getWidth() - 1;
    float max = getMax();
    float min = getMin();
    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.BLACK);
        canvas.drawText(verlabels[i], 0, y, paint);
    }
    int hors = horlabels.length - 1;
    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-1)
            paint.setTextAlign(Align.RIGHT);
        if (i==0)
                paint.setTextAlign(Align.LEFT);
        paint.setColor(Color.BLACK);
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

    if (max != min) {
        paint.setColor(color);
        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;
}

and when i call
setContentView(graphView);
it’s work good, but when i call

            diagramRelativeLayout = (RelativeLayout)switcher.getChildAt(i).findViewById(R.id.diagramRelativeLayout);
            diagramRelativeLayout.addView(graphView);

it’s don’t work.

 Error:07-12 11:07:38.933: ERROR/AndroidRuntime(30488): java.lang.RuntimeException:  Unable to resume activity {com.meters.src/com.meters.src.Main_Activity}:  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

RelativeLayout contained in ViewFlipper. How i can add my view to this layout?

  • 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-23T18:51:35+00:00Added an answer on May 23, 2026 at 6:51 pm

    I did it, I just made xml with one layout, and added a reference to it in diagramRelativeLayout:

    my xml:

      <RelativeLayout
      android:id = "@+id/diagramRelativeLayout"
      android:background="@layout/button_style"
      android:layout_width="fill_parent"
      android:layout_height = "fill_parent"
      android:layout_marginTop = "10sp"
      android:layout_marginLeft = "5sp"
      android:layout_marginRight = "5sp"
      android:layout_marginBottom = "10sp"
      android:gravity="center"> 
        <include android:id="@+id/graph" layout="@layout/graph"/>  
      </RelativeLayout>
    

    My java:

    graphLayout = (RelativeLayout)switcher.getChildAt(i).findViewById(R.id.graph);
    graphLayout.addView(graphView);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

friends, i have a custom view class public class Zoom extends View { private
i have a Custom view ... package nan.salsa.goal.customview; import android.R; import android.content.Context; import android.graphics.Canvas;
I have created a Custom preference which has the following constructor public CoordinatesPreference(Context context,
I have made a small custom view component: public class ActionBar extends RelativeLayout {
I have a custom view engine in ASP.NET MVC and since upgrading to RC1
I have a custom control/view that observes the direction of a gesture within its
I have a custom-made view that extends the View class. I would like 2
I have a simple custom view that is connected via outlet to a NIB.
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have the following situation: There is one custom view inside of the first

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.