How Android pops up Popwindow at initialization time

Source: Internet
Author: User

http://blog.csdn.net/sxsboat/article/details/7340759

Android in OnCreate () pop-up Popwindow, many people have similar needs, but directly in OnCreate () call Popwindow showatlocation () method is reported abnormal, The reason is that the activity has not been initialized at this time, there are some solutions on the Internet, but generally through a short period of time to pop up the implementation, code is not strong enough. In fact, you can constantly detect the state of the current activity, once the initialization is complete immediately call Popwindow's Showatlocation () method display. The detailed code is as follows:

[Java]View Plaincopy
  1. /************************************************************************
  2. *
  3. * This class is used to pop up a popwindow at initialization time, the file used is as follows, especially to note that the
  4. * android:minheight= "1DP" and Android:minwidth= "1DP" properties. The key to ejecting Popwindow when initializing is
  5. * Wait for the entire activity to initialize before calling the Showatlocation () method, otherwise it will be out of the ordinary.
  6. * The implementation of the method is to constantly detect whether the acitvity is initialized, once the completion of the call
  7. * Showatlocation () method shows Popwindow.
  8. *
  9. * The following are the layout files:
  10. * <?xml version= "1.0" encoding= "Utf-8"?>
  11. * <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  12. * android:id= "@+id/main"
  13. * android:layout_width= "Fill_parent"
  14. * android:layout_height= "Fill_parent"
  15. * android:minheight= "1DP"
  16. * android:minwidth= "1DP"
  17. * android:orientation= "Vertical" >
  18. * </LinearLayout>
  19. *
  20. * Stone
  21. * Completion time: 2012/3/10
  22. * Version: 1.0
  23. *
  24. ************************************************************************/
  25. Package CN.SD.SX.INITPOPW;
  26. Import android.app.Activity;
  27. Import Android.graphics.Color;
  28. Import Android.os.Bundle;
  29. Import Android.os.Handler;
  30. Import android.view.Gravity;
  31. Import Android.view.View;
  32. Import Android.view.ViewGroup.LayoutParams;
  33. Import Android.widget.LinearLayout;
  34. Import Android.widget.PopupWindow;
  35. Import Android.widget.TextView;
  36. Import CN.SD.SX.POPW.R;
  37. Public class Popwactivity extends Activity {
  38. private Popupwindow Mpopupwindow;
  39. private Handler Mhandler;
  40. //Detection time interval
  41. private int detchtime = 5;
  42. @Override
  43. public void OnCreate (Bundle savedinstancestate) {
  44. super.oncreate (savedinstancestate);
  45. Setcontentview (R.layout.main);
  46. Mhandler = new Handler ();
  47. //Display Popwindow
  48. Showpopwindow ();
  49. }
  50. private void Showpopwindow ()
  51. {
  52. LinearLayout view = new LinearLayout (this);
  53. Layoutparams params = new Layoutparams (Layoutparams.fill_parent,
  54. Layoutparams.fill_parent);
  55. TextView txt1 = new TextView (this);
  56. Txt1.setlayoutparams (params);
  57. Txt1.setgravity (Gravity.center);
  58. Txt1.settext ("txt1");
  59. Txt1.setbackgroundcolor (color.red);
  60. View.addview (TXT1);
  61. Mpopupwindow = New Popupwindow (view, 300);
  62. Mpopupwindow.setoutsidetouchable (true);
  63. /***************** The following code is used to loop to detect if activity is initialized ***************/
  64. Runnable showpopwindowrunnable = new Runnable () {
  65. @Override
  66. public Void Run () {
  67. //Get the root element in activity
  68. View view = Findviewbyid (R.id.main);
  69. //How the width and height of the root element is greater than 0 indicates that activity has been initialized
  70. if (view! = null && view.getwidth () > 0 && view.getheight () > 0) { /c2>
  71. //Display Popwindow
  72. Mpopupwindow.showatlocation (popwactivity. This.findviewbyid (R.id.main),
  73. Gravity.center, 0, 0);
  74. //Stop detection
  75. Mhandler.removecallbacks (this);
  76. } Else {
  77. //If activity is not initialized, wait 5 milliseconds for re-detection
  78. Mhandler.postdelayed (this, detchtime);
  79. }
  80. }
  81. };
  82. //Start detection
  83. Mhandler.post (showpopwindowrunnable);
  84. /****************** The above code is used to loop to detect if activity is initialized *************/
  85. }
  86. }

How Android pops up Popwindow at initialization time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.