Gui with touch screen

Put your problem here if it does not fit any of the other categories.

Gui with touch screen

Postby helloandroid » Thu Apr 03, 2008 2:20 pm

Hi,

Does anyone know the way to implement touch screen with buttons?

I guess it may be implement the "OnClickListener"

Am I right? or anyone have any reference or examples for that issue?

Kindly Thanks!
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Top

Postby Jesmiatka » Thu Apr 03, 2008 2:30 pm

Yup you are right, it will also handle if someone uses the "OK" button on your navigation panel.

It would look something like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* React to the xx button */
  2.  
  3.                 button.setOnClickListener(new OnClickListener(){
  4.  
  5.                        
  6.  
  7.                         @Override
  8.  
  9.                         public void onClick(View arg0) {
  10.  
  11.                                 //Do Something
  12.  
  13.                         }
  14.  
  15.                 });
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby helloandroid » Thu Apr 03, 2008 5:19 pm

Jesmiatka wrote:Yup you are right, it will also handle if someone uses the "OK" button on your navigation panel.

It would look something like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* React to the xx button */
  2.                 button.setOnClickListener(new OnClickListener(){
  3.                        
  4.                         @Override
  5.                         public void onClick(View arg0) {
  6.                                 //Do Something
  7.                         }
  8.                 });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



Thank you, Jesmiatka

And so, how to simulate the input (eg, KeyEvent.KEYCODE_A) to the program if the button is clicked?
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Postby plusminus » Thu Apr 03, 2008 6:33 pm

Hello helloandroid :roll: ,

helloandroid wrote:And so, how to simulate the input (eg, KeyEvent.KEYCODE_A) to the program if the button is clicked?


On how to simulate Keystrokes, have a look here: http://www.anddev.org/viewtopic.php?t=717

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby helloandroid » Thu Apr 03, 2008 6:56 pm

plusminus wrote:Hello helloandroid :roll: ,

helloandroid wrote:And so, how to simulate the input (eg, KeyEvent.KEYCODE_A) to the program if the button is clicked?


On how to simulate Keystrokes, have a look here: http://www.anddev.org/viewtopic.php?t=717

Regards,
plusminus


Thank you plusminus,

but indeed, i just want to implement a button of touch screen,
once button is clicked, simulating the key(eg KeyEvent.KEYCODE_A) has pressed
just like the dialer in emulator, the button output numbers if clicked.
please help :D
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Postby cadlg » Thu Apr 03, 2008 7:05 pm

Hi helloandroid.

I think that simulate a keystroke for that is too expensive. You could write the "a" to screen by code in the onClick event of the button.

regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Top

Postby helloandroid » Thu Apr 03, 2008 7:13 pm

cadlg wrote:Hi helloandroid.

I think that simulate a keystroke for that is too expensive. You could write the "a" to screen by code in the onClick event of the button.

regards,

cadlg


hi cadlg and thank you =]

but the key "a" is used for further processing
that is there is 2 input ways(keybroad or click button on screen) to my program

i wonder if i have to override onKeyUp?

Regards
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Postby cadlg » Thu Apr 03, 2008 7:23 pm

I think yes, if you want to do something when the 'a' key is pressed, I think you will have to overwrite that method, and write another method to do what you need to do, which will then be called from both the onKeyUp and the onClick.

regards,


cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby helloandroid » Thu Apr 03, 2008 7:47 pm

cadlg wrote:I think yes, if you want to do something when the 'a' key is pressed, I think you will have to overwrite that method, and write another method to do what you need to do, which will then be called from both the onKeyUp and the onClick.

regards,


cadlg


so,

button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
//Do Something
}
});

how to call the onKeyUp in that action?
sorry for simple question :oops:
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Postby cadlg » Thu Apr 03, 2008 8:01 pm

