Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: setText() for TextView in PopupWindow not working

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-

like image 610
codesw1tch Avatar asked Aug 10 '11 22:08

codesw1tch


2 Answers

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);

like image 159
codesw1tch Avatar answered Nov 15 '22 18:11

codesw1tch


You will be able to find the views with the "findViewById" only using the view you inflated the popupWindow before

like this

private View viewPopUp;
private PopupWindow windowPopUp;
//...
//form_popup is the template to the popup
viewPopUp = mContext.getLayoutInflater().inflate(R.layout.form_popup, null); 
windowPopUp = new PopupWindow(viewPopUp, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//...
viewPopUp.findViewById(R.id.popupTopTitle);
viewPopUp.findViewById(R.id.popupMiddleMsg);
//...
like image 33
Leandro Silva Avatar answered Nov 15 '22 17:11

Leandro Silva