Previously created already popup everything worked, but on this minimum code is not. I do not understand what's the matter, tell me. Pressing the button works, but the button itself does not work, i.e. No click animation, the same with editText.

public class MainActivity extends Activity { LinearLayout lay; PopupWindow popup; Button btnOpenPopup, btnClosePop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOpenPopup = (Button) findViewById(R.id.popupbutton); btnOpenPopup.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popup.showAsDropDown(v, 30, 60); } }); View window = getLayoutInflater().inflate(R.layout.popup, null); Button b = (Button) window.findViewById(R.id.btnPopup); popup = new PopupWindow(window, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); btnClosePop = (Button) window.findViewById(R.id.btnPopup); btnClosePop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("tag", "pressed"); } }); } } 

Leighout is the simplest:

POPUP:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnPopup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Close" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> 

MAIN:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drag1" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drag2" /> <Button android:id="@+id/popupbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup" /> </LinearLayout> 

    1 answer 1

    As I understand it, in rows

     public void onClick(View v) { popup.showAsDropDown(v, 30, 60); } 

    The popup variable is not initialized. It is strange that the code worked and did not give an error.

    UPDATE

    Andreich, I agree, initialization is below. Today I also suffered for a long time, I found a lot of rakes with this PopupWindow. In particular, when opening a button, I have a different code.

      @Override public void onClick(View view) { if (popupWindow == null) { // здСсь инициализация popupWindow ΠΈ Π΅Π³ΠΎ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. } if (!popupWindow.isShowing()) { popupWindow.showAsDropDown(txtSomeElement, 0, 0); } } 
    • it is initialized. Watch carefully - andreich