key event problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Postby mpathare » Mon May 25, 2009 2:51 pm

Thats right. Also, I haven't used xml for the ScrollView like you. I did it in java. I am stumped... :?
mpathare
Developer
Developer
 
Posts: 26
Joined: Sat Apr 11, 2009 10:43 pm

Top

Postby mangaluve » Mon May 25, 2009 3:02 pm

Hm but if you're using my code, where do you create the scrollview (I cannot see it in your activity). Post the "modification" of my code, that one that's not working, and I can take a look at it :)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby mpathare » Mon May 25, 2009 3:19 pm

Here you go.

main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent">
  11.  
  12.         <TextView
  13.  
  14.                 android:text="APPNAME"
  15.  
  16.                 android:textSize="30px"
  17.  
  18.                 android:layout_width="fill_parent"
  19.  
  20.                 android:layout_height="wrap_content"
  21.  
  22.                 android:height="35px"
  23.  
  24.                 android:layout_weight="1"
  25.  
  26.                 android:gravity="center"/>
  27.  
  28.         <TextView
  29.  
  30.                 android:text="Press the menu button to start"
  31.  
  32.                 android:textSize="15px"
  33.  
  34.                 android:height="20px"
  35.  
  36.                 android:layout_width="fill_parent"
  37.  
  38.                 android:layout_height="wrap_content"
  39.  
  40.                 android:layout_weight="0"
  41.  
  42.                 android:gravity="center"/>
  43.  
  44. </LinearLayout>
  45.  
  46.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Test extends Activity {
  3.  
  4. /** Called when the activity is first created. */
  5.  
  6. private MyView mGv;
  7.  
  8. private GameScrollView mGsv;
  9.  
  10.  
  11.  
  12. @Override
  13.  
  14. public void onCreate(Bundle savedInstanceState) {
  15.  
  16.     super.onCreate(savedInstanceState);
  17.  
  18.     setContentView(R.layout.main);
  19.  
  20. }
  21.  
  22.  
  23.  
  24. @Override
  25.  
  26. public boolean onCreateOptionsMenu(Menu menu) {
  27.  
  28.     boolean result = super.onCreateOptionsMenu(menu);
  29.  
  30.     menu.add(0, 1, 0, "new");
  31.  
  32.     menu.add(0, 2, 0, "exit");
  33.  
  34.     return result;
  35.  
  36. }
  37.  
  38.  
  39.  
  40. @Override
  41.  
  42. public boolean onOptionsItemSelected(MenuItem item) {
  43.  
  44.     switch (item.getItemId()) {
  45.  
  46.     case 1:
  47.  
  48.         mGv = new MyView(this);
  49.  
  50.         mGsv = new GameScrollView(this);
  51.  
  52.         mGsv.addView(mGv);
  53.  
  54.         setContentView(mGsv);
  55.  
  56.         return true;
  57.  
  58.     case 2:
  59.  
  60.         finish();
  61.  
  62.         return true;
  63.  
  64.     }
  65.  
  66.     return false;
  67.  
  68. }
  69.  
  70. }
  71.  
  72.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Scroll View
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class GameScrollView extends ScrollView{
  3.  
  4.        
  5.  
  6.         public GameScrollView(Context context) {
  7.  
  8.                 super(context);
  9.  
  10.         }
  11.  
  12.        
  13.  
  14.         @Override
  15.  
  16.         public boolean onInterceptTouchEvent (MotionEvent ev){
  17.  
  18.                 return false;
  19.  
  20.                
  21.  
  22.         }
  23.  
  24. }
  25.  
  26.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Child View
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyView extends View implements OnKeyListener, OnTouchListener{
  3.  
  4.  
  5.  
  6.         //called by parent view to get the measurement of a child view
  7.  
  8.         @Override
  9.  
  10.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  11.  
  12.                 setMeasuredDimension(480, 400);
  13.  
  14.         }
  15.  
  16.  
  17.  
  18.         public MyView(Context context) {
  19.  
  20.                 super(context);
  21.  
  22.                 this.setOnKeyListener(this);
  23.  
  24.                 this.setOnTouchListener(this);
  25.  
  26.         setFocusable(true);
  27.  
  28.         setFocusableInTouchMode(true);
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         @Override
  35.  
  36.         protected void onDraw(Canvas canvas) {
  37.  
  38.         Paint paint = new Paint();
  39.  
  40.         paint.setColor(0xFFFF0000);
  41.  
  42.        
  43.  
  44.         canvas.drawRect(0, 0, 500, 500, paint);         }//end onDraw
  45.  
  46.  
  47.  
  48.         @Override
  49.  
  50.         public boolean onKey(View v, int keyCode, KeyEvent event){
  51.  
  52.                 if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
  53.  
  54.                         Log.w("gameview", "inside onkey");
  55.  
  56.                         return true;
  57.  
  58.                 }
  59.  
  60.                 else{
  61.  
  62.                         return false;
  63.  
  64.                 }
  65.  
  66.         }
  67.  
  68.        
  69.  
  70.         @Override
  71.  
  72.         public boolean onTouch(View v, MotionEvent event){
  73.  
  74.                 return true;
  75.  
  76.         }
  77.  
  78. }
  79.  
  80.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
mpathare
Developer
Developer
 
Posts: 26
Joined: Sat Apr 11, 2009 10:43 pm

Postby mangaluve » Mon May 25, 2009 3:47 pm

Try to put a call to requestFocus() at the end of the constructor of MyView, at least your code works for me when I try that.. It could be that the View doesnt have focus (the setFocusable only says that it Can have focus)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby mpathare » Mon May 25, 2009 3:53 pm

Whew! That worked :D
I don't get my you have to explicitly request focus. I don't need it for touch events...why for key events? Also, why would it not be required when you create the view in xml?

Thanks for all your help man!
mpathare
Developer
Developer
 
Posts: 26
Joined: Sat Apr 11, 2009 10:43 pm

Postby mangaluve » Mon May 25, 2009 5:34 pm

Finally :)

I dont really know, perhaps your View doesn't have focus because you press the menu and create the View there... if you create it in onCreate it will be focused automatically. Also, key events are a little bit different from touch event.. I mean, if you touch the View, it should get focus. But when you press a key, especially the back key, you dont really know which View to focus.

I still got a lot to learn :)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top
Previous

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests