About
Home > Tutorials Home > Android > PopupWindow Example in Android

PopupWindow Example in Android

Published on October 17, 2019 8:22:55 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Popup Window in Android".

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/show_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Show Popup" />

</RelativeLayout>

Step 2 : Add another xml File to your Project.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/popup"
    android:background="#ffffff"
    android:layout_gravity="center_vertical|center_horizontal"
    android:padding="5dp">
	
    <LinearLayout
        android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:padding="5dp"
		android:orientation="vertical"
		android:background="#ff00ff">
		
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

     <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

      <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

    <Button
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:id="@+id/ok"
        android:text="Ok"
        android:layout_gravity="center"/>
    </LinearLayout>

</LinearLayout>

Step 3 : Copy the Following Code to .java File.

package com.example.popupexample;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    private Button show;
    final Point p=new Point();

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        show=(Button)findViewById(R.id.show_popup);

        show.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v)
			{
				if(p!=null)
				{
					showpopupwindows(MainActivity.this,p);
				}
			}
		});
    }
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus)
	{
		int location[]=new int[2];	

		show.getLocationOnScreen(location);

		p.x=location[0];
		p.y=location[1];
	}

	private void showpopupwindows(final Activity context, Point p)
	{
		int popupwidth=320;
		int popupheight=240;

		LinearLayout viewgroup=(LinearLayout)context.findViewById(R.id.popup);
		LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

		View layout=inflater.inflate(R.layout.popupfile,viewgroup);

		final PopupWindow window=new PopupWindow(context);
		window.setContentView(layout);
		window.setWidth(popupwidth);
		window.setHeight(popupheight);
		window.setFocusable(true);

		int offset_x=0;
		int offset_y=50;

		//window.setBackgroundDrawable(new BitmapDrawable());

		window.showAtLocation(layout,Gravity.NO_GRAVITY,p.x+offset_x,p.y+offset_y);

		Button ok=(Button)layout.findViewById(R.id.ok);

		ok.setOnClickListener(new OnClickListener() {

			public void onClick(View v)
			{
				window.dismiss();
			}
		});
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile