Detect clicks outside of a View - android

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.

Related

Can Android Presentation handle touch events?

I know that Android Presentations can have their own layouts which means I can create UI components like buttons etc. However, does anyone know if it is possible for Presentations to handle touch events?
I have tried adding a button on the Presentation layout and registering a button onClickListener but it seems to not be working. Is there another way?
Here is my code:
In my Presentation class
mHelloToast = (Button) findViewById(R.id.btn_presentation_hello_toast);
mHelloToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getContext(), "hello presentation", Toast.LENGTH_SHORT );
toast.show();
}
});
Edit: bump
Doc says:
A presentation is a special kind of dialog whose purpose is to present content on a secondary display. A Presentation is associated with the target Display at creation time and configures its context and resource configuration according to the display's metrics.
So I think Android Presention can handle touche event as Dialog.Here I will show you how Dialog handles touch event.
Step 1
Create your custom layout res/layout/dialog_signin.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_presentation_hello_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 2
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Get the view which you want to receive touch event
//// Pass null as the parent view because its going in the dialog layout
View rootView = inflater.inflate(R.layout.dialog_signin, null);
Button bt = (Button)rootView.findViewById(R.id.btn_presentation_hello_toast);
// set click or touch listener
bt.setOnClickListener(....);
// set dialog's contents
builder.setView(rootView);

Android - DialogFragment blocking edit text focus in base activity

Good day.
I have an android application and I have a custom listView. Upon clicking the customListView I change the layout of the selected row (I change the last 2 column to editTexts), and I show a dialogFragment (which has an editText). Here is my code:
The onItemClickListener:
resultListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
//first I move the selected row to the top of the listView
resultListView.smoothScrollToPositionFromTop(pos, 0);
//then I disable selection of the listView to limit user action
resultListView.setScrollContainer(false);
resultListView.setClickable(false);
resultListView.setEnabled(false);
//I try to make the row still enabled and clickable
view.setClickable(true);
view.setEnabled(true);
//TODO - set clickstate = 0 is base/default state
for(SearchResultListViewItem row : results){
row.setClickState(1); //state 1 = dim
}
SearchResultListViewItem rowItem = results.get(pos);
rowItem.setClickState(2); //state 2 - clicked state
//in my CustomAdapter, I handle the clickState in the getView method
//basically, the change happens when click state is 2, the last 2 columns become editText
adapter.updateResults(results);
//I removed a lot of unrelated code in here
ItemDialog itemDialog = new ItemDialog();
//I set various variables here
itemDialog.show(getFragmentManager(), "");
};
});
This is the onCreateDialog Method. As you can see below, I set various properties such as setCancelable, setCancelOnTouchOutside, and window Flags for Modal.
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
//set width height here
int width = getResources().getDisplayMetrics().widthPixels * 4/5;
int height = getResources().getDisplayMetrics().heightPixels;
dialog.getWindow().setLayout(width, height);
//set other features here
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//make sure that the dialog persists through certain events
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
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);
//set window properties such as modal, dim, and position
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP|Gravity.LEFT);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 50;
params.y = y;
params.copyFrom(window.getAttributes());
window.setAttributes(params);
//edited out a lot of extra code
dialog.show();
return dialog;
}
And if necessary, here is the code in the getView method in my Custom BaseAdapter that handles when I change the clickState to 2:
//the if condition checks if the clickstate is < 2, if < 2, do the default and dim state
//click state of 2 means that I use the other row layout which has 2 editTexts
else{
convertView = layoutInflater.inflate(R.layout.search_result_inflate, null);
holder = new ViewHolder();
holder.itemView = (TextView) convertView.findViewById(R.id.dialogItemName);
holder.itemView.setText(listData.get(position).getItemName());
holder.itemView.setBackgroundColor(Color.parseColor("#66B2FF"));
holder.priceView = (TextView) convertView.findViewById(R.id.price);
holder.priceView.setText(listData.get(position).getPrice());
holder.priceView.setBackgroundColor(Color.parseColor("#66B2FF"));
holder.discountView = (TextView) convertView.findViewById(R.id.discount);
holder.discountView.setText(listData.get(position).getDiscount());
holder.discountView.setBackgroundColor(Color.parseColor("#66B2FF"));
holder.qtyIn = (EditText) convertView.findViewById(R.id.qtyInput);
holder.qtyIn.setText(listData.get(position).getQtyInput());
holder.qtyIn.setFocusable(true);
holder.qtyIn.setClickable(true);
holder.discIn = (EditText) convertView.findViewById(R.id.discInput);
holder.discIn.setText(listData.get(position).getDiscInput());
holder.discIn.setFocusable(true);
holder.discIn.setClickable(true);
return convertView;
}
As you can see, I tried to enable the necessary rows as much as possible. However, it seems that the DialogFragment is blocking it.
I also tried to comment out the
resultListView.setScrollContainer(false);
resultListView.setClickable(false);
resultListView.setEnabled(false);
block of code in my OnItemClickListener but to no avail.
The funny/interesting thing about this is that I am able to select the editTexts in the new row, I can see the focus shift, however, the soft keyboard does not appear at all. The soft keyboard only appears when I click the editText inside the DialogFragment. Even switching focus from the EditText in the dialog Fragment to the one in the list view doesn't enable me to put input inside the EditText in the rows.
If it's conflict between the EditText in the background/base activity (the one in the rows) and the EditText in the DialogFragment, then I hope there's a way to properly switch focus as I cannot resort to removing either of them.
Does anyone have an idea on how to work around this? Any help is very much appreciated, thank you.
Edit: I removed the EditText in the DialogFragment and it still doesn't work.

Upon button click, popup window to enter data?

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

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