I want to make toast click able or a UI element which will have clickable button but behaves like toast.
It should not run on UI thread like toast.
It should not halt or overlay current user activity, message should come like toast with clickable button and vanish but as toast user should be able to access background ongoing UI item.
If any one has any idea about how to achieve this pls share with me.
Little trick. Tested working Android 4.4
toast = new Toast(context);
try {
Class<?> clazz = Class.forName("android.widget.Toast");
Method method = clazz.getDeclaredMethod("getWindowParams");
WindowManager.LayoutParams param = (WindowManager.LayoutParams) method.invoke(toast);
param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
} catch (Exception e) {
e.printStackTrace();
}
The Gmail undo bar is the best suitable for you, its just like a toast with a button.
Here is a code implementation for it.
http://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java
I had a similar requirement that I solved using a PopupWindow. Basically, I had an about window with a clickable link that I wanted displayed like a toast. The popup window can accomplish this as follows:
In the parent class, I use the following flag:
private boolean durationExpired = false;
Then, when I invoke what would have been the toast, I do the following instead:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.about_hiittimer,
(ViewGroup) findViewById(R.id.about_hiittimer));
TextView url = (TextView) layout.findViewById(R.id.url);
url.setMovementMethod(LinkMovementMethod.getInstance());
final PopupWindow popupWindow = new PopupWindow(layout, 280, 160, false);
popupWindow.showAtLocation(layout, 17, 0, 0);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
final Handler popupHandler = new Handler();
runOnUiThread(new Runnable() {
#Override
public void run() {
if (!durationExpired) {
durationExpired = true;
popupHandler.postDelayed(this, 2000);
} else {
popupWindow.dismiss();
popupHandler.removeCallbacks(this);
durationExpired = false;
}
}
});
Put your main layout inside a FrameLayout. Write a layout for your toast, with the button and all, insert it into the FrameLayout (below your main layout) and set its visibility to GONE.
When you show it (setting visibility to VISIBLE) start a new thread that counts down the seconds till its dismissed. Set it back to invisible from the thread via a Handler (cause all UI elements can only be managed from the main thread).
cheers!
Related
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)
I'm trying to instantiate a Progress Bar from the main Activity and add it to the view dynamically. When I do this, I get a bar:
ProgressBar bar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(params);
bar.setId(mInt);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(bar);
I started a new project that only includes the above code, and the progress bar displays correctly.
If I add the following lines to the above code, the screen is blank for a second and then shows a half full bar. I would expect to see an empty bar for a second and then it goes to 50%.
try {
Thread.sleep(2000);
} catch ...{
}
bar.setProgress(50);
If I add some code like this, however, the bar and changes display correctly.
Button b = new Button(this);
b.setText("change bar");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
changeProg();
}
});
public void changeProg() {
switch (state) {
case 0: bar.setProgress(100); state = 1; break;
case 1: bar.setProgress(127); state = 2; break;
case 2: bar.setProgress(33); state = 0; break;
}
}
If I try to automate the process with a loop to call changeProg() every so often, the screen just stays blank - no widgets at all display, whether they are described by the XML or pro grammatically makes no difference.
I'm having trouble understanding why this behavior exists.
It seems that you call Thread.sleep(2000) from UI thread, which causes the problem, even create ANR. Try to create a new thread (separate thread) to change the progress value.
See the example below:
https://stackoverflow.com/a/17758416/3922207
ProgressBar pdialog = new ProgressBar(context,null, android.R.attr.yourcustomstyle);
and check your apptheme in style
SetProgress tip:
Set the current progress to the specified value. Does not do anything if the progress bar is in indeterminate mode.
So you have to call:
bar.setIndeterminate(false);
Have you tried looking at this site?
http://www.mkyong.com/android/android-progress-bar-example/
The correct way is use 1 thread to run your time consuming task and another thread to update the progress bar status
Hope this helps!
I have implemented ontouchlistener in my service class and trying to get the touch event which is working well with gingerbread but it is not working with higher than icreamsandwich. I am trying a lot to solve this but I am not getting any solution even after reading a lot of documents in stackoverflow,
Here is the code ..please help me solve this problem. In onCreate method I have defined ImageButton
ImageButton imageButton=new ImageButton(this);
imageButton.setBackgroundColor(Color.TRANSPARENT);
imageButton.setOnTouchListener(this);
WindowManager.LayoutParams layoutParams=new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(imageButton, layoutParams);`
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Toast.makeText(getApplicationContext(), "Double touched "+DoubleTouch, 1000).show();
return true;
}
why you are returning false ? you should return true on touch listener !!
sorry but can you tell me what you want to do with the touch listener ? as i can see on your code you only showing Toast. if you want to perform on click . just use OnClickListener instead of touch listener !
response to your Comment. well you can use OnClickListener instead for double clicks or more!
here is an example : first you create a Boolean variable and assign it to true
boolean lightit = true;
second create the method for the button clicks. in my example here when first time i click the button image will change to another one. and when the button clicked again it change it back to the old image. follow my example here and hope it helps you in anyway.
private void showHide1() {
if (lightit) {
if (null != bmp) {
rotator = null;
styleId = StyleKosh.bright;
change = StyleKosh.changeStyle(bmp, styleId);
view.setImageBitmap(change);
fix.setImageResource(R.drawable.lighton);
} else if (null == bmp) {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_SHORT);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
} else {
rotator = null;
change = null;
fix.setImageResource(R.drawable.light);
view.setImageBitmap(bmp);
}
lightit = !lightit;
}
and then on your button Click Listener add this showHide();
this is how i handle clicks on a button or imageview. for multiple clicks you just need to add if else . Please see i'm using the same method to show and hid the statusBar in fullScreen but i'm using Gesture instead of OnTouchListener . tell me if you like to know how i use the Gesture is easier to use.
You would have to return true on the onTouch to indicate that a touch event has occurred and you want to return the result.
Refer to this : Android: How to detect double-tap? and Android multi touch and double tap working together for an imageview
I am using a popup window to display some information and have an edit button that allows that information to be changed. I am loading all textviews, buttons and edittext fields and hiding/showing them as needed. The edittext fields are not letting me edit them. I have tried the below suggestion about setting focusable to true, but that isn't working. Any other suggestions?
Tried this: EditText On A Popup Window
EDIT: Below is part of my code. I've just included the parts for initializing the edittext and showing the popup contents since everything else is working, just not the edittext. I am using a tablelayout, and when the user clicks a row, the popup window displays. Keep in mind I'm still pretty new to Java and Android!
EDIT #2: The softkeyboard was not showing when the edittext was selected, but now it will show if I dismiss the popup once and then call it again. I then tried forcing the softkeyboard to display, and it showed behind the popup (another problem). I was able to select the number 1 since it was barely showing behind the popup window, but it didn't seem to work either. The fix was the same here: dismiss the popup window and recall it. So my remaining problem is being able to type into the edittext without having to dismiss the popup once and recall it. As such, I am changing the title of this question.
row1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final EditText pwETBrand = (EditText) vPopUp.findViewById(R.id.et_editbrand);
if (!infoClick) {
infoClick = true;
// Popup elements initialized earlier
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
....
// Hide some elements initially until "EDIT" button is pressed
pwETBrand.setVisibility(View.INVISIBLE);
....
// When EDIT button is pressed, hide default elements, and show edit elements
pwEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v2) {
pwETBrand.setVisivility(View.VISIBLE);
// Other element settings go here
pwInfo.setFocusable(true);
}
)};
....
}
}
I have implemented a solution, though it may not be the best solution. I have set a boolean field at the class initialization that checks if the popupwindow has ever been called. If it has not yet been called, then it will immediately dismiss the popupwindow and re-open it since my problem is in the popupwindow initialization.
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
if (pwFirst) {
pwFirst = false;
pwInfo.dismiss();
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
}
Not the best solution, but it works.
Try this,
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
works for me.
Use Below Link's Code for that, it may help you.
Popup With Edittext
Whenever you create a PopupWindow you must put true in focusable like that :
PopupWindow(View contentView, int width, int height, boolean focusable)
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
good luck
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()}