0

Here's a challenge for all you android master programmers out there! I am currently putting 2 surface views into a fragment (LeftPanel and MySurfaceView), below a 'banner' edit text area and with 1 button below the two surface views in a 'footer ' position. The layout is described in the fragment3.xml file see below :-

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#eeeeee"
    >

    <EditText
        android:id="@+id/textF3"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text=" Fragment 3 "
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@null"
        />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textF3"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        >

        <com.fragmentnavigationtestbed.LeftPanel
            android:id="@+id/leftPanel"
            android:layout_width="wrap_content"
            android:layout_height="633dp"
            android:layout_weight="0.5"
            />

        <com.fragmentnavigationtestbed.MySurfaceView
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="633dp"
            android:layout_weight="0.5"
            />

    </LinearLayout>


    <Button
        android:id="@+id/button32"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:text="GO BACK TO FRAGMENT 1"
        android:onClick="goto_fragment1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"

     />
</RelativeLayout>

This fragment is inflated by the onCreateView() method in the Fragment3.java file where the 2 surfaceViews (SVs) are instantiated and have their inTouchListeners set inside the onActivityCreated Method.

       @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       return inflater.inflate(R.layout.fragment3, container, false);
   }

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
        panel = new LeftPanel(this.getActivity());
        panel.setOnTouchListener(this);

       myView = new MySurfaceView(this.getActivity());
       myView.setOnTouchListener(this);

    }

I have a simple draw method (doDraw) which draws a square on a coloured background, I cannot get this to create any visible change to the SV LeftPanel, unless its within the onTouchEvent() method of the LeftPanel.java file and only when the user clicks the surface of the Leftpanel SV. (This problem applies to the other SV MySurfaceView as well !).

 public void doDraw() {
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);

        holder = getHolder();
        Canvas canvas = holder.lockCanvas();
        if (canvas != null) {
            canvas.drawColor(Color.CYAN);
            canvas.drawRect(100, 100, 200, 200, paint);
            holder.unlockCanvasAndPost(canvas);
        }
    }

If you don't click the SV you see just black. I've tried putting the draw method in the Fragment3 onActivityCreated() method, and in LeftPanel's run() method, but though the method is evoked nothing happens on the SV. It only works in the onTouchEvent() method

As long as I click on my Leftpanel SV, I get my onDraw() method result!

My problem is I want to visibly display, the results of my OnDraw() method before any user action has occurred, can anyone give me some help here?
I've searched a lot of the S/O answers, but none go anywhere near this area.

  • Any particular reason you need SurfaceView? Custom Views are easier to work with and feature hardware-accelerated Canvas rendering -- http://developer.android.com/training/custom-views/index.html . – fadden May 16 '16 at 20:19
  • Hi fadden, I chose SurfaceView because most of the online tutorials use this for 2D graphics and a lot of them feature simple game programming. Though my app isn't going to be a game its going to be a bit busy with graphics, which have to respond to user interaction. But for my problem, I thought using SVs would be relatively easy, but clearly not! So I'm going to take a look at your link and hope it'll show me a way to draw my graphics the way I want to ! – Martin Clifford May 18 '16 at 15:20

0 Answers0