Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 23rd, 2012  |  syntax: None  |  size: 1.96 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to create Dialog on startup in android?
  2. public class PopupActivity extends Activity implements OnClickListener {
  3.  
  4.  
  5. LinearLayout ll = new LinearLayout(this);
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8.     super.onCreate(savedInstanceState);
  9.     setContentView(ll);
  10.  
  11. }
  12.  
  13.   protected void onStart()
  14.  {
  15. super.onStart();
  16. final PopupWindow pw;
  17. Button button = new Button(this);
  18. button.setText("Hello");
  19. pw = new PopupWindow(button, 245, 284, true);
  20. button.setOnClickListener(new OnClickListener() {  
  21.     @Override  
  22.     public void onClick(View view) {  
  23.         pw.dismiss();            
  24.     }  
  25. });
  26.  
  27. pw.showAsDropDown(ll, 10, -50);
  28. }
  29.        
  30. <?xml version="1.0" encoding="utf-8"?>
  31. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  32.     android:layout_height="fill_parent"
  33.     android:layout_width="fill_parent"
  34.     android:orientation="vertical"
  35.     android:id="@+id/layoutTemp">
  36.  
  37. </LinearLayout>
  38.        
  39. <?xml version="1.0" encoding="utf-8"?>
  40. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  41.     android:orientation="vertical"
  42.     android:padding="10dip"
  43.     android:layout_width="fill_parent"
  44.     android:layout_height="wrap_content"
  45.     android:background="#FFFFFF">
  46.  
  47.     <TextView
  48.         android:layout_width="fill_parent"
  49.         android:layout_height="wrap_content"
  50.         android:layout_marginTop="10dip"
  51.         android:text="Test Pop-Up" />
  52.  
  53. </LinearLayout>
  54.        
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57.     super.onCreate(savedInstanceState);
  58.     setContentView(R.layout.main);
  59.  
  60.     new Handler().postDelayed(new Runnable() {
  61.         public void run() {
  62.             showPopup();
  63.         }
  64.     }, 100);
  65. }
  66.  
  67.  
  68. public void showPopup(){
  69.     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  70.  
  71.     PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
  72.     pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
  73. }