It should be something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3. public void do_something(){
  4.  
  5. //      ... do something
  6.  
  7. }
  8.  
  9.  
  10.  
  11. @Override
  12.  
  13. public boolean onKeyUp(int keyCode,KeyEvent msg){
  14.  
  15.     switch(keyCode){
  16.  
  17.     case KeyEvent.KEYCODE_A:
  18.  
  19.              do_something();
  20.  
  21.     };
  22.  
  23.     return false;
  24.  
  25. };
  26.  
  27.  
  28.  
  29. @Override
  30.  
  31. public void onCreate(Bundle icicle) {
  32.  
  33.         // ...
  34.  
  35.         mButton.setOnClickListener(new View.OnClickListener() {;
  36.  
  37.         public void onClick(View view) {
  38.  
  39.           mText.append("A");               
  40.  
  41.           do_something();
  42.  
  43.      }
  44.  
  45.      });
  46.  
  47.  
  48.  
  49.         // ...
  50.  
  51. }
  52.  
  53.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby helloandroid » Fri Apr 04, 2008 11:54 am

Actually, my program is ready, so I dont want to make big changes.

I have override the onKeyUp class , which is waiting for any key event from keyboard.

So if I click the GUI botton, how can I simulate I make the pressing keybroad(eg "a") event,
so that the onKeyUp can receive the pressing key event?

Or any other implement method?

Kindly Thanks
helloandroid
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Postby cadlg » Fri Apr 04, 2008 4:06 pm

this is how you can simulate key strokes (taken from a plusminus tutorial):

get the window manager service:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    final IWindowManager windowManager = IWindowManager.Stub
  2.  
  3.     .asInterface(ServiceManager.getService("window"));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


inject the key event:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new Thread(new Runnable() {
  2.  
  3.             public void run() {
  4.  
  5.                  /* Simulate a KeyStroke to the A key. */
  6.  
  7.                  simulateKeystroke(KeyEvent.KEYCODE_A);
  8.  
  9.             }
  10.  
  11.        }).start();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void simulateKeystroke(int KeyCode) {
  2.  
  3.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
  4.  
  5.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
  6.  
  7.    }
  8.  
  9.  
  10.  
  11.    /** This function actually handles the KeyStroke-Injection. */
  12.  
  13.    private void doInjectKeyEvent(KeyEvent kEvent) {
  14.  
  15.         try {
  16.  
  17.              /* Inject the KeyEvent to the Window-Manager. */
  18.  
  19.              windowManager.injectKeyEvent(kEvent.getAction()==kEvent.ACTION_DOWN, kEvent.getKeyCode(),
  20.  
  21.                        kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
  22.  
  23.                                  .getEventTime(), true);
  24.  
  25.         } catch (DeadObjectException e) {
  26.  
  27.              e.printStackTrace();
  28.  
  29.         }
  30.  
  31.    }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby helloandroid » Sat Apr 05, 2008 4:46 am

cadlg wrote:this is how you can simulate key strokes (taken from a plusminus tutorial):

get the window manager service:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    final IWindowManager windowManager = IWindowManager.Stub
  2.     .asInterface(ServiceManager.getService("window"));
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


inject the key event:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new Thread(new Runnable() {
  2.             public void run() {
  3.                  /* Simulate a KeyStroke to the A key. */
  4.                  simulateKeystroke(KeyEvent.KEYCODE_A);
  5.             }
  6.        }).start();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void simulateKeystroke(int KeyCode) {
  2.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
  3.         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
  4.    }
  5.  
  6.    /** This function actually handles the KeyStroke-Injection. */
  7.    private void doInjectKeyEvent(KeyEvent kEvent) {
  8.         try {
  9.              /* Inject the KeyEvent to the Window-Manager. */
  10.              windowManager.injectKeyEvent(kEvent.getAction()==kEvent.ACTION_DOWN, kEvent.getKeyCode(),
  11.                        kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
  12.                                  .getEventTime(), true);
  13.         } catch (DeadObjectException e) {
  14.              e.printStackTrace();
  15.         }
  16.    }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


regards,

cadlg


thank you cadlg & plusminus!
it works

i tried before, but making
kEvent.getAction()==kEvent.ACTION_DOWN as kEvent.getAction()
since isDown() is out
helloandroid
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Mar 02, 2008 6:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 12 guests