Archive

Archive for the ‘android java’ Category

Projects based on Mezzofanti – Droidslator

November 2nd, 2009 No comments

We are happy other people reuse our code. From the groups of people from academia and industry that use Mezzofanti, we are pleased to point to this project – because it is an Apache License 2.0 project, that looks for contributors.

http://code.google.com/p/droidslator/

Droidslator version 1.0 is the first language translation program desiged for android-powered phones, seamlessly combined both online and offline dictionaries databases. Droidslator contains many intelligent features such as gesture commands, online/offine search auto-detection and other interesting features.

Categories: android java Tags:

Using the keys events (keyboard)

August 25th, 2009 No comments

(Android phone – how-to/example)

There are two methods you need to override:
onKeyDown – that is called when the key is pressed
onKeyUp – that is called when the key is released

This is how your code will look like:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode) {
     case KeyEvent.KEYCODE_FOCUS: /* .. code for KEYCODE_FOCUS .. */ break;

return super.onKeyDown(keyCode, event);
}

Note1: do not forget to call the super.onKeyDown method, if you want to allow the key to be used in the default mode (example: “back-key” or “power-key” in most of the applications keep the default functionality)

Note2: there is a method you may find useful event.getRepeatCount() that <>

Your code should look the same for onKeyUp.

Categories: android java Tags: , ,

How to create a standard Android Menu

August 25th, 2009 No comments

(Android phone – how-to/example)

Maybe you’ve noticed the menu that appears when you are in desktop and press “menu-key”, almost all applications implement this feature, and it looks pretty nice. If you want to include it in your code, this is how.
First override the onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);

menu.add(PREFERENCES_GROUP_ID, SETTINGS_ID, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add(PREFERENCES_GROUP_ID, HELP_ID, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help);
menu.add(PREFERENCES_GROUP_ID, FEEDBACK_ID, 0, R.string.menu_feedback)
.setIcon(android.R.drawable.ic_menu_send);
menu.add(PREFERENCES_GROUP_ID, ABOUT_ID, 0, R.string.menu_about)
.setIcon(android.R.drawable.ic_menu_info_details);

return true;
}

In this example we just add 4 items to the menu (preferences, help, feedback and about). Each item has a specific icon. There is a default set of icons, you may wish to use as all the other applications. In our example we used ic_menu_preferences for preferences, etc.. Note: that you can use your own icons as well.

Also for each item you have to add a default action.

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
   case SETTINGS_ID: /* .. start settings activity .. */ break;
   case HELP_ID: /* .. start help activity .. */ break;
   case FEEDBACK_ID: /* .. start feedback activity .. */ break;
   case ABOUT_ID: /* .. start about activity .. */ break;

Categories: android java Tags:

Custom widgets

August 25th, 2009 No comments

(Android phone – how-to/example)

If you consider doing your own custom widgets, you must start by creating a new class, that extends android.view.View

The simplest example is customizing a bit the screen layout. Consider you want to draw a frame, on top of an image or the camera preview. In this example you just need to extend View (as mentioned), and than override the onDraw(Canvas canvas) method, where you will be doing your drawing.

public final class CaptureLayout extends View
{
….
@Override
public void onDraw(Canvas canvas)
{
   int width = canvas.getWidth();
   int height = canvas.getHeight();

   // Draw the exterior (i.e. outside the framing rect) darkened
   Paint m_Paint = new Paint();
   m_Paint.setColor(Color.GREEN);
   Rect m_Box = new Rect();
   m_Box.set(0, 0, width, frame.top); // create a box
   canvas.drawRect(m_Box, m_Paint); // draw the box with the given color

If you want more, let’s add some interaction with the widget.
For example let’s say we want to display a message if the user hits the green box (that we just drew).
In order to do this, we need to add a click listener. Thus, when we allocate the widget, we need to pass this click_listener as a parameter to setOnClickListener. Example:

CaptureLayout cl = (CaptureLayout)findViewById(R.id.mezzofanti_capturelayout_view);
cl.setOnClickListener(new OnClickListener() {
     public void onClick(View clickedView) {
     // action for green rectangle pressed
     }
     }
);

Categories: android java Tags:

Shared Preferences

August 25th, 2009 No comments

(Android phone – how-to/example)

The android.content.SharedPreferences may be useful if you want to save some application settings in a file, and share them through-out all the activities in your application. One cool feature is that the SharedPreferences is directly linked to the android.preference package, that is if you use a android.preference.PreferenceActivity in your application for storing/retrieving the setup, all the variables in the setup and the variables in the SharedPreferences will share the same xml file on the disk. This file may be found on disk in /data/data/com.my.package.name/. For more info about creating your own PreferenceActivity consult the appropriate post on this website.

Let’s say you want to store on disk a boolean variable, to know if you want to skip the introduction at the start of the application or not. The code to write the variable is:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor spe = prefs.edit();
spe.putBoolean(KEY_SKIP_INTRO_AT_STARTUP, true); // we just save “true” in the xml file
spe.commit();

The code to read the variable is:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
m_bSkipIntroAtStartup = prefs.getBoolean(KEY_SKIP_INTRO_AT_STARTUP, false); // if it cannot read the value from file, will by default return false

where “this” is the current activity, m_bSkipIntroAtStartup is the variable to be initialized, and KEY_SKIP_INTRO_AT_STARTUP is the string key (ex: String KEY_SKIP_INTRO_AT_STARTUP = “preferences_skip_intro_at_startup”;)

Categories: android java Tags: