Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

disabled toggleButton changing state until ready? [android]

  • 16-10-2013 2:04pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭


    I have a toggle button that I press, keyboard keys are then pressed at half second delays. To stop a user pressing the button twice in quick succession and messing this up I disable the button while the commands are running, and enable it again after. This is a lot of messing about on the UI thread with repeated code. What would be a better way of doing this? Queue up all the commands in a thread or something and leave the user being able to press the button as much as they want? Or jsut abstract this runonui functionality to it's own method. Something else?

    public void pushtotalk(final View view) {
    
           final boolean on = ((ToggleButton) view).isChecked();
    
            new Thread(new Runnable() {         
                @Override
                public void run() {
                    try {
                        Instrumentation inst = new Instrumentation();
                        if (on)
                            {
    
                            runOnUiThread(new Runnable() {
                                 public void run() {
    
                                     ((ToggleButton) view).setEnabled(false);
    
    
                                }
                            });
    
    
    
                     inst.sendKeyDownUpSync(KeyEvent.KEYCODE_NUMPAD_MULTIPLY);
                     Thread.sleep(500);
                     inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9); 
                     Thread.sleep(500);
    
                              runOnUiThread(new Runnable() {
                                     public void run() {
    
                                         ((ToggleButton) view).setEnabled(true);
    
    
                                    }
                                });
    
    
                            }
                        else
                            {   
                            runOnUiThread(new Runnable() {
                                 public void run() {
    
                                     ((ToggleButton) view).setEnabled(false);
    
    
                                }
    
    
    });
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POUND);
                    Thread.sleep(500);
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9); 
                    Thread.sleep(500);  
                    runOnUiThread(new Runnable() {
    
                                 public void run() {
    
                                     ((ToggleButton) view).setEnabled(true);
    
    
                                }
                            });
                        }
    
                   }
                    catch(InterruptedException e){
                        Log.d(TAG, "Failed to send keycodes: " + e.getMessage());
                }
            }   
        }).start();
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    What about enabling and disabling the button outside of the new thread?

    As soon as you check to see if it's checked or not, can't you disable it then, before you start a new thread?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    Heya that's good, I can indeed disable it beforehand, I had tried that and tried enabling at the end of the method but that happens instantly (as it's not attached to the thread delays) so I thought it wasn't working initially. I suppose I can disable the button beforehand and reenable it in the thread to remove some of the duplication.

    Or else add a delay when reenabling the button at the end of the thread by calling a handler but that's more code again.
    Guess removing some of the duplication will do unless there is some way to introduce delays without threads/handers! Thanks.


    public void pushtotalk(final View view) {
    
    		final boolean on = ((ToggleButton) view).isChecked();
    		[B]((ToggleButton) view).setEnabled(false);[/B]
    
    		new Thread(new Runnable() {
    			@Override
    			public void run() {
    				try {
    					Instrumentation inst = new Instrumentation();
    					if (on) {
    						inst.sendKeyDownUpSync(KeyEvent.KEYCODE_NUMPAD_MULTIPLY);
    						Thread.sleep(500);
    						inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9);
    						Thread.sleep(500);
    
    					[B]	runOnUiThread(new Runnable() {
    							public void run() {
    
    								((ToggleButton) view).setEnabled(true);
    							}
    						});
    					} [/B]else {
    						inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POUND);
    						Thread.sleep(500);
    						inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9);
    						Thread.sleep(500);
    						[B]runOnUiThread(new Runnable() {
    							public void run() {
    
    								((ToggleButton) view).setEnabled(true);
    							}
    						});
    					}[/B]
    				} catch (InterruptedException e) {
    					Log.d(TAG, "Failed to send keycodes: " + e.getMessage());
    				}
    			}
    		}).start();
    	}
    


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    A solution on stackoverflow that might suit your needs.

    How to set delay in Android onClick function


Advertisement