Getting problem with the method View.onKeyDown

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

Getting problem with the method View.onKeyDown

Postby logtoavi » Thu Dec 18, 2008 5:47 am

hi,

I am new in android. my code is

/**
*
*/
package demo.demo;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.graphics.Typeface;
/**
* @author avishek
*
*/
public class MyCanvas extends View implements Runnable{
SoundMgr sound;
Typeface tf;
public static String debug = "null";
Bitmap logo, splash, menubg , selected;


public static int state = -1;

public static int W_LCD = 0;
public static int H_LCD = 0;

private final Paint mPaint = new Paint();

int index = 0;
private final String[] main_menu =
{
"New Game" ,
"Options" ,
"Help" ,
"About" ,
"Exit" ,
};

public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
Resources r = this.getContext().getResources();
logo = createImage(r.getDrawable(R.drawable.logo),128,50);
splash = createImage(r.getDrawable(R.drawable.splash),320,455);
menubg = createImage(r.getDrawable(R.drawable.menubg),320,455);
selected = createImage(r.getDrawable(R.drawable.selected),124,30);
sound = new SoundMgr();
sound.createPlayer(context, R.drawable.menu);
new Thread(this).start();
tf = Typeface.create("Arial", Typeface.BOLD_ITALIC);
}

public void onDraw(Canvas canvas)
{
mPaint.setColor(Color.WHITE);
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), mPaint);

switch (state)
{
case Const.logo_state:
if (logo != null)
canvas.drawBitmap(logo, (this.getWidth()/2 - logo.getWidth()/2),(this.getHeight() - logo.getHeight())/2,mPaint);
break;
case Const.splash_state:
if (splash != null)
canvas.drawBitmap(splash, (W_LCD - splash.getWidth())/2,(H_LCD - splash.getHeight())/2,mPaint);
break;
case Const.main_menu:
if (menubg != null)
canvas.drawBitmap(menubg, (W_LCD - menubg.getWidth())/2,(H_LCD - menubg.getHeight())/2,mPaint);

drawMenu(canvas,main_menu);

canvas.drawText("x_pos " + x_pos + " y_pos -> " + y_pos, 200, 160, mPaint);
break;
default:
mPaint.setColor(Color.BLUE);
canvas.drawText("x_pos " + x_pos + " y_pos -> " + y_pos, 20, 20, mPaint);
break;
}

//canvas.drawText("debug = " + debug, 200, 160, mPaint);
}

public Bitmap createImage(Drawable tile,int mTileWidth, int mTileHeight) {
Bitmap bitmap = Bitmap.createBitmap(mTileWidth, mTileHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, mTileWidth, mTileHeight);
tile.draw(canvas);

return bitmap;
}

public void drawMenu(Canvas c,String[] s)
{
int x = 200;
int y = 200;
if (Key.equalsIgnoreCase("null"))
c.drawText(Key, 200, 180, mPaint);

for (int i = 0; i < s.length; i++)
{
if (i == index)
{
mPaint.setColor(Color.RED);
c.drawText(s[i], x + menu_x, y, mPaint);
}
else
{
mPaint.setColor(Color.BLUE);
c.drawText(s[i], x + menu_x, y, mPaint);
}
y += 30;
}

mPaint.setColor(Color.BLACK);
//c.drawRect(this.x, this.y, 10, 10, mPaint);
c.drawLine(x_pos,y_pos,this.x+5,this.y+5, mPaint);
}

int menu_x = 0;

@Override
public void run() {
while (true)
{
try
{
if (W_LCD == 0 && H_LCD == 0)
{
W_LCD = this.getWidth();
H_LCD = this.getHeight();
}
if (state < 1)
{
state++;
Thread.sleep(3000);
}
else if (state == 1)
{
state++;
sound.play();
Thread.sleep(3000);
}
else if (state == 2)
{
menu_x += 2;
Thread.sleep(100);
}
this.postInvalidate();
}
catch(Exception e)
{
debug = "Exception at thread";
}
}
}

String Key = "null";

/*@Override
public boolean onKeyDown(int keyCode, KeyEvent key)
{
debug = "onKeyDown";
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_DOWN:
Key = "KeyEvent.KEYCODE_DPAD_DOWN";
this.postInvalidate();
return (true);
//break;

case KeyEvent.KEYCODE_DPAD_UP:
Key = "KeyEvent.KEYCODE_DPAD_UP";
this.postInvalidate();
return (true);
//break;

case KeyEvent.KEYCODE_DPAD_LEFT:
Key = "KeyEvent.KEYCODE_DPAD_LEFT";
this.postInvalidate();
return (true);
//break;

case KeyEvent.KEYCODE_DPAD_RIGHT:
Key = "KeyEvent.KEYCODE_DPAD_RIGHT";
this.postInvalidate();
return (true);
//break;

case KeyEvent.KEYCODE_DPAD_CENTER:
Key = "KeyEvent.KEYCODE_DPAD_CENTER";
this.postInvalidate();
return (true);
//break;
}
this.postInvalidate();
return false;
//return super.onKeyDown(keyCode, key);
}*/

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg)
{
debug = "onKeyDown";

switch (keyCode)
{
case KeyEvent.KEYCODE_0:
case KeyEvent.KEYCODE_1:
case KeyEvent.KEYCODE_2:
case KeyEvent.KEYCODE_3:
case KeyEvent.KEYCODE_4:
case KeyEvent.KEYCODE_5:
case KeyEvent.KEYCODE_6:
case KeyEvent.KEYCODE_7:
case KeyEvent.KEYCODE_8:
case KeyEvent.KEYCODE_9:
Key = "1234567890";
this.postInvalidate();
return true;

case KeyEvent.KEYCODE_STAR:
Key = "KEYCODE_STAR";
this.postInvalidate();
return true;
}
return false;
}

int x_pos = 0, y_pos = 0;
float x = 0 , y = 0;

@Override
public boolean onTouchEvent(MotionEvent motion)
{
x_pos = (int)x;
y_pos = (int)y;

try
{
x = motion.getX();
y = motion.getY();


debug = "onTouchEvent";
this.postInvalidate();
return (true);
}
catch(Exception e)
{
return (false);
}
}
}


And I am getting problem with method onKeyDown(int keyCode, KeyEvent msg).
This method is not working i dont know why. Where i m doing wrong :(
Thanks and Regards
Avishek Kumar
logtoavi
Freshman
Freshman
 
Posts: 9
Joined: Wed Dec 17, 2008 11:30 am
Location: Noida

Top

Postby logtoavi » Thu Dec 18, 2008 11:39 am

Hi add this.setFocusable(true);

then View.onKeyDown(int keyCode, KeyEvent key) will work
Thanks and Regards
Avishek Kumar
logtoavi
Freshman
Freshman
 
Posts: 9
Joined: Wed Dec 17, 2008 11:30 am
Location: Noida

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests