0

My layout has 4 button. One is the 'Right' answer and the other 3 are 'Wrong' answers. I want the SAME popup to be displayed with one of the 3 'wrong' buttons are pressed. Here is the code that i have got.

I dont want to repeat this Code for EACH of the 3 buttons, how do i call the same code with the different button names?

    ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);                            

            Button btnDismiss = (Button)popupView.findViewById(R.id.nextscreen);             
            btnDismiss.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,LearningTimeMenu.class);
                    startActivity(myintent1);
                }
            });
        }});

1 Answer 1

2

Try this

private void createPopUP()
{
    LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);                            

            Button btnDismiss = (Button)popupView.findViewById(R.id.nextscreen);             
            btnDismiss.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,LearningTimeMenu.class);
                    startActivity(myintent1);
                }
            });

}

ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
rredButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        createPopUP();

    }});

Otherwise in xml file, use

<Button ..........
 android: onClick ="createPopUP"
</Button>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This worked. Is there any particular reason to use "Private" void vs. "protected" void?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.