I need to know how can I make a pop up window when long press button.
I can't find any solution.
I can t see the window that you want to pop.
Maybe you want to show a DialogFragment which will pop in the center of the screen?
For longPress event use
button.setOnLongClickListener(..);
You could do this using a gesture detector, something like the example below:
GestureDetector gd = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Toast.makeText(getActivity(), "Long press", Toast.LENGTH_LONG).show();
}
});
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
};
Related
I want to display some toast when my button is clicked once and other toast when my button is clicked twice. Is there any built in library or do I need to implement custom logic?
You can Gesture Detector for detecting double taps.
final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
});
For more details you can check this documentation.
Answer given by #TaranmeetSingh is absolutely right but there is a better solution. You can use this library to do it.
How to implement
Add this library -> implementation 'com.github.pedromassango:doubleClick:CURRENT-VERSION'(Version is given above).
Instead of using an onClickListenerObject for onClickListener, you can use DoubleClickListener Check the example below
Button btn = new Button(this);
btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
#Override
public void onSingleClick(View view) {
// Single tap here.
}
#Override
public void onDoubleClick(View view) {
// Double tap here.
}
});
I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?
I am not very skilled in Java, so I would be grateful if you provided some description or code.
You can use onFocusChange listener on your edit text to overcome this problem
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b==true){
//entered in the edit text
}
else {
//left edit text
}
}
});
Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean bool) {
if(bool){
// here, the EditText is in focus
}
else {
//here, the EditText is no longer in focus.
// do what you want to do
}
}
});
I hope this helps.
I want to change the background color of button, on button click, this code is not showing any error , neither it is working .
capstone.setOnClickListener(new HandleClick());
}
private class HandleClick implements View.OnClickListener{
#Override
public void onClick(View v) {
Button btn = (Button) v;
String text = btn.getText().toString();
btn.setBackgroundResource(R.color.btnClick);
Log.d("color", R.color.btBackground + ""); // D/color: 2131427344
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
}
The Log.d() shows the value of color, but the color of button does not changed.
setBackgroundResource(int) method is waiting for a drawable object. If you'd like to use colors, use the appropriate method which is setBackgroundColor(int)
and call btn.invalidate(); after that
More details on Android Developers
http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)
If you need to use a color resource as the background color of a view, you will need to get the ARGB color from resources using getColor(), then set that in setBackgroundColor().
btn.setBackgroundColor(getResources().getColor(R.color.btBackground, null));
The function setBackgroundResource will render the button.
try
setBackground(this.getResources().getDrawable(R.drawable.btBackground))
We can use this, as It will get the default background color when the unclick the button. Or we can use the code what I have posted in answer, the problem was I was using same color as default as well as on button click.
capstone.setOnTouchListener(new HandleTouch());
}
private class HandleTouch implements View.OnTouchListener{
#Override
public boolean onTouch(View v, MotionEvent event) {
Button btn = (Button) v;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
btn.setBackgroundResource(R.color.btnClick);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btn.setBackgroundResource(R.color.btBackground);
}
return false;
}
}
I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device. How can I do that? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets. But the dialog class doesn't have the addWindowListener method, so I cannot use it. Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?
This is the code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
}
Thanks for the attention!
EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?
onDismiss DialogInterface
Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.
Hope this will solve your problem.
Edit You can use dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(false);
OR Alternatively
Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?
dialog.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();
this.dismiss();
}
return false;
}
});
And for back press you can do dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.
OR you can alternatively override setOnKeyListener event and put your own code into it.
Edit
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Happy Coding!!!
You need to Override onBackPressed inside Dialog class. Also make sure to close dialog after override OnBackPressed .
Try this
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dismiss(); // make sure to call dismiss to close dialog
// put your code here
}
}
If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
}
});
But if you want to trigger an event if something dismisses the dialog, like some other event then use
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
}
});
Hi stackoverflow friends
I recently faced an issue that how can i disable global search button in android while an alert is shown in the screen.I don't want to disappear the alert box by using search button. I need to user must click the alertbox button and disappears in that way.So I want to disable the search button while alert box is shown. But I can disable the back button using setCancable(false).How can I solve this ?
THanks in advance.
So, Your intention is to provide non-cancelable alert.
Suggesting to set OnDismissListener and just show alert again. It's not very good from visual perspective (alert get closed and opened again).
Below is some obvious example how to achieve such non-cancelable alert (code is inside Acctivity class):
/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;
#Override
protected void onResume() {
// Say, we need to show alert when activity resumed
if(true/*provide condition to show alert*/) {
showAlert();
}
}
/**
* Show non dismissable alert
*/
private void showAlert() {
if(null == this.alert) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.str_alert_title);
builder.setMessage(R.string.str_alert_text);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
YourActivity.this.alertKeyPressed = true;
dialog.dismiss();
}
});
this.alert = builder.create();
this.alert.setOwnerActivity(this);
this.alert.show();
this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// dialog is not allowed to be dismissed, so show it again
YourActivity.this.alert = null;
if(!YourActivity.this.alertKeyPressed) {
showAlert();
}
}
});
}
}
However, I don't think it's the right way to left such alert for the user, sometimes it might be needed for cases like evaluation restriction etc.
Override onSearchRequested in your Activity and have it return false while the dialog is being shown. This should block the request, as per the docs:
You can override this function to force global search, e.g. in
response to a dedicated search key, or to block search entirely (by
simply returning false).
Returns true if search launched, and false if activity blocks it. The
default implementation always returns true.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//There you catch the key, do whatever you want to do.
//Return true if you handled the key event, so nothing will trigger.
//Return false if you want your activity to handle.
return true;
}
})
Just add the code above to alert dialog builder. Hope this snippet would help. Good luck.