0

I want to show a statistic page as a popup from an activity. My question is how do I hand off the data to the canvas drawing it?

My popupWindow xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_dark_teal_bg">

    <LinearLayout
        android:id="@+id/llvStats"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/gradient_teal_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtBanner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:gravity="center_vertical|center_horizontal|center"
            android:text="STATISTICS"
            android:textColor="@color/colorTeal1"
            android:textSize="24sp"
            android:textStyle="bold|italic" />

        <com.mairyu.app.lingoflash.StatsDrawCanvas
            android:id="@+id/statsCanvasDraw"
            android:layout_below="@+id/txtBanner"
            android:layout_width="400dp"
            android:layout_height="500dp"
            android:layout_column="1"
            android:layout_gravity="left|top"
            android:background="@drawable/gradient_light_teal_bg"
            android:layout_row="0" />

    </LinearLayout>

</RelativeLayout>

My popup launch snippet is:

            case R.id.btnDatabaseStats:

                layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

                popupView = layoutInflater.inflate(R.layout.stats_popup_window,null);

                popupWindow = new PopupWindow(popupView,STATS_POPUP_WIDTH,STATS_POPUP_HEIGHT);

                popupWindow.showAtLocation(findViewById(R.id.database_page),
                        Gravity.CENTER, STATS_POPUP_GRAVITY_X, STATS_POPUP_GRAVITY_Y);

                LinearLayout llvStats = (LinearLayout) popupView.findViewById(R.id.llvStats);

                llvStats.setOnTouchListener(new OnSwipeTouchListener(this) {
                    @Override
                    public void onSwipeLeft() {

                        popupWindow.dismiss();
                    }
                });

I want to see something like this:

enter image description here

Since this is not a new activity, I can't use the intent/bundle method to exchange variables (or can I ?). If it would be simple textview or other widgets, I could use setText etc., but I need to get the data inside the canvas.

I think I still lack understanding of how to use onDraw and other canvas features, but right now I just hardcoded the numbers. Is there an easy way to simply get those numbers (5/15/25) from the launching activity ? (I tried a global variable, but it wasn't visible within onDraw)

    @Override
    protected void onDraw(Canvas c) {

        CanvasWidth = c.getWidth();
        CanvasHeight = c.getHeight();

        Paint myPaint = new Paint();
        myPaint.setStyle(Paint.Style.FILL);
        myPaint.setColor(getResources().getColor(R.color.colorTeal1));
        myPaint.setStrokeWidth(10);
        c.drawRect(160, 60, 800, 110, myPaint);
        c.drawRect(160, 160, 250, 210, myPaint);
        c.drawRect(160, 260, 400, 310, myPaint);

        brush.reset();
        brush.setColor(getResources().getColor(R.color.colorTeal1));
        brush.setTextSize(36);
        brush.setTypeface(Typeface.DEFAULT_BOLD);
        c.drawText("1 Day:", 20, 100, brush);
        c.drawText("2 Days:", 20, 200, brush);
        c.drawText("3 Days:", 20, 300, brush);

        brush.setColor(getResources().getColor(R.color.colorTeal3));
        c.drawText("5", 167, 99, brush);
        c.drawText("15", 167, 198, brush);
        c.drawText("25", 167, 298, brush);
    }

Hope this is clear and not too amateurish ...

Mairyu
  • 809
  • 7
  • 24

1 Answers1

0

Actually seems to be pretty simple, now I'm just using a method call within my canvas class:

    public void setBar(ArrayList<Integer> left, ArrayList<Integer> top, ArrayList<Integer> right, ArrayList<Integer> bottom) {

        barCoordLeft = left;
        barCoordTop = top;
        barCoordRight = right;
        barCoordBottom = bottom;

        invalidate();
    }

and access these global variables within onDraw.

In my activity where I launch the popup, I get the canvas via:

statsDrawCanvas = (StatsDrawCanvas) popupView.findViewById(R.id.statsCanvasDraw);

and then pass on the data via the method call:

statsDrawCanvas.setBar(CanvasBarLeftArray, CanvasBarTopArray, CanvasBarRightArray, CanvasBarBottomArray);
Mairyu
  • 809
  • 7
  • 24