This is a simple android program to show a popup window. For this we need two xml files, one for the main window and other for the popup.
The source code for the java file is given below

package com.pack.pop;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
public class Pop extends Activity
{
/** Called when the activity is first created. */
Button ok;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ok = (Button) findViewById(R.id.but);
ok.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//this is the code for popup window

LayoutInflater inflater = (LayoutInflater) Pop.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here x is the name of the xml which contains the popup components
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.x,null, false),300,400,true);
//Here y is the id of the root component

pw.showAtLocation(findViewById(R.id.y), Gravity.CENTER, 0,0);
}
});
}
}


The Popup xml file is..

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget"
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="@drawable/pic"
android:text="This is my new PopUp Window "
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:typeface="sans"
android:textStyle="bold|italic"
android:layout_x="49px"
android:layout_y="319px"
>
</Button>
</AbsoluteLayout>

Output:

Incoming search terms:

  • Popup App Android
  • popup window android

Filed under: Android

Like this post? Subscribe to my RSS feed and get loads more!