- How to create Dialog on startup in android?
- public class PopupActivity extends Activity implements OnClickListener {
- LinearLayout ll = new LinearLayout(this);
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(ll);
- }
- protected void onStart()
- {
- super.onStart();
- final PopupWindow pw;
- Button button = new Button(this);
- button.setText("Hello");
- pw = new PopupWindow(button, 245, 284, true);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- pw.dismiss();
- }
- });
- pw.showAsDropDown(ll, 10, -50);
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent"
- android:orientation="vertical"
- android:id="@+id/layoutTemp">
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:padding="10dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#FFFFFF">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dip"
- android:text="Test Pop-Up" />
- </LinearLayout>
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- new Handler().postDelayed(new Runnable() {
- public void run() {
- showPopup();
- }
- }, 100);
- }
- public void showPopup(){
- LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
- pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
- }