2

I am implementing more than two buttons inside SurfaceView and are clickable. I have created customView and surfaceview in my app but however, I do not know how to put these button inside my code.I have found out that surfaceview can not have any "child", but I am struck , I do not know how should I edit my code so that I able to get the button appear.I have tried searching it online but I do not really understand how does it works, as I am new to android java. Any help will be greatly appreciated!

SurfaceViewExample.Java

public class SurfaceViewExample extends Activity implements OnTouchListener{

OurView v;
Bitmap ball, blob;
float x,y;
Button btnClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    v = new OurView(this);
    v.setOnTouchListener(this);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    x = y = 0;

    btnClick = new Button(this);
    btnClick.setText("Click");
    btnClick.setGravity(Gravity.TOP | Gravity.LEFT);
    btnClick.setVisibility(View.VISIBLE);

    //adding the btn to the Custom VIEW
    v.addView(btnClick);
    setContentView(v);

}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    v.pause();
}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    v.resume();
}


public class OurView extends SurfaceView implements Runnable{

    Thread t = null;
    //helps to change the dimension of the surfaceview
    SurfaceHolder holder;
    boolean isItOk = false;


    public OurView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();
    }
    public void addView(Button button_clear) {
        // TODO Auto-generated method stub

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isItOk  == true){
            //perform canvas drawing
            //if surface view is NOT VALID
            if(!holder.getSurface().isValid()){

                continue; // if it is FALSE -- will executed the codes after this {}
            }
                Canvas c = holder.lockCanvas();
                //background of the canvas color!
                c.drawARGB(255, 150, 150, 10);
                //THREAD
                c.drawBitmap(ball, x - (ball.getWidth()/2), y - (ball.getHeight()/2), null);
                holder.unlockCanvasAndPost(c);
        }
    }

    public void pause() {
        isItOk = false;
        while(true){
            try{
                t.join();
            }
            catch ( InterruptedException e){

                e.printStackTrace();
            }
            break;
        }
        t = null;
    }

    public void resume(){
        isItOk = true;
        t = new Thread(this);
        t.start();
    }
}
@Override
public boolean onTouch(View arg0, MotionEvent me) {
    // TODO Auto-generated method stub

    //reduce the time taken for process
    try{

        Thread.sleep(50); //20 times per seconds
    }
    catch ( InterruptedException e){
        e.printStackTrace();
    }

    switch(me.getAction()){

    case MotionEvent.ACTION_DOWN:
        x = me.getX();
        y = me.getY();
        break;

    case MotionEvent.ACTION_UP:
        x = me.getX();
        y = me.getY();
        break;

    case MotionEvent.ACTION_MOVE:
        x = me.getX();
        y = me.getY();
        break;
    }

    return true;
}

}

------MYLayout-------

activity_main.XML

<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"
    tools:context=".MainActivity" >

</RelativeLayout>
nandeesh
  • 24,740
  • 6
  • 69
  • 79
ping
  • 133
  • 1
  • 1
  • 7

1 Answers1

1

Why you not create one LinearLayout which have parent as RelativeLayout and add view to that LinearLayout. Now after that create another LinearLayout which contain your buttons.

In this way you can have more then ona button on your surfaceView.

Hope you got mypoint. If not then let me know. i will help you.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Hey.. Thanks for your help! I managed to link my layout in XML with my surfaceView. But do u have any idea how to load image from the button that I clicked into surfaceview? – ping Dec 18 '12 at 06:49
  • means on the click of the button you want to load images on surfaceView right ? – Shreyash Mahajan Dec 18 '12 at 10:53
  • if yes then you have to see this: http://stackoverflow.com/questions/6526470/fastest-way-to-load-and-display-a-jpeg-on-a-surfaceview and also http://stackoverflow.com/questions/7740687/add-image-to-surface-view-in-android – Shreyash Mahajan Dec 18 '12 at 10:54
  • If my answer help you then please also upvote it so it can help other also. – Shreyash Mahajan Dec 18 '12 at 10:55
  • wow!! THANKS for your help! I will read thru it! Sorry I tried to upvote it but I haven't meet the no.of reputation it required >. – ping Dec 19 '12 at 01:22