PopupWindow cannot display Unity3D views in Android, but Dialog does? - android

Here is some brief codes of my project, in which I'd like to display a Unity3D model to do something.
public class MainActivity extends FragmentActivity{
protected UnityPlayer mUnityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
getWindow().setFormat(PixelFormat.RGBX_8888);
mUnityPlayer = new UnityPlayer(this);
...
mTestImageButton = ***;
mTestImageButton.setOnClickListener(mTestImageButtonClickListener);
}
private ImageButton mTestImageButton;
private View.OnClickListener mTestImageButtonClickListener = new
View.OnClickListener() {
#Override
public void onClick(View v) {
showUnity3D_dialog(v);
//showUnity3D_popupwindow(v);
}
};
private void showUnity3D_dialog(View v) {
Dialog dialog = new Dialog(v.getContext(), R.style.move_dialog);
dialog.setContentView(mUnityPlayer);
Window window = dialog.getWindow();
window.setWindowAnimations(R.style.move_dialog);
WindowManager.LayoutParams lp = window.getAttributes();
window.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
lp.width = 1179;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.setAttributes(lp);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
}
});
dialog.setCanceledOnTouchOutside(true);
dialog.show();
mUnityPlayer.resume();
}
private void showUnity3D_popupwindow(View v) {
PopupWindow pop = new PopupWindow(mUnityPlayer, 1000, 800);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(false);
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
}
});
pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
mUnityPlayer.resume();
}
}
As codes above, I tried to display a Unity3D-model in a PopupWindow, but the result makes me a bit confused, since there is nothing on screen when I called showUnity3D_popupwindow(v).
But when I called showUnity3D_dialog(v), the model by Unity3D shows.
I really can't understand what caused such scenario, and how can I display my model in a PopupWindow in android? Or, is it an impossible task?
I have my project under Android 4.3 & Unity 5.4.0.
Thanks a lot.

Looks like you are currently using the PopupWindow(View contentView, int width, int height) overload function. I've seen instances where functions that take View, Context or Activity does not work in Unity and you have to try other overloads.
You've tried the one takes View, now try the Context overload:
PopupWindow(Context context)
then set the width and height later on with the setWidth and setHeight function. You can get the current Context with UnityPlayer.currentActivity.getApplicationContext().
private void showUnity3D_popupwindow(View v)
{
Context mContext = UnityPlayer.currentActivity.getApplicationContext();
PopupWindow pop = new PopupWindow(mContext);
pop.setWidth(1000);
pop.setHeight(800);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(false);
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
}
});
pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
mUnityPlayer.resume();
}

Related

PopupWindow - not working on a few devices

