Upon button click, popup window to enter data? - android

I have a button and when I press it I want a smaller window to popup (To where you can still see the previous screen, like a box inside a box). Inside that smaller popup box I want to be able to have the user input several things and when they hit OK it closes the popup window and adds the data back.
How would I go about doing this? Would I create another activity or maybe a dialog box? I tried creating another activity but couldn't figure out how to scale the size to make it smaller.
public void newUserInput(View view){
}
is the button

Declare popup window and popupView and the layout inflator.
private PopupWindow pw;
private View popupView;
private LayoutInflater inflater;
in the oncreate method inflate the layout you want to show as popup.
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.popup_layout, null, false);
and this will be your onclick method.
public void newUserInput(View view){
pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(false);
pw.setContentView(popupView);
// Use any one method - showAtLocation or showAsDropDown to show the popup
pw.showAtLocation(parent, gravity, x, y);
pw.showAsDropDown(anchor, xoff, yoff)
}

Related

Android - Floating Alert Dialog which still enables user input in the main layout/activity

Good day, apologies for the confusing title.
I am creating an android application and I need a dialog/pop up to appear at a specific X Y Position. I already have a working DialogFragmet as shown below:
public class ItemDialog extends DialogFragment implements OnClickListener{
//a lot of public and private variables here
public interface onSubmitListener {
void setOnSubmitListener(String qty, String disc, String instructions, UOMClass uom);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout2 = layoutInflater.inflate(R.layout.fragment_item_dialog, null);
dialog.setContentView(layout2);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
//snip snip more code
dialog.show();
//buttons - I set the buttons here
//editTexts - I set the edit texts here
//TextViews - I set the text views here
return dialog;
}
//more functions here
}
And I call it from my base activity as follows:
ItemDialog itemDialog = new ItemDialog();
//pass values from base activity to the ItemDialog here
itemDialog.show(getFragmentManager(), "");
However, I need to be able to see the base activity and input values in the edit texts in it. As of now, my DialogFragment covers a huge part of the screen and I'm unable to see the activity behind it. Clicking outside the Dialog Fragment cancels it as well.
I've searched on how to make an alert dialog float but to no avail.
Is there a way to display a Dialog Fragment over a certain area of the screen to allow the user to see and still input values in the base activity?
Any help is very much appreciated, thank you.
you can get window object and set it's layout parameters below code might help you.
Window window = getDialog().getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 300;
params.y = 100;
window.setAttributes(params);
for more info Position of DialogFragment in Android
you can also set background color or bitmap to dialog

how to create popup in oncreate method of service class

I am trying to create popup window on home screen when an floating image is clicked (using windowmanager).
so while using layoutinflater , I am not able to set a viewgroup in the second argument as findviewbyId is not recognised. so I kept null. like below.
LayoutInflater inflater = (LayoutInflater) Floater.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_for_floating_img,
null);
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
But when i click the floating image on home screen , it is giving below error message.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
so please suggest me the way forward to get the popup on click.
I am trying for popup like if we enable floating widget in CLEAN MASTER app and click on floating broom image will give popup.
Try this in your showPopup(...) function:
layout.post(new Runnable()
{
public void run()
{
popup.showAtLocation(layout, Gravity.NO_GRAVITY, OFFSET_X, OFFSET_Y);
}
});
where the layout is your popup. This is because you trying to show the popup before you even created the activity.

Detect clicks outside of a View

I have a view declared as such:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = layoutInflater.inflate(R.layout.background, null);
I want to detect clicks outside of myView, how can I do this?
So this background.xml is a small relativelayout that pops up when the user clicks on a button. It has an edit text so I do this to gain focus to it and allow to type on it:
response.setFocusable(true);
params.flags = params.flags & ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowManager.updateViewLayout(myView, params);
Now if the user clicks outside this pop up(background.xml aka myView) I want to set the param.flags back to FLAG.NOT_FOCUSABLE
You can create a dummy view and make it fill parent and add onClickListener and do what you want on onClick.

Issue dismissing popup window

