title: Set custom layout in popup window in android
body: <p>I have a problem with popup window. I want to create popup window with my own layout. 
  This is code:</p>
  
  <pre><code>public class PopupWindowView extends PopupWindow{
  
      PopupWindow popup;
      boolean click = true;
      LayoutParams params;
      RelativeLayout mainLayout;
      TextView tv;
      LinearLayout layout;
      ImageView chooseFlag;
  
      public void createPopupWindow(Activity act){
          popup = new PopupWindow(act);
          chooseFlag = (ImageView) act.findViewById(R.id.login_choose_flag);
          mainLayout = (RelativeLayout) act.findViewById(R.id.login_layout);
          tv = new TextView(act);
          layout = new LinearLayout(act);
          //layout = (LinearLayout) findViewById(R.id.popuplayout);
          chooseFlag.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  if (click) {
                      int[] values = new int[2]; 
                      v.getLocationOnScreen(values);
                      popup.showAtLocation(mainLayout, Gravity.NO_GRAVITY, 10, 10);
                      popup.update(values[0], values[1], 300, 80);
                      click = false;
                  } else {
                      popup.dismiss();
                      click = true;
                  }
              }
          });
          params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                  LayoutParams.WRAP_CONTENT);
          layout.setOrientation(LinearLayout.VERTICAL);
          tv.setText("Hi this is a sample text for popup window");
          layout.addView(tv, params);
          popup.setContentView(layout);
      }
  }
  </code></pre>
  
  <p>and this is layout which I want to set in my popup window:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
     &gt;
  
      &lt;LinearLayout
          android:id="@+id/popuplayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:background="@color/patient_button_bg"&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>In my class I can't use <code>findbyid</code> method because this is not Activity. How I can set my own layout in popup widow in my class?</p>
  
  <p>Edit:
  this is stack trace where I get error:</p>
  
  <pre><code>03-01 09:48:48.761: E/AndroidRuntime(16776): FATAL EXCEPTION: main
  03-01 09:48:48.761: E/AndroidRuntime(16776): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addView(ViewGroup.java:3208)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addView(ViewGroup.java:3188)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.preparePopup(PopupWindow.java:969)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:840)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:813)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at pl.asseco.amms.mobile.tools.PopupWindowView$1.onClick(PopupWindowView.java:44)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.View.performClick(View.java:3558)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.View$PerformClick.run(View.java:14152)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Handler.handleCallback(Handler.java:605)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Handler.dispatchMessage(Handler.java:92)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Looper.loop(Looper.java:137)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.app.ActivityThread.main(ActivityThread.java:4514)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  03-01 09:48:48.761: E/AndroidRuntime(16776):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Edit 
  Activity which use popup:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
      getActionBar().hide();
  
      mainMenuGenerator = new MainMenuGenerator();
      mainMenuGenerator.generateMainMenu(this);
      mainMenuGenerator.hideIcons();
      popup = new PopupWindowView();
  
      popup.createPopupWindow(this);
  
  }
  </code></pre>