I use following code to show a small popup:
public static PopupWindow showImportMenu(Activity activity, View anchor, PopupWindowClickListener onClickListener)
{
LayoutInflater inflater = LayoutInflater.from(activity);
PopupImportBinding binding = DataBindingUtil.inflate(inflater, R.layout.popup_import, null, false);
if (!RootTools.isRootAvailable())
binding.llImportRootMethod.setVisibility(View.GONE);
PopupWindow popupWindow = new PopupWindow(activity, null, R.attr.popupMenuStyle);
popupWindow.setFocusable(true);
popupWindow.setContentView(binding.getRoot());
popupWindow.setOutsideTouchable(true);
PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.BOTTOM);
View.OnClickListener clickListener = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
onClickListener.onClick(popupWindow, view);
}
};
binding.llImportDefault.setOnClickListener(clickListener);
binding.llImportRootMethod.setOnClickListener(clickListener);
binding.llImportHTCFromContacts.setOnClickListener(clickListener);
binding.llImportManual.setOnClickListener(clickListener);
return popupWindow;
}
This works on a lot of devices but on some rare devices it does not work, like:
Android 5.1.1 root slim rom
maybe others... until now, I don't know more about other devices
I got the feedback that no popup is shown. Does anyone know why this is not working on the above mentioned device? And what I can do to make it work on this device as well?
EDIT
It seems like it's not clear that what I want is following:
use showAsDropDown not showAtLocation or similar, I never saw this problem with showAtLocation yet
my solution is working on nearly all devices, it seems to be a phone/rom specific problem, maybe it's not even solvable as it COULD be a bug in the device as well => if someone knows of such a bug, telling me would be fine as well
I don't want to use a dialog (or anything else) instead, that's not answering my question. I currently use a BottomSheet which is fine for me, but still I would like to know if the problem can be solved and somehow handled
I got the same problem on a Nexus 7 (not 2012) running the 5.1.1. It is finally fixed by adding this line:
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I was having the same problem: my PopupWindow was not showing on my 5.1.1 android device but it was on others. I realised that I had to specify the width and height in order to be shown on that version (which is still compatible with the rest of the versions as well).
Here is an example:
popUp.setWidth(MATCH_PARENT);
popUp.setHeight(WRAP_CONTENT);
In my case, popup window have no sizes on few devices.
Try that after setContentView
50000 - just a big size for measure.
popupWindow.getContentView().measure(50000, 50000);
popupWindow.setWidth(popupWindow.getContentView().getMeasuredWidth());
popupWindow.setHeight(popupWindow.getContentView().getMeasuredHeight());
You can use screen size instead 50000
Some ROMs restrict usage of popupview with their own permissions. So user have to explicitly turn on permission to show pop up views.
Even MIUI will restrict popupview to be displayed by default.
Please have a look whether there is any permission in that ROMs or devices.
Try QuickAction
Activity (ExampleActivity1.java) to show how to use QuickAction:
public class Example1Activity extends Activity {
private static final int ID_ADD = 1;
private static final int ID_ACCEPT = 2;
private static final int ID_UPLOAD = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example1);
ActionItem addItem = new ActionItem(ID_ADD, "Add", getResources().getDrawable(R.drawable.ic_add));
ActionItem acceptItem = new ActionItem(ID_ACCEPT, "Accept", getResources().getDrawable(R.drawable.ic_accept));
ActionItem uploadItem = new ActionItem(ID_UPLOAD, "Upload", getResources().getDrawable(R.drawable.ic_up));
//use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
uploadItem.setSticky(true);
final QuickAction mQuickAction = new QuickAction(this);
mQuickAction.addActionItem(addItem);
mQuickAction.addActionItem(acceptItem);
mQuickAction.addActionItem(uploadItem);
//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
#Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
if (actionId == ID_ADD) {
Toast.makeText(getApplicationContext(), "Add item selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
}
}
});
mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
#Override
public void onDismiss() {
Toast.makeText(getApplicationContext(), "Ups..dismissed", Toast.LENGTH_SHORT).show();
}
});
Button btn1 = (Button) this.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mQuickAction.show(v);
}
})
Button btn2 = (Button) this.findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mQuickAction.show(v);
mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
}
});
}
}
Output:
(source: unitid.nl)
## Ok i implemented popupwindow for sorting in my tab fragment and i checked working fine once try this
I used in that custom layout for popup window
final PopupWindow popupWindow = new PopupWindow(getActivity());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popmenu1t1, null);
l8[![enter image description here][1]][1] = (LinearLayout) view.findViewById(R.id.atoz);
l8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
date_sort="0";
discount_sort="";
price_sort="";
alpha_sort="";
popupWindow.dismiss();
}
});
int width = 900;
int height = 400;
try {
WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
} catch (Exception e) {
e.printStackTrace();
}
popupWindow.setWidth(width*3/6);
popupWindow.setFocusable(true);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
Find the below attached screen shot popupwindow in my app
I had created a custom popup dialog shows at bottom of screen
public class MoreOptionDialog {
private Dialog dialog;
private Context context;
private int size;
public MoreOptionDialog(Context context) {
this.context = context;
}
public void showMoreOptionDialog(List<String> listMoreOption) {
dialog = new Dialog(new ContextThemeWrapper(context, R.style.DialogSlideAnim));
dialog.getWindow().setWindowAnimations(R.style.DialogSlideAnim);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = View.inflate(context, R.layout.dialog_more_option, null);
dialog.setContentView(view, new LinearLayout.LayoutParams(utility.getScreenWidth() - 100, size));
dialog.getWindow().setGravity(Gravity.BOTTOM);
ListView listView = (ListView) view.findViewById(R.id.lvMoreOption);
MoreOptionAdapter moreOptionAdapter = new MoreOptionAdapter(context, listMoreOption, Gravity.CENTER);
listView.setAdapter(moreOptionAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
dialog.dismiss();
}
});
dialog.show();
}
}
And style is here
<style name="DialogSlideAnim">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
<item name="android:windowBackground">#color/color_white</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
And this dialog working in all devices :)

Android - Popup window is not closing

