Search code examples
androidbuttonadditionsurfaceview

Button not working on surface view


I've was studying on some source code from this link http://android-er.blogspot.com/2010/12/add-overlay-on-camera-preview.html

what should i do here if i want to add another button, i've added button in xml, but they are not working, their source code is:

LinearLayout layoutbg = (LinearLayout) findViewById(R.id.background);
    layoutbg.setOnClickListener(new LinearLayout.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

            case (R.id.previous_btn):
            //some source   
showImage();
                break;

            case (R.id.next_btn):
    //some source           showImage();
                break;

            }

        }

        private void showImage() {
            // TODO Auto-generated method stub
    Toast.makeText(AndroidCamera.this, " User don't  ", Toast.LENGTH_SHORT);
        }

    });
    }

Solution

  • make your Activity implement View.OnClickListener

    After that in your onCreate() method do:

    Button btn1= findViewById(R.id.previous_btn);
    Button btn2= findViewById(R.id.next_btn);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    

    In the OnClick() Method that you have to override after implementing View.OnClickListener do the following:

    @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
    
                case (R.id.previous_btn):
                //some source   
    showImage();
                    break;
    
                case (R.id.next_btn):
        //some source           showImage();
                    break;
    
                }
    
            }