Android - Prevent button click outside of PopupWindow - android

I've spent a while trying to get this to work, looked for similar solutions online, but none seem to work. I need my PopupWindow to only be dismissed on the click of the generate button, not by clicking outside the window. Anyone encountered this issue before?
private void LoadRAMSPopup() {
mainLayout.getForeground().setAlpha(150);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View ramsView = layoutInflater.inflate(R.layout.popup_rams, null);
final PopupWindow popupRAMS = new PopupWindow(
ramsView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
if (Build.VERSION.SDK_INT >= 21) {
popupRAMS.setElevation(5.0f);
}
findViewById(R.id.mainLayout).post(new Runnable() {
#Override
public void run() {
popupRAMS.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 0, 0);
popupRAMS.setOutsideTouchable(false);
popupRAMS.setFocusable(true);
popupRAMS.update();
Button btnGenerate = (Button) ramsView.findViewById(R.id.btnGenerate);
btnGenerate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), CreateRAMSActivity.class);
startActivity(intent);
popupRAMS.dismiss();
mainLayout.getForeground().setAlpha(0);
}
});
}
});
}

Setting up popupRAMS.setFocusable(false). removes unnecessary touch required to make popup window disappear. So please replace
popupRAMS.setFocusable(true);
with
popupRAMS.setFocusable(false);
Also try to add
popupRAMS.setOutsideTouchable(false);
Hope it will help you out.

Try this one
popupRAMS.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, android.R.color.transparent)));
popupRAMS.setOutsideTouchable(false);

I tried every other solution and the one that worked forme was setting "false" on the constructor
mDiscountPopUp = new PopupWindow(discountPopUpView, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT, false); // Creation of popup

Related

EditText cursor bug after dialog close

I'm facing a problem. Not sure what to call it, or what causes it
I'm learning Android SQLite and to train started making a simple note app.
The problem is I have a custom dialog for category select, before opening the dialog everything is fine in the EditText field, but after opening, and closing it the text starts writing over, like creating multiple layers of the same text and the text cursor leaves a line after every symbol. (See "bug demo" GIF of the problem)
Has anyone else seen something like this? What could be causing this, the dialog?
Edit:
So this is the code that takes action when clicking on the star to open the dialog
starred.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(CreateNoteActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_category_select, null);
ListView categoryList = mView.findViewById(R.id.category_list);
Button cancelSelect = mView.findViewById(R.id.cancelSelect);
final CategoryListAdapter adapter = new CategoryListAdapter(CreateNoteActivity.this);
categoryList.setAdapter(adapter);
//get the data and append to a list
Cursor data = myDB.getCategories();
while(data.moveToNext()){
Category thisNote = new Category(data.getInt(0), data.getString(1), data.getString(2));
adapter.add(thisNote);
}
categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
final Category selectedCategory = (Category) adapterView.getItemAtPosition(i);
int duration = Toast.LENGTH_SHORT;
String s = "Category celected: "+selectedCategory.getCategoryName();
Toast toast = Toast.makeText(context, s, duration);
toast.show();
}
});
builder.setView(mView);
final AlertDialog selectCategory = builder.create();
selectCategory.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
selectCategory.show();
View decorView = getWindow().getDecorView();
decorView.setBackgroundResource(android.R.color.transparent);
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.80);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.80);
selectCategory.getWindow().setLayout(width, height);
cancelSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectCategory.dismiss();
}
});
}
});
This answer might help you
Write this after the dialog close
ediText = findViewById(R.id.edit_text);
editText.setSelection(editText.getText().length);
Basically using the above logic, the cursor won't be pointed at the first character of the editText on the dialog close
Noticed that I was trying to set a transparent background to display my custom dialog bcg two times.
So what fixed the problem was removing two lines
*View decorView = getWindow().getDecorView();
decorView.setBackgroundResource(android.R.color.transparent);*
Not sure why it was causing this. Should check what is getDecorView() method. Used it cause found it as a solution to show the custom background.
This line works as well
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
Guess this was a case of rubber duck debugging - just had to tell someone about the problem to fix it. Thanks everyone.
Try this code when you dismiss or close the dialog,
edittext.setSelection(editText.getText().toString().trim().length);

How to show PopupWindow above keyboard?

I want to show a PopupWindow above virtual keyboard like Google Keep did when creating a reminder:
I believe, what you are looking for is combination of:
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
It's basically how native SearchView works:
https://android.googlesource.com/platform/frameworks/support/+/android-6.0.1_r31/v7/appcompat/src/android/support/v7/widget/SearchView.java#1695
which basically calls AutocompleteTextView.ensureImeVisible()
public void ensureImeVisible(boolean visible) {
mPopup.setInputMethodMode(visible
? ListPopupWindow.INPUT_METHOD_NEEDED : ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
if (mPopup.isDropDownAlwaysVisible() || (mFilter != null && enoughToFilter())) {
showDropDown();
}
}
This would resize Popup window to be shown exactly between Anchor and Keyboard.
use the following code. Change the location of the popup window in the method "showAtLocation".
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, true);
findViewById(R.id.activity_layout).post(new Runnable() {
#Override
public void run() {
pw.showAtLocation(findViewById(R.id.activity_layout), Gravity.CENTER, 0, 0); //set location here
}
});
To make a PopupWindow display above (on top of, or in front of) the on-screen keyboard, and make it dismiss when clicked outside, use this:
popupWindow = new PopupWindow(context);
popupWindow.setFocusable(true);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Sample code: https://github.com/lorensiuswlt/NewQuickAction3D
To make a ListPopupWindow display in front of the keyboard, and make it automatically close when clicked outside, use this:
listPopupWindow = new ListPopupWindow(context);
listPopupWindow.setModal(true);
listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
Sample code: Custom PopupMenu (layout)

