0

I want to create a popup window in a fragment. The popup will be created on the button press down and destroyed on button press up. This is the code I have currently but the window is not displayed or cannot be seen.

Inside the fragment class:

This is the OnTouchListener method for the button

Button button2 = (Button) view.findViewById(R.id.res_weak_button);
    button2.setOnTouchListener(
            new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        Log.w("PokeApp", "DOWN");
                        createPopupVisuals();
                        return true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        Log.w("PokeApp", "UP");
                        popupMessage.dismiss();
                        return true;
                    }
                    return false;
                }
            }
    );

This is the method to create the popup

private void createPopupVisuals(){

    RelativeLayout layout = new RelativeLayout(getActivity());
    TextView resist = new TextView(getActivity());
    resist.setText("Pokemon is resistant to:");
    TextView weakness = new TextView(getActivity());
    weakness.setText("Pokemon is weak against:");

    RelativeLayout.LayoutParams resistParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    RelativeLayout.LayoutParams weaknessParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    weaknessParams.addRule(RelativeLayout.BELOW, resist.getId());
    weaknessParams.setMargins(0,40,0,0);

    resist.setLayoutParams(resistParams);
    weakness.setLayoutParams(weaknessParams);

    layout.addView(resist);
    layout.addView(weakness);

    //TODO: Get types

    popupMessage = new PopupWindow(layout, RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    popupMessage.setContentView(layout);
    Log.w("PokeApp", "Should have created layout");
}
chiangy77
  • 163
  • 1
  • 5
  • 14

1 Answers1

0

I simply forgot to add this line at the bottom.

popupMessage.showAsDropDown(aButton, 0, 0);

It now works perfectly

chiangy77
  • 163
  • 1
  • 5
  • 14