Android: Dismissing one popup window when the next is called? - android

I have 3 Image buttons and on press they each have a Popup window that opens up. The issue is when i click on button 1, there is a popup, if I DONT dismiss that popup but click on Button 2 instead, the pop-up for Button 1 and Button 2 appear.
How do i dismiss any open Pop-up's when a new button is pressed?
Here is my code
final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
rredButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupright, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);
btnNxtScr.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
startActivity(myintent1);
}
});
popupWindow.showAsDropDown(rredButton, 1, -1);
}});
And here is the other button (with a similar popup method)
final ImageButton ryellowButton=(ImageButton)findViewById(R.id.RyellowButton);
ryellowButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
createWrongPop(arg0);
}});

To do this you'll need to save a reference to the PopupWindow in your class when the popup is shown, and clear it out when the popup is dismissed. Then, if you click the second button, you can check to see if the popup is available, and if it is, either call .dismiss() on it and create a new one, or modify it's contents.

Related

Android popup - passing arguments to event handlers

I have a NavigationView with menu.
When any menuitem is long pressed, I inflate a popup which gives the user various options/buttons. Each button has an event handler.
I need those handlers to know which menuitem originally triggered the popup.
What I am missing is the knowledge of how to pass that identity along the flow of objects.
In the posted code, the menuitem is longclicked, I pass menutem as a view to the popup constructor.
Yet no idea how to then pass it to the popup Button's event handler.
I am considering as an alternative, simply setting a variable with the id when the longpress occurs, and then reading that variable back again in the final handler.... but that does feel like a cheat and would require managing to ensure synchronising with events.
#Override
public boolean onLongClick(View v) {
pop(v);
return true;
}
public void pop(View v){
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View vw) {
Toast.makeText(getApplicationContext(), "id of menuitem here", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(v, 50, -30);
}
If I got your question right, you just want to pass the id of menuitem tapped on to pop method and later display that value in Toast.
In that case considering you can cast the id value into string, you can use setTag() and getTag():
#Override
public boolean onLongClick(View v)
{
v.setTag(place the id of menuitem tapped here);
pop(v);
return true;
}
public void pop(View v)
{
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View vw) {
Toast.makeText(getApplicationContext(), String.valueOf(v.getTag()), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(v, 50, -30);
}

How can block out and darken a view when i show a popup in android?

my problem by displaying a popup is that the view back is not blocked , I need block and obscure the view while the popup this over , I need the view can not be clicked , only the popup
My Popup:
View popupView;
PopupWindow popupWindow;
popupView = layoutInflater.inflate(R.layout.popup_ver_noconf_submateria_urgente, null);
popupWindow = new PopupWindow(popupView, RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
aceptar = (Button) popupView.findViewById(R.id.acept);
aceptar.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
sub.activity.startActivity(new Intent(sub.activity, VerNoConformidadesSubmateria.class));
popupWindow.dismiss();
releaseLayout();
}
});
PD: Sorry for the bad english

In Android how to call a popup window when a button in another custom popup window is clicked

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.

Set Text for pop up

I have a button with the onClick name of checkResult.
public void checkResult(View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(this);
View popupView = layoutInflater.inflate(R.layout.layout_result_popup, myRoot);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//what I want to show in the popup
TextView scorePopup = (TextView)findViewById(R.id.score_popup);
scorePopup.setText("Your score: " + score);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(checkResultButton, 100, -1200);
popupWindow.setFocusable(true);
popupWindow.update();
}
When checkResult button is clicked, layout_result_popup is displayed.
I directly put a button at main activity and use onClick to show popup. So far, I managed to show the popup text in XML. But when I try to set some text to it, my app crashed.
Help please. :)
Try to replace those 2 lines with this:
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView countView = (TextView)findViewById(R.id.count);
countView .setText("Count: " + count+ "/" + totalCount);
}
});
It very likely even works with the first line outside of the runOnUiThread

multiple popups in android

I have two Linear Layouts in single activity .
I want to display custom popups on each of them when clicked.
When I click on first layout, popup apperas,then on clicking on 2nd layout ,both popups are displayed. how can i have only single popup displayed at a time?
this is my code
workLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater=
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate
(R.layout.activity_your_places__work__popup, null);
updateTextView = (TextView)
popupView.findViewById(R.id.UpdateTextView);
updateTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Your_Places2Activity.this,
UpdateWorkAddressActivity.class);
startActivity(i);
}
});
deleteTextView = (TextView)
popupView.findViewById(R.id.DeleteTextView);
deleteTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
//code to delete address
}
});
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//dismiss other popup if is showing
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();}
//display popup
popupWindowWork.showAsDropDown(workLinearLayout, 0, -70);
}
});
i have done same thing on other linear layout
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
in this line, you are dismissing the popupWindowHome dialog if popupWindowHome is showing, with popupWindowHome being the NEW dialog. Move the if statement before the constructor call.
if(popupWindowHome != null && popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

Categories

Resources