POPUP window showing in android

i have created a button in android and when clicking it would show the popup window..but the code doesnot work like that..it has no errors but not showing popup window...please helpme..here is my code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout objrl = (RelativeLayout) findViewById(R.id.myrl);
final Button objButton = (Button) findViewById(R.id.mybutton);
objButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupWindow objPopupWindow = new PopupWindow(objrl, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
objPopupWindow.setAnimationStyle(R.drawable.background2);
objPopupWindow.showAtLocation(objButton, Gravity.CENTER_HORIZONTAL, 10, 10);
}
});
}
have you tried this : objPopupWindow.showAsDropDown(popupButton, 0, 0);
or try this http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html
PopupWindow popupWindowDogs = popupWindowDogs();
called below function where they want ::-
public PopupWindow popupWindowDogs() {
// initialize a pop up window type
PopupWindow popupWindow = new PopupWindow(this);
// the drop down list is a list view
ListView listViewDogs = new ListView(this);
// set our adapter and pass our pop up window contents
listViewDogs.setAdapter(dogsAdapter(popUpContents));
// set the item click listener
listViewDogs.setOnItemClickListener(new DogsDropdownOnItemClickListener());
// some other visual settings
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
popupWindow.setContentView(listViewDogs);
return popupWindow;
}
I've found some strange stuff in your codes
You've specified WRAP_CONTENT but haven't specified its content at all
Pass a drawable as animation style to the setAnimationStyle method.
In my opinion if you specify a valid animation style and a content view, It should appear.
I think you missed this code inside the OnClickListener
objPopupWindow.setContentView(objrl);

Show popup window from another class Android

I have an activity and a class that implements a popup window. Using this tutorial I implemented the popup. I call the methods init() and popupInit() from the activity and everything else is in the class. My problem is that the popup does not show.
Here is the popup class:
public class PopupAudio implements OnClickListener {
LinearLayout layoutOfPopup;
PopupWindow popupMessage;
Button popRecord, popStopRecord, popPlay, popStopPlaying;
TextView popupText;
public void popupInit() {
popRecord.setOnClickListener(this);
popStopRecord.setOnClickListener(this);
popPlay.setOnClickListener(this);
popStopPlaying.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupMessage.setContentView(layoutOfPopup);
}
public void init(Context context) {
popRecord = new Button(context);
popRecord.setId(112);
popStopRecord = new Button(context);
popPlay = new Button(context);
popStopPlaying = new Button(context);
layoutOfPopup = new LinearLayout(context);
popRecord.setText("REC");
layoutOfPopup.setOrientation(1);
layoutOfPopup.addView(popRecord);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case 112:
break;
}
}
}
It is a school project so it is very important. Please help me, I'll be grateful :)
You need to call a method to actually show the popup on some event action or whenever you need it. Here are the different methods from the docs
Here is one example of using showAtLocation().
showAsDropDown(View anchor) may be the simplest depending on your needs. Just pass it the view you want it to attach to. Though, the other two give you more flexibility on where it shows.

android.view.WindowManager$BadTokenException: - popup window on activity start

I have been hunting for the answer to this but most seem to point to getApplicationContext() issues but I am not using getApplicationContext().
Basically I am trying to have a popup window open on start up of a activity but when the activity starts it force closes with the above error.
The code im have writen is:
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.installguides_menu);
setTitleFromActivityLabel(R.id.title_text);
btn_Back = (Button) findViewById(R.id.btn_Back);
btn_Back.setOnClickListener (btn_Back_onClick);
btn_Ubuntu10Guide = (Button) findViewById(R.id.btn_Ubuntu10Guide);
btn_Ubuntu12Guide = (Button) findViewById(R.id.btn_Ubuntu12Guide);
btn_BacktrackGuide = (Button) findViewById(R.id.btn_BacktrackGuide);
btn_DebianGuide = (Button) findViewById(R.id.btn_DebianGuide);
btn_Ubuntu10Guide .setOnClickListener (btn_Ubuntu10Guide_onClick);
btn_Ubuntu12Guide .setOnClickListener (btn_Ubuntu12Guide_onClick);
btn_BacktrackGuide .setOnClickListener (btn_BacktrackGuide_onClick);
btn_DebianGuide .setOnClickListener (btn_DebianGuide_onClick);
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.donation_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(){
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btn_Ubuntu10Guide, 50, -30);
}
Hope you guys can help, been hunting for the answer for hours so your my last hope!
My first guess is that you are not allowed to show a pop-up inside onCreate. It is for initializing your application and constructing your layout, but you are forcing it to display a pop-up instead.
onStart() is a more suitable place to show it. Override this function and show your pop-up there. If you prefer to do it in onResume(), note that the user will see it not only when they launch the application but also when they come back to it after navigating away to another activity.
EDIT 2: Try this for creating the pop-up in onStart or onCreate:
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(findViewById(R.id.popup_layout), null, false),
100,
100,
true);
// delaying popup until after all application initialization is done
findViewById(R.id.main_page_layout).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.main_page_layout), Gravity.CENTER, 0, 0);
}
});
Add id's to your activity and pop-up layouts using
android:id="#+id/main_page_layout"
and
android:id="#+id/popup_layout"
respectively so that they match the id's you will use in the code above.

Categories

Resources