Android: setText() for TextView in PopupWindow not working

codesw1tch picture codesw1tch · Aug 11, 2011 · Viewed 8.4k times · Source

Basically, I have a TextView in a layout which I use for a PopupWindow. I show this PopupWindow when a user clicks a button; I want to be able to dynamically change the text in the PopupWindow upon button click. However, findViewById(my_textview).setText() does not seem to do anything, and indeed causes the PopupWindow to no longer show when I click the button.

I can set text from the layout xml fine.

Anyone know what's up with this? Thanks-

Answer

codesw1tch picture codesw1tch · Jul 18, 2012

I solved the problem. For whatever reason you need to call popup.getContentView().findViewById instead of just findViewById (where popup is your PopupWindow object). I wasn't getting a NullPointerException before so I'm not exactly sure why this fixed the issue but it did.

So the code goes something like:

PopupWindow pw = new PopupWindow(your layout and params here);

((TextView)pw.getContentView().findViewById(R.id.my_textview)).setText("hello there");

pw.showAtLocation(your params here);