<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/poptext" android:layout_width="225px" android:layout_height="281px" android:background="#123456" android:text="This is my new PopUp Window,\n www.powenko.com" android:textSize="18sp" android:textColor="#ff000000" android:layout_x="53px" android:layout_y="44px"> </TextView> <Button android:id="@+id/widget41" android:layout_width="234px" android:layout_height="wrap_content" android:text="OK" android:textSize="26sp" android:layout_x="49px" android:layout_y="319px" > </Button> </AbsoluteLayout>
res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:id="@+id/lnparent"> <TableLayout android:layout_width="fill_parent" android:layout_weight=".7" android:id="@+id/tblntarialview" android:layout_height="wrap_content" android:gravity="center"> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TableRow android:layout_width="wrap_content" android:paddingTop="15dip" android:gravity="center" android:layout_height="wrap_content"> <TextView android:text="PowenKo.com" android:textColor="#ffffff" android:textSize="16dip" android:textStyle="bold" android:layout_width="fill_parent" android:id="@+id/txtmain" android:layout_height="wrap_content" android:layout_marginRight="5dip" /> </TableRow> </TableLayout> </LinearLayout>
package com.powenko.Tutorial_Dialog_PopupWindow; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.PopupWindow; public class Tutorial_Dialog_PopupWindowActivity extends Activity { private Handler mHandler = new Handler(); private PopupWindow pw; /** Called when the activity is first created. */ Button ok; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ok = (Button) findViewById(R.id.button1); ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //this is the code for popup window LayoutInflater inflater = (LayoutInflater) Tutorial_Dialog_PopupWindowActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Here x is the name of the xml which contains the popup components pw = new PopupWindow(inflater.inflate(R.layout.popuplayout,null, false),300,400,true); //Here y is the id of the root component pw.setAnimationStyle(android.R.style.Animation_Dialog); pw.showAtLocation(findViewById(R.id.lnparent), Gravity.CENTER, 0,0); mHandler.postDelayed(mUpdateTimeTask, 5000); } }); } private Runnable mUpdateTimeTask = new Runnable() { // class DismissPopup implements Runnable { public void run() { // Protect against null-pointer exceptions if (pw != null) { pw.dismiss(); } } }; }
sample code:
Tutorial_Dialog_PopupWindowActivity