Juan H Juan H - 2 years ago 84
SQL Question

How to draw on canvas using database data in android

I'm new to Android and I have been struggling to draw on canvas a rectangle and text where the size of the rectangle, the text and the color depend on values selected from a MySql database.

I managed to get data selected inside my Activity, but I just can't figure it out how to pass the MySql data to the

method so I can draw the rectangle and text using the data.

Any help will be greatly appreciated.

public class MyTankActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

Intent intent = getIntent();
int useridInt = intent.getIntExtra("userid", -1);
String userid = useridInt+"";

Response.Listener<String> responseListener = new Response.Listener<String>() {
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);

int success = jsonResponse.getInt("success");
JSONArray tank_data = jsonResponse.getJSONArray("tank_data");

if (success == 1) {
int i;
// Log.v("Result--", "" + tank_data.getString(i));

JSONObject tankObj = tank_data.getJSONObject(0);

String location = (String) tankObj.getString("Location");
String color = (String) tankObj.getString("Color");
String Level = (String) tankObj.getString("Level");
} else {
// No records found in database
} catch (JSONException e) {

MyTankRequest myTankRequest = new MyTankRequest(userid, responseListener);
RequestQueue queue = Volley.newRequestQueue(MyTankActivity.this);

setContentView(new TankView(this));

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_height="match_parent" />


TankView class:

package com.nivelsonic.nivelsonic;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TankView extends View {

private Paint _paintTank = new Paint();
private Path _path = new Path();

public TankView(Context context) {
// TODO Auto-generated constructor stub
init(null, 0);

public TankView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(attrs, 0);

public TankView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(attrs, 0);

private void init(AttributeSet attrs, int defStyle) {

public void onDraw(Canvas canvas) {
// canvas.drawText();
// canvas.rect();

Answer Source

One suggestion:

Move JSON data to global variables

public class MyTankActivity extends AppCompatActivity {

   String location;
   String color;
   String Level;

And change setContentView with different constructor of TankView that allows to pass the obtained data:

  setContentView(new TankView(this, location, color, Level));

Modify the constructor of TankView accordingly, also add some variables to TankView to store this values:

  public TankView(Context context, String loc, String color, String level) {
    init(null, 0);
    this.location = loc;// ... do the same with color/level

Modify the onDraw after you get the data

    public void onDraw(Canvas canvas) {
        //using this.Location, color, text
        // canvas.drawText();.
        // canvas.rect();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download