Reputation: 400
I don't want to button for closing pop-up window in Android. I just press the screen than pop-up should close. It works only button click If I click close button then it close but I don't want any button in my popup window I just want click anywhere in the screen then it should be close.
MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn1, btn2;
private QuickAction quick;
private PopupWindow window;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(btn1_Click);
btn2.setOnClickListener(btn2_Click);
ActionItem itm1 = new ActionItem(1, "Item1", null);
ActionItem itm2 = new ActionItem(2, "Item2", null);
quick = new QuickAction(this, QuickAction.VERTICAL);
quick.addActionItem(itm1);
quick.addActionItem(itm2);
quick.setOnActionItemClickListener(quick_Clicked);
}
private OnClickListener btn1_Click = new OnClickListener() {
@Override
public void onClick(View v) {
quick.show(v);
}
};
private OnClickListener btn2_Click = new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup, null);
window = new PopupWindow(layout, 500, 400, true);
window.showAtLocation(layout,17, 0, 0);
Button btn = (Button) layout.findViewById(R.id.buttonclose);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
window.dismiss();
}
});
}
};
private QuickAction.OnActionItemClickListener quick_Clicked = new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction source, int pos, int actionId) {
if (actionId == 1) {
Toast.makeText(MainActivity.this, "Item 1 was clicked", Toast.LENGTH_LONG).show();
} else if (actionId == 2) {
Toast.makeText(MainActivity.this, "Item 2 was clicked", Toast.LENGTH_LONG).show();
}
}
};
}
Here this is my popup. What kind of changes is enough for me. Should I changes popup class?
public class PopupWindows {
protected Context mContext;
public PopupWindow mWindow;
protected View mRootView;
protected Drawable mBackground = null;
protected WindowManager mWindowManager;
/**
* Constructor.
*
* @param context Context
*/
public PopupWindows(Context context) {
mContext = context;
mWindow = new PopupWindow(context);
mWindow.setBackgroundDrawable(new BitmapDrawable());
mWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
mWindow.dismiss();
return true;
}
return false;
}
});
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
/**
* On dismiss
*/
protected void onDismiss() {
}
/**
* On show
*/
protected void onShow() {
}
/**
* On pre show
*/
protected void preShow() {
if (mRootView == null)
throw new IllegalStateException("setContentView was not called with a view to display.");
onShow();
// if (mBackground == null)
// mWindow.setBackgroundDrawable(new BitmapDrawable());
//else
// mWindow.setBackgroundDrawable(new BitmapDrawable());
mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// mWindow.setTouchable(true);
// mWindow.setFocusable(true);
// mWindow.setOutsideTouchable(true);
mWindow.setContentView(mRootView);
}
/**
* Set background drawable.
*
* @param background Background drawable
*/
public void setBackgroundDrawable(Drawable background) {
mWindow.setBackgroundDrawable(background);
}
/**
* Set content view.
*
* @param root Root view
*/
public void setContentView(View root) {
mRootView = root;
mWindow.setContentView(root);
}
/**
* Set content view.
*
* @param layoutResID Resource id
*/
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(inflator.inflate(layoutResID, null));
}
/**
* Set listener on window dismissed.
*
* @param listener
*/
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
mWindow.setOnDismissListener(listener);
}
/**
* Dismiss the popup window.
*/
public void dismiss() {
mWindow.dismiss();
}
Popup.xml:
It looks ok ı will delete close button. If ı succes to close without close button for my popupwindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Black"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="500dp"
android:layout_height="1000dp"
android:layout_weight="0.09"
android:src="@drawable/karisikwaffle" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#33b5e5"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a popupWindow"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/light_gray" />
<Button
android:id="@+id/buttonclose"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:text="Close" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 10726
Reputation: 869
The accepted answer actually introduces a bug that intercepts your click listener if your popup has a list view. This means that clicking on the listview/expandablelistview will dismiss the dialog. You actually don't need to set the touch interceptor.
So the code snippet would look like:
window.setBackgroundDrawable(new BitmapDrawable());
window.setOutsideTouchable(true);
that's it!
if you still wanted to have the touch intercepted (no reason), then you should do something like this:
window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
window.dismiss();
boolean consumedEvent = true;
if (view.hasOnClickListeners()) {
consumedEvent = view.onTouchEvent(event);
}
return consumedEvent;
}
return false;
}
});
Upvotes: 4
Reputation: 383
window.setTouchable(true);
window.setTouchInterceptor(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
window.dismiss();
return true;
}
return false;
}
});
BUNU window.showAtLocation in Altina koy.
Upvotes: -1
Reputation: 383
private OnClickListener btn2_Click = new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup, null);
window = new PopupWindow(layout, 500, 400, true);
// window.setTouchable(true);
// window.setOutsideTouchable(true);
window.setBackgroundDrawable (new BitmapDrawable());
// window.setFocusable(false);
window.setOutsideTouchable(true);
window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
window.dismiss();
return true;
}
return false;
}
});
window.showAtLocation(layout,17, 0, 0);
Button btn = (Button) layout.findViewById(R.id.buttonclose);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
window.dismiss();
}
});
}
};
Upvotes: 3