Show PopupWindow 3 seconds after Activity is created and last 3 seconds - android

I have popup window in activity. What I want is that this popup starts after 3 seconds when activity is created and last for 3 seconds. Any help please?
here is my code:
try {
LayoutInflater inflater1 = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate the view from a predefined XML layout
View layout = inflater1.inflate(R.layout.activity_pop_up,
(ViewGroup) findViewById(R.id.relativeLayoutZaFragment));
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 300, 470, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}

Try this
boolean isShowing=false;
In onCreate
CountDownTimer timer=new CountDownTimer(3000,1000) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
if(isShowing){
//CLOSE
}
else{
isShowing=true;
LayoutInflater inflater1 = (LayoutInflater)
MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater1.inflate(R.layout.activity_pop_up,
(ViewGroup) findViewById(R.id.relativeLayoutZaFragment));
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 300, 470, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0)
timer.start();
}
}
};
timer.start();

That can be easily implemented with posting an event on Handler.
private final Handler handler = new Handler(Looper.getMainLooper());
private PopupWindow popupWindow;
private final Runnable dismissPopupRunnable = new Runnable() {
#Override
public void run() {
// dismiss popupWindow
}
};
private final Runnable showPopupRunnable = new Runnable() {
#Override
public void run() {
// show popupWindow
handler.postDelayed(dismissPopupRunnable, 3000);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize popupWindow here
handler.postDelayed(showPopupRunnable, 3000);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(showPopupRunnable);
handler.removeCallbacks(dismissPopupRunnable);
}
Note, you have to take care of removing callbacks from handler, when activity is being paused.

Related

Error inflating class com.lamudi.phonefield.PhoneInputLayout in popupwindow

In my android app, I used compile ('com.lamudi.phonefield:phone-field:0.1.3#aar') library it will perfectly worked on activity layout. Problem is that it will not open in popup window i got error regarding xml layout.
XML layout
<com.lamudi.phonefield.PhoneInputLayout
android:id="#+id/phone_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java source code
public class PopupActivity extends Activity {
private Context mContext;
private Activity mActivity;
private android.widget.PopupWindow mPopupWindow;
private LinearLayout mBirthdetail;
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup);
mContext = getApplicationContext();
// Get the activity
mActivity = PopupActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.raw_birthdate, null);
mBirthdetail = (LinearLayout) findViewById(R.id.birth_detail);
mPopupWindow = new android.widget.PopupWindow(
customView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
// mPopupWindow.setContentView(findViewById(R.id.demo));
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
if (Build.VERSION.SDK_INT >= 21) {
mPopupWindow.setElevation(5.0f);
}
mPopupWindow.setFocusable(true);
mPopupWindow.update();
mPopupWindow.setOutsideTouchable(false);
final PhoneInputLayout phoneInputLayout = (PhoneInputLayout) customView.findViewById(R.id.phone_input_layout);
phoneInputLayout.setHint(R.string.phone_hint);
phoneInputLayout.setDefaultCountry("DE");
String phoneNumber = phoneInputLayout.getPhoneNumber();
new Handler().postDelayed(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(mBirthdetail, Gravity.CENTER, 0, 0);
}
}, 100L);
}
#Override
protected void onStop() {
super.onStop();
mPopupWindow.dismiss();
}
}

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);

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

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();
}

android popup window call from oncreate

private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.loading_dialog, null);
PopupWindow windows = new PopupWindow(layout , 300,300,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
windows.showAtLocation(layout,Gravity.CENTER, 0, 0);
}
when invoke the method loadingPopup() from oncreate() an exception accrued .. please can you help me
You are trying to show the pop-up window even before the activity window has been displayed.
With the help of post method we can wait until all necessary start up life cycle methods get completed.
Try this :
private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.loading_dialog, null);
final PopupWindow windows = new PopupWindow(layout , 300,300,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout,Gravity.CENTER, 0, 0);
}
});
}

Categories

Resources