I want to close the popup window when I click a button, but it seems dismiss function doesn't work and the window is not closing. What did I wrong?
(I'm a beginner, so codes might be 'weird'. Please understand...)
public class AlarmPopup extends Activity {
private PopupWindow popup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onShowPopup();
}
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
final PopupWindow popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
#Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
findViewById(R.id.button).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) { // dismiss and stop the alarm function on other class
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i); // this function is working...
popup.dismiss();
}
};
}
You have declared popup as global and inside your onShowPopup you are creating new object for popup so that local popup will never be accessible from listener so make the changes as below:
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
#Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
view.findViewById(R.id.button).setOnClickListener(mClickListener);
}
Popup variable that you are using to dismiss your popup window has not been initialized in the code that you have posted. Your final variable that you have created inside method is local and will not be accessible outside that method.
So initialize your variable or use same variable inside method too.

BadTokenException: Unable to add window -- token null is not valid; is your activity running? in activity.isFinishing statment

I have a popUpWindow that need to by displayed, after couple of seconds if the user is still on current activity. I implemented stament that check if the activity isnt finished/destroyed and then display the Popup, and it works fine, for weekend users :) ( slowly clicking from activity to activity) but in high pressuretests ( activities are recreating, finished, fast move form activity to activity) that gives me that error :
E/UncaughtException: android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not valid; is your activity
running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:598)
at
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:341)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1040)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1003)
at com.guides4art.app.ImageSlider.RatePopUp$3.run(RatePopUp.java:86)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
code:
private void showPopUpWindow(final Activity context){
popupWindow = new PopupWindow(context);
LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(layoutParams.height);
popupWindow.setWidth(layoutParams.width);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setContentView(view);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if(context instanceof CarSale) {
((CarSale) context).saveRate((int) rating);
((CarSale) context).initRate();
title.setText(""+context.getString(R.string.thanksForRate));
}
else
Log.i("kamil","error");
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
if(!context.isFinishing() || !context.isDestroyed() )
activityView.post(new Runnable() {
#Override
public void run() {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
});
}
//View Pager Class
#Override
public void onPageSelected(int position) {
if(viewPager !=null){
this.position=position;
if(position==carList.size()-1 && isRated() && showRateBar)
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
new RatePopUp(Cars.this,activityView);
showRateBar=false;
}
},5*SECOND);
//RatePopUp constructor
public RatePopUp(Activity context,View activityView){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rate_popup_layout, null);
this.activityView=activityView;
ratingBar = (RatingBar) view.findViewById(R.id.ratingPop);
title= (TextView)view.findViewById(R.id.rateTitle);
title.setText(context.getString(R.string.rate_exhibition));
closeButton = (Button)view.findViewById(R.id.close_button);
Typeface typeface =Typeface.createFromAsset(context.getAssets(),"fonts/fontawesome-webfont.ttf");
closeButton.setTypeface(typeface);
closeButton.setText(context.getString(R.string.exitIcon));
showPopUpWindow(context);
}
try this code:
new Handler().postDelayed(new Runnable(){
public void run() {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
}, 200L);
instead of:
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
Also make sure you pass ActivityName.this as context.. not getApplicationContext()
Try replace your runnable code in showPopUpWindow() with this:
runOnUiThread(new Runnable() {
#Override
public void run() {
if (!isFinishing()) {
popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
}
});
Instead of
if(!context.isFinishing())
try
if(!((Activity) context).isFinishing())
Worked fine for me.
When using popup window you should make should two states:
Activity is alive that the activity is not destroyed and finished
make should the activity is created or the parent view you pass has attached to window
The example code as followed:
val decorView = ctx.window.decorView
decorView.post {
val posArr = IntArray(2)
anchorView.getLocationInWindow(posArr)
val yOffset = posArr[1] - anchorView.height + verticalOffset
if (isActivityAlive(ctx) && decorView.isAttachedToWindow) {
pop.showAtLocation(decorView, Gravity.TOP, 0, yOffset)
}
}
private fun isActivityAlive(ctx: Activity?): Boolean {
return ctx != null && !ctx.isDestroyed && !ctx.isFinishing
}
Hope this will help you...
Replace:
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
with:
new Handler().postDelayed(new Runnable(){
public void run() {
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}, 100L);

Can't get transparent DialogFragment

I have a dialog Fragment which look like that.
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
This code get background semi transparent. But I still got a white part on the bottom. I want to get rid of the white to just have semi transparent background
I already tried a lot of stuff that I saw in other posts.
I don't know what is the object that I must change between the DialogFragment, the AlertDialog and the LinearLayout.
It may not be the LinearLayout, because when I increase margin, nothing is moving.
Here is my code :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// setStyle(DialogFragment.STYLE_NORMAL, 0);
// setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
// setStyle(STYLE_NO_FRAME, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(
R.layout.share_or_die, null);
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
ad.setCanceledOnTouchOutside(true);
ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return ad;
}
I just call it in the mainActivity when user click back button:
#Override
public void onBackPressed() {
if (isUserConnected && !hasShared) {
shareOnExitDialog = new ShareOnExitDialog();
shareOnExitDialog.setCancelable(true);
shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
} else {
finish();
}
}
Any help would be appreciated !
The issue is in default dialog theme. Based on this and this answers it's much easier to achieve your target.
The Activity should be like the following:
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
#Override
public void onBackPressed() {
MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
}
}
And fragment:
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
/**
* Create a new instance of MyDialogFragment, providing "title"
* as an argument.
*/
static MyDialogFragment newInstance(String title) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);
final Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
Also, be sure to do the following: before setContentView() in the activity, getWindow().requestFeature(Window.FEATURE_NO_TITLE) should be added in order to use *NoTitleBar style.
The result is (I've used LinearLayout with single TextView inside):
I hope it can help you
#Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}
None of the above solutions worked for me. What i found to work was to make sure that I had imported android.support.v7.app.AlertDialog instead of the generic app.alertdialog.
Hope this helps others too.
It works for me:
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Return a value from Popup Window

I have popup window class like the following.
public class Popup extends PopupWindow {
Context context;
EditText et_bankname, et_banknumber;
String bank_name, account_number;
public Popup(Context ctx) {
super(ctx);
context = ctx;
setContentView(LayoutInflater.from(context).inflate(R.layout.bank_details, null));
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
View popupView = getContentView();
setFocusable(true);
Button btn_close = (Button) popupView.findViewById(R.id.popupClose);
Button btn_submit = (Button) popupView.findViewById(R.id.popupSave);
et_bankname = (EditText) popupView.findViewById(R.id.bank_name);
et_banknumber = (EditText) popupView.findViewById(R.id.bankacc_no);
btn_submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
bank_name = et_bankname.getText().toString();
account_number = et_banknumber.getText().toString();
if (!bank_name.equals("") && !account_number.equals("")) {
Toast t1 = Toast.makeText(context,bank_name + " "+account_number , Toast.LENGTH_SHORT);
t1.setGravity(Gravity.TOP, 0, 100);
t1.show();
dismiss();
} else {
Toast t1 = Toast.makeText(context,
"Please provide valid details", Toast.LENGTH_SHORT);
t1.setGravity(Gravity.TOP, 0, 100);
t1.show();
}
}
});
}
public void show(View v) {
showAtLocation(v, Gravity.CENTER, 0, 0);
}
}
I will access this popup in my activity by
Popup popup = new Popup(getBaseContext());
popup.show(arg1);
This works perfectly. But I want to know when this popup window gets dismissed. for this purpose now I am using Thread concept like following.
if (isPopupShowing) {
Thread thread = new Thread() {
#Override
public void run() {
while (isPopupShowing) {
if (!popup.isShowing()) {
isPopupShowing = false;
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
loadDSP(type);
}
});
}
}
}
};
thread.start();
}
But this thread will run continuesly until the popup window gets dismissed. So I feel it is better to replace this solution by any other way.
What I want?
Just intimate to my activity like "popup is closed" when popup is dismissed.
Why I am use this way?
I will use this popup window in three activty. That is why I am create a separate class for popup window.
Any help will be highly appriciated.
Thank you.
PopupWindow already has its own dismiss listener, just use it as follows
Popup popup = new Popup(getBaseContext());
popup.show(arg1);
Change that to
Popup popup = new Popup(getBaseContext());
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
// Do your action
}
});
popup.show(arg1);
EDIT
I hadn't noticed you were extending a PopupWindow, which already has this implemented as shown in #Jayabal's answer. Anyway this is how you would do it if the PopupWindow didn't already have it's own onDismissListener.
Simply create your own OnDismissListener Interface.
public interface onDismissListener{
public void onDismiss();
}
Add a reference to a Listener in the PopUp class and a setter.
OnDismissListener listener;
public void setOnDismissListener(OnDismissListener listener){
this.listener = listener;
}
Then in your Activity
Popup popup = new Popup(getBaseContext());
popup.setOnDismissListener(new OnDismissListener(){
public void onDismiss(){
//do what you need to here
}
});
popup.show(arg1);
This pattern should be familiar to you, its used everywhere in Android.

Categories

Resources