PopUpWindow in Android
Android supports Popup Window like in IPhone. The difference between pop up window and dialog is we have to set the position (like center, top, bottom...) and also x and y positions. The below snippets explains the way.
Home.java:
package com.popup.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class Home extends Activity
{
private Button btnShowPopUp;
private PopupWindow mpopup;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowPopUp = (Button)findViewById(R.id.btnShowPopUp);
btnShowPopUp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
View popUpView = getLayoutInflater().inflate(R.layout.popup, null); // inflating popup layout
mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0); // Displaying popup
Button btnOk = (Button)popUpView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mpopup.dismiss(); //dismissing the popup
}
});
Button btnCancel = (Button)popUpView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mpopup.dismiss(); dismissing the popup
}
});
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class Home extends Activity
{
private Button btnShowPopUp;
private PopupWindow mpopup;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowPopUp = (Button)findViewById(R.id.btnShowPopUp);
btnShowPopUp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
View popUpView = getLayoutInflater().inflate(R.layout.popup, null); // inflating popup layout
mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0); // Displaying popup
Button btnOk = (Button)popUpView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mpopup.dismiss(); //dismissing the popup
}
});
Button btnCancel = (Button)popUpView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mpopup.dismiss(); dismissing the popup
}
});
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:id="@+id/btnShowPopUp"
android:text="ShowPopUp"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
popup.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:id="@+id/btnShowPopUp"
android:text="ShowPopUp"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
popup.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#90FFFFFF"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:text="PopUp Window"
android:textColor="#000000"
android:gravity="center"
android:textStyle="bold"
android:layout_height="wrap_content">
</TextView>
<Button
android:layout_width="fill_parent"
android:id="@+id/btnOk"
android:text="Ok"
android:layout_height="wrap_content">
</Button>
<Button
android:layout_width="fill_parent"
android:id="@+id/btnCancel"
android:text="Cancel"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Thanks
akm
cdacians
No comments:
Post a Comment