How can I simulate onLongClick? Basically I need the user to click once - and a method to turn it into longClick without actually long clicking.
On Android, every View object has the method performLongClick, that allows you to simulate the action programmatically. But you have to set the listener before:
View dummyView = findViewById(R.id.dummy_view);
dummyView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
And now you can call dummyView.performLongClick(), to simulate the longClick action
View dummyView = findViewById(R.id.dummy_view);
dummyView .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dummyView .performLongClick();
}
});
Related
I'm using a gesture listener to monitor user's action, but when the app pops up the dialog, I don't know how to switch my gesture listener to dialog event and handle the event button (ok and cancel), can anyone give me a suggestion?
Pseudo code likes this
public class MainActivity extends FragmentActivity
implements ConnectionEventListener{
......
// when connection established,
// pop a diaglog (android native diaglog with listview and its adapter) to ask user to select ok or cancel button
#Override
public void onUpdateAlert(final int event, final String message){
}
// gesture listener
// if a dialog pops up, the pose can be used to select OK or cancel
#Override
public void onDetected(Hand pose){
}
}
The problem I have is not the button listener. Actually, I have two listeners work at the same time, one for event monitor and another for pose monitor. When an event comes, the event will pop up a dialog to select "ok" or "cancel" . In the mean time, a pose listener still works. I'd like to know when this case happens, how can I use the pose listener to select "ok" or "cancel" while the diaglod pops up?
I think we need something like this:
public static void showDialog(SomeActivity someActivity, final SomeCallback callBack {
final Dialog dialog = new Dialog(someActivity);
dialog.setContentView(R.layout.dialog_with_buttons);
// OK button ...
Button dialogButtonOk = dialog.findViewById(R.id.btn_ok);
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
callBack.execute(true);
dialog.dismiss();
}
});
// Cancel button ...
Button buttonCancel = dialog.findViewById(R.id.btn_cancel);
buttonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callBack.execute(false);
dialog.dismiss();
}
});
dialog.show();
}
we can call the showDialog from SomeActivity like this:
showDialog(this, new SomeCallback () {
#Override
public void execute(boolean status) {
if (status) {
...
} else {
...
}
}
});
and the callback interface:
public interface SomeCallback {
void execute(boolean status);
}
good luck
the pseudo code can be like:
boolean status = false;
#Override
public void onUpdateAlert(final int event, final String message){
status = true;
// pop up the dialog
}
#Override
public void onDetected(Hand pose){
if(status) {
status = false;
...
}
}
I'm trying to map a button in my fragment layout as a keypress on a keyboard.
This is my button press:
Button down = (Button)(myView.findViewById(R.id.btnDOWN));
down.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "Down pressed");
return false;
}
});
I wasn't able to find any library which would allow me to do so, all I've found was reverse case, when a keyboard or controller press would be sent to device.
What I'm trying to generally do here, is an app that acts like a controller for RetroPie by using the phone as a bluetooth keyboard, with only the keys necessary for a specific controller.
The method you're using is not correct. you should use the setOnClickListener:
Here an example from Android official guide:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
If you're looking for continuos aka long click, you should change it to setOnLongClickListener:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
It wasn't hard to find in this case the solution on this site
In the below code i tried to change the image on first click(working). But on second click it should change back to original state. Likewise it should change on every click simultaneously. Please explain me the logic. i am new to android. Thanks in advance
ib_accordion1 = (ImageButton)findViewById(R.id.ib_accordion1);
ib_accordion1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ib_accordion1.setImageResource(R.drawable.minus_icon);
}
});
Just use a simple boolean value to determine it's state.
boolean isOriginal = true;
ib_accordion1 = (ImageButton)findViewById(R.id.ib_accordion1);
ib_accordion1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
isOriginal = !isOriginal;
ib_accordion1.setImageResource(isOriginal ? R.drawable.original : R.drawable.minus_icon);
});
boolean original = true;
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
original = !original;
img.setBackgroundResource(original ? R.drawable.ic_action_new_light
: R.drawable.ic_action_chat_light);
}
});
Actually, I have edited above comment and worked for me thanks to Sai Chakradhar Sana.
A button triggers an action that should only be invoked once. The button is disabled and hidden in the onClick handler before the action is performed:
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
performTaskOnce();
}
});
private void performTaskOnce() {
Log.i("myapp", "Performing task");
//Do something nontrivial that takes a few ms (like changing the view hierarchy)
}
Even though the button is disabled immediately, it is nonetheless possible to trigger multiple "onClick" events by tapping multiple times very quickly. (i.e. performTaskOnce is called multiple times). Is seems that the onClick events are queued before the the button is actually disabled.
I could fix the problem by checking in every single onClick handle whether the corresponding button is already disabled but that seems like a hack. Is there any better way to avoid this issue?
The problem occurs on Android 2.3.6, I cannot reproduce it on Android 4.0.3. But given the rarity of 4.x devices it is not an option to exclude older devices.
You could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.
This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.
boolean processClick=true;
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(processClick)
{
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
performTaskOnce();
}
processClick=false;
}
});
private void performTaskOnce() {
Log.i("myapp", "Performing task");
//Do something nontrivial that takes a few ms (like changing the view hierarchy)
}
In the interest of keeping DRY:
// Implementation
public abstract class OneShotClickListener implements View.OnClickListener {
private boolean hasClicked;
#Override public final void onClick(View v) {
if (!hasClicked) {
onClicked(v);
hasClicked = true;
}
}
public abstract void onClicked(View v);
}
// Usage example
public class MyActivity extends Activity {
private View myView;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView.setOnClickListener(new OneShotClickListener() {
#Override public void onClicked(View v) {
// do clicky stuff
}
});
}
}
Bit late but this might be of use to someone. In my case I am calling another activity so;
Declare a boolean;
boolean clickable;
In the click listener;
if(clickable){
// Launch other activity
clickable = false;
}
Enable when onResume is called;
#Override
public void onResume() {
Log.e(TAG, "onResume");
super.onResume();
clickable = true;
}
You can use RxView(com.jakewharton.rxbinding2.view.RxView) is an extension around RxJava that created by Jake Wharton.
To integrate it to project you should use implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'
Simple Java usage:
RxView.clicks(yourButton)
.sample(500, TimeUnit.MILLISECONDS)
.subscribe { action() }
In Kotlin you can create extension function to handle your clicks:
View.singleClick(action: () -> Any) {
RxView.clicks(this)
.sample(500, TimeUnit.MILLISECONDS)
.subscribe { action() }
}
Sample:
Kotlin
yourButton.singleClick({
//do some stuff here
})
Java
SingleClickListenerKt.singleClick(yourButton, () -> {
doSomeStuff();
return null;
});
Note: you can use any RxJava operators like debounce, map, first, etc if you wish.
declare a varieble
and use it as
boolean boo = false;
someButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(boo==false){
someButton.setEnabled(false);
someButton.setClickable(false);
someButton.setVisibility(View.GONE);
boo = true;
}
}
});
by this you prevent multiple clicks on your button
hope it help
I have a method deleteDilaog (it displays dialog with yes and no option. when clicked yes it does something, when clicked no it cancel dialog) and it is called either buy taping a button or on selecting a item in a option menu. Problem is, result is not the same? It works fine when selected from menu but when clicking a button it just displays dialog and no matter what i click,nothing happens?
Button:
private void RemoveAll(){
Button button=(Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteDialog();
}
});
}
Menu Item:
#Override
public boolean onMenuItemSelected(int id, MenuItem item) {
mDeleteId=item.getItemId();
switch(item.getItemId()) {
case INSERT_ID:
addItem();
return true;
case DELETE_ALL_ID:
deleteDialog();
break;
}
return super.onMenuItemSelected(id, item);
}
deleteDialog method:
private void deleteDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage
(CONFIRM_DIALOG_STRING).setCancelable(false).setPositiveButton
(POSITIVE, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int
which) {
switch (mDeleteId) {
case DELETE_ALL_ID:
mDbHelper.removeAllLists();
fillData();
break;
case DELETE_ID:
Cursor c = (Cursor)
getListView().getAdapter().getItem(which);
mDbHelper.removeList
(mItemId);
c.requery();
break;
}
}
}).setNegativeButton(NEGATIVE, new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int
which) {
dialog.cancel();
}
});
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
A dialog firstly should never be called as you have coded.
Make us of Activity.onCreateDialog to initialise and maintain you dialog lifecycle
Activities provide a facility to manage the creation, saving and restoring of dialogs. Also See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), and dismissDialog(int).
It looks like the problem is with mDeleteID. It is set in OnMenuItemSelected, but not in button2's onClick listener.
I'm guessing that the switch(mDeleteID) falls through when the button is clicked.
In case of button you're not setting a value to mDeleteId.