Juan H Juan H - 7 months ago 22
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

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

Any help will be greatly appreciated.

public class MyTankActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
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;
for(i=0;i<tank_data.length();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) {
e.printStackTrace();
}
}
};

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

setContentView(new TankView(this));
}
}


XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.nivelsonic.nivelsonic.MyTankActivity"
android:background="#AEECFF">

<com.nivelsonic.nivelsonic.TankView
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>


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) {
super(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) {
_paintTank.setColor(Color.RED);
_paintTank.setAntiAlias(true);
_paintTank.setStyle(Paint.Style.STROKE);
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawText();
// canvas.rect();
}
}

Answer

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) {
    super(context);
    init(null, 0);
    this.location = loc;// ... do the same with color/level
    ...
 }

Modify the onDraw after you get the data

@Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //using this.Location, color, text
        // canvas.drawText();.
        // canvas.rect();
    }   
Comments