I have a popup menu implemented , which shows up on click of a button. This is my onclick method.
public void showOverflow(View view) {
boolean click = true;
Button action = (Button) findViewById(R.id.btbAction);
LayoutInflater inflater = (LayoutInflater) main.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.overflow_layout, null);
final PopupWindow pw = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(true);
if (click) {
pw.showAsDropDown(action, 0, 0);
click = false;
} else {
pw.dismiss();
click = true;
}
}
The popup window shows up when the button is clicked. Now, the problem is that the window is not dismissed when i touch outside the popup window.
I tried setting this property to the popup window
pw.setOutsideTouchable(true);
Things remain the same. Please help me fix this
You should change the setOutsideTouchable call's parameter to true:
pw.setOutsideTouchable(false);
Controls whether the pop-up will be informed of touch events outside
of its window. This only makes sense for pop-ups that are touchable
but not focusable, which means touches outside of the window will be
delivered to the window behind. The default is false.
If the popup is showing, calling this method will take effect only the
next time the popup is shown or through a manual call to one of the
update() methods.
Parameters: touchable true if the popup should receive outside touch
events, false otherwise
On the other hand, what is the click local variable supposed to do? It is set to true, so it will always force the pw to pop up, whenever the showOverflow method is called, and for no reason it is set to false later, because it's life cycle ends as you leave that method.
Your code should look something like this:
private LayoutInflater inflater;
private Button action;
private PopupWindow pw;
private View popupView;
/*
* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.overflow_layout, null, false);
action = (Button) findViewById(R.id.action);
action.setOnClickListener(this);
}
public void showOverflow()
{
pw = new PopupWindow(getApplicationContext());
pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(true);
pw.setContentView(popupView);
pw.showAsDropDown(action, 0, 0);
}
The getApplicationContext() shoud be used in case you are inside an Activity class. Otherwise you should get the Context as a parameter.
change pw.setOutsideTouchable(true); to pw.setOutsideTouchable(false);
I know this is an old question but this is what I have done to fix it
The problem is:
You are creating a new instance of popupwindow everytime you call showOverFlow() thats why after you close the popupwindow another popup window will show
What will you do is initialize popupview in OnCreate
Then call popupwindow.showAsDropDown(view) in showOverFlow() method
And lastly you can check whether is it showing below code
Put this code in your button onclick
if(popupwindow.isShowing()){
popup.dismiss() }
else{
ShowOverflow()}

android create a popup screen for showing help on a page that extends view

I am creating an application which using custom view and i have designed the layout using a class that extends view.
Now i have a help icon on that view which have to popup on click.I have tried dialog window but i need a window without title and border.
I have checked some games and they are using what exactly i need. Anybody can suggest a better solution?
here is my sample code to get help button
public boolean onTouchEvent(MotionEvent me) {
int action = me.getAction();
if(action == MotionEvent.ACTION_DOWN ){
x = me.getX();
y = me.getY();
if (x >= helpButtonX && x < (helpButtonX +help.getWidth())&&
y >= helpButtonY && y < helpButtonY + help.getHeight() )
{
// code toshow popup
}
}
}
Yes you can create a custom dialog with the layout designed by you.
For that simply create a dialog and set the layout by using setContentView() method.
For example:
Dialog dialog = new Dialog(myActivity.this);
dialog.setContentView(R.layout.myDialogLayout);
dialog.setTitle("");
dialog.setCancelable(true);
dialog.show();
You can create a hidden View that is set using relativeLayout over the other elements in the layout.xml. when the user clicks the help button, the visibility is changed to visible and the View is shown. YOu can then set an onclick listener on the View that when they touch it, it will be hidden again.
You can just use a PopupWindow with custom layout.
Add this code in your {//Code to show popup}
//Get a layout inflator
LayoutInflater li = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
//Initialize a popup window with custom xml view - R.layout.popup.xml
View popupView = li.inflate(R.layout.popup, null);
final PopupWindow pW = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
To dismiss it use pW.dismiss() wherever you want
Try this: http://android-er.blogspot.jp/2012/03/example-of-using-popupwindow.html

Categories

Resources