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);
}
});
}
Related
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.
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 :)
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.
On clicking a button in my main screen, I have a dialog box opening.
On clicking a button in the dialog box, a pop-up screen opens.The following is the code. This part works fine.
final Dialog dialog = new Dialog(this,R.style.FullHeightDialog);
// dialog box layout is layout.xml
dialog.setContentView(R.layout.layout);
//layout of pop-up window is howtoplay.xml
final LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.howtoplay, null, false);
final PopupWindow pw = new PopupWindow(popupView,400,440, true);
//On clicking button in dialog,popup opens
Button howto = (Button) dialog.findViewById(R.id.rules);
howto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.setContentView(inflater.inflate(R.layout.howtoplay, null, false));
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
pw.update(0,0,wth*8,ht*10);
Button close = (Button)pw.getContentView().findViewById(R.id.done);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
});
}
});
I want to open a pop-up screen(playvideo.xml) when a button in the howtoplay pop-up screen is clicked. The following is the code I've written in the onclicklistener method of the button.
public void howtoplayvideo(View v){
System.out.println("------clicked-------");
final LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.playvodeo, null, false);
final PopupWindow pw = new PopupWindow(popupView,400,440, true);
pw.setContentView(inflater.inflate(R.layout.playvideo, null, false));
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
pw.update(0,0,wth*8,ht*10);
}
"clicked" is getting printed, but then am getting an exception
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#453afa80 is not valid; is your activity running?
Please help me!!
This might be too late but the reason why your code is not working is because the you are saying:
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
The v you have passed in is the view for the howto button and not for your layout view that you want the popupwindow to show on top of.
After finding that starting a new intent might not be the right way to notify user of GameOver, I'm now struggeling with runOnUiThread, Runnable and dialogs..
Question : How and where would I implement a Dialog.show() to notify the user that the game has ended? I am implementating a Rail race Game .In this when trains collision occur game over i want to show dialog here.
I've experimented with runOnUiThread, Runnable and Async threads. Can't figure it out.
I want to show dialog in collision occur method of MainGamePanel.
I have a game. Activity AndroidGameActivity :
private static final String TAG = AndroidGame.class.getSimpleName();
public static Vibrator v;
private GameDatabaseOperations gameDatabaseOperations;
private int currentDate;
private int todayDate;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
try{
currentDate = Calendar.getInstance().get(Calendar.DATE);
gameDatabaseOperations = new GameDatabaseOperations(this);
gameDatabaseOperations.createWriteModeDatabase();
Cursor crDate = gameDatabaseOperations.fetchTodayDate();
startManagingCursor(crDate);
if(crDate!= null && crDate.getCount() >0){
crDate.moveToFirst();
todayDate = crDate.getInt(0);
}
if(currentDate != todayDate ){
gameDatabaseOperations.updateTodayDefaultScore(0);
gameDatabaseOperations.updateTodayDate(currentDate);
Cursor cr1Date = gameDatabaseOperations.fetchTodayDate();
startManagingCursor(cr1Date);
if(cr1Date!= null && cr1Date.getCount() >0){
cr1Date.moveToFirst();
todayDate = cr1Date.getInt(0);
}
}
gameDatabaseOperations.closeDatabase();
}
catch(Exception e){
e.printStackTrace();
}
FrameLayout fl = new FrameLayout(this);
setContentView(fl);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout pause = (LinearLayout) inflater.inflate(
R.layout.pausebutton, null);
fl.addView(new MainGamePanel(this));
fl.addView(pause);
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
have you thought of implementing your game's engine like this:
while (!collision)
{
playGame();
doAnimation();
checkCollision();
}
dialog.show();
performAction(determineNextGameState());
putting all the game play code in the playGame() and then modifying collision field when the user dies?