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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:14:04+00:00 2026-06-05T13:14:04+00:00

We are writing a dashboard for a car. The code for calculating/drawing the speedometer

  • 0

We are writing a “dashboard” for a car. The code for calculating/drawing the speedometer was done separate from the rest of the code, and now I am trying to combine the two.

They both work separately, and with the speedometer view code in, the main portion of the UI is still functioning, I just can’t see the speedometer.

I have it compiling, and there are no obvious errors. However, when the app is run, I don’t see the speedometer showing up in the main view, even though I have added it using the addView method.

For Reference:

My code to create and add the view:

    setContentView(R.layout.main); 
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
    myView v = new myView(this);
    v.setLayoutParams(lp);
    relativeLayout.addView(v, lp);

The code for the myView class:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;

public class myView extends View {
    private Paint mPaints;
    private Paint textpaint;
    private boolean mUseCenters;
    private RectF mBigOval;
    private float mStart;
    private float mSweep;
    public float SPEED_raw;
    public int SPEED = 0;   //initialized to 0 just for loop.  Remove initialization when IF statement below is deleted
    public static Bitmap gaugefront;
    public static Bitmap gaugeback;

    public myView(Context context) {
        super(context);
        mPaints = new Paint();
        mPaints.setAntiAlias(true);
        mPaints.setColor(Color.YELLOW);

        textpaint = new Paint();
        textpaint.setAntiAlias(true);
        textpaint.setColor(Color.WHITE);
        textpaint.setTextSize(150);
        textpaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        gaugefront = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugefront);    //Load gaugefront.png
        gaugeback = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugeback);      //Load gaugeback.png

        mUseCenters = true;
        mBigOval = new RectF(400, 10, 880, 490);                //left[x-coordinate],top[y-coordinate],right[x],bottom[y]
    }

    private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) {
        canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(myView.gaugeback, 400, 10, null);     //draw gauge background, X coordinate:400, Y coordinate: 10
        drawArcs(canvas, mBigOval, mUseCenters, mPaints);

        //SPEED_raw = 70;                   //Raw float data from CCS. Uncomment when the IF statement below is deleted.
        SPEED = Math.round(SPEED_raw);      //SPEED integer.  Units km/h - Top speed of 135. Used for digital readout

        mStart = 90;                        //Start drawing from -90 degrees (Counter clockwise)                
        mSweep = SPEED_raw*2;               //Draw stop point

            if(SPEED >= 135){           //JUST FOR SHOW.  Delete this for actual speedo
                SPEED_raw = 0;          // "
            }                           // "
            else                        // "
                SPEED_raw += 0.5;       // "

        canvas.drawBitmap(myView.gaugefront, 400, 10, null);                //draw gauge foreground, X coordinate:400, Y coordinate: 10

        String speed_string = String.valueOf(SPEED);                        //Convert int SPEED to String for display

//            while (speed_string.endsWith(".0") || speed_string.endsWith(".")){            //Erase trailing ".0" for float types. Don't need if SPEED is an int
//              speed_string = (speed_string.substring(0, speed_string.length() - 1));
//            }

        canvas.drawText(speed_string, 640, 420, textpaint);                 //arguments: (string, x-coordinate, y-coordinate, paint)

        invalidate();
    }
}

Here’s my XML File:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/solarbg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/solarbg" />
</LinearLayout>

<ImageView
    android:id="@+id/l_lamp"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="left"
    android:src="@drawable/l_arrow_dim" />

<DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:textColor="#ffffff"
    android:textSize="50dp" />

<ImageView
    android:id="@+id/r_lamp"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/r_arrow_dim" />

<TextView
    android:id="@+id/setAmps"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:gravity="left"
    android:text="@string/setAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/setAmpsVal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_below = "@+id/setAmps"
    android:layout_alignParentLeft="true"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/setVelocity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/setAmps"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:gravity="right"
    android:text="@string/setVelocity"
    android:textColor="#ffffff"
    android:textSize="30dp" />

    <TextView
    android:id="@+id/setVelocityVal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layout_below = "@+id/setVelocity"
    android:layout_alignParentRight="true"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/actVelocity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/setVelocityVal"
    android:gravity="right"
    android:text="@string/actVelocity"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/actVelocityVal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below = "@+id/actVelocity"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/busAmps"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/setAmpsVal"
    android:gravity="left"
    android:text="@string/busAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" /><TextView
    android:id="@+id/busAmpsVal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_below = "@+id/busAmps"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

  <TextView
    android:id="@+id/powerVal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom = "true"
    android:gravity="right"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/power"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_above = "@+id/powerVal"
    android:gravity="left"
    android:text="@string/power"
    android:textColor="#ffffff"
    android:textSize="30dp" />


 <TableLayout
    android:id="@+id/msgCenterLayout"
    android:layout_width="600dp"
    android:layout_height="200dp" 
    android:layout_alignParentBottom = "true" 
    android:layout_centerHorizontal = "true"
    android:orientation = "vertical"
    android:visibility = "invisible">

<ScrollView
    android:id="@+id/messageCenterText"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left"
    android:text="@string/busAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" />


</TableLayout>

</RelativeLayout>

edit:
I’m using an XML file for the main layout, and trying to add this new view to a relativelayout inside that XML file.

  • 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-05T13:14:06+00:00Added an answer on June 5, 2026 at 1:14 pm

    Try writing

    LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT); 
    
    v.setLayoutParams(lp);
    relativeLayout.addView(v);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Dashboard Application in C#, using DevExpress components. I am trying to
Writing Classic ASP code in JScript has a lot going for it: more humane
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing documentation in html requires some code examples. What to do with characters that
I'm writing a reports dashboard for a rails app. The dashboard is for user
Writing/reading code seems less stress then preparing a deploy scripts such as ./configure then
I'm writing a Dashboard widget in Dashcode, and on the back side, I've got
I'm writing some sort of new adminstration dashboard for our cms (which runs on
I'm writing an application that'll display a lightweight dashboard to the computer's secondary display
I'm writing a new theme from scratch, based off the Toolbox theme. My installation

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.