I thought you couldn't instantiate interfaces?

by Mark Murphy » Wed, 31 Mar 2010 01:05:30 GMT

Sponsored Links

This code is creating an instance of an anonymous inner class that
implements the interface.

Possible, yes. "Better" is difficult to answer in the abstract.

I tend to make longer anonymous inner classes be private data members:

private View.OnKeyListener onKey=new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                                return true;
                return false;

then call myEditText.setOnKeyListener(onKey) where needed. But that's
just me.

Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 2.0 Available!


Other Threads

1. Getting Location and Orientation Values Continuously

Using a HTC Magic. (Android 1.6)
Am a beginner.  Have gone through the documentations available. I know
I might be asking a silly question.
I am trying to continuously read and assign the lat/long and
orientation values into variables.  I am able to do them as separate
programs using the sample codes available.

I tried to implement this using the code I found here:

And replaced the 'Accelerometer' with 'Orientation'.

TYPE_ORIENTATION should be replaced with
SensorManager.getOrientation() for the newer APIs.  So Is there a way
I can integrate that changes?

public class abc extends Activity implements LocationListener,
            public void onCreate(Bundle savedInstanceState) {
                       //bind the ui components to code referances
                       latitude = (TextView) findViewById(R.id.latitude);
                        longitude = (TextView) findViewById(R.id.longitude);
                        //get the locationManager
                        locationManager = (LocationManager)

             //Sensor Manager
           sensorManager = (SensorManager)


public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
                //if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                if (event.values[0] >= 0) {
                myazimuth = event.values[0];
                mypitch = event.values[1];
                myroll = event.values[2];


Is the the right way of doing it?
It just doesn't seem to work.
I don't want to display these values(although am currently doing that
to check if I am getting results) but I want 5 variable to store the
values of La/long and the orientation sensors. I want them available
at all times in the program so that I use them for performing some
computations in other methods.

Please advice if there is  better way of doing it.

Thanks in advance


2. Fullscreen input method


I'm trying to develop a new input method on Android 2.1, and have got
the basics working, but I want to make my IM work in fullscreen-mode
even when the device is portrait (I'm using a Nexus One atm). If I
override onEvaluateFullscreenMode to always return true, I can get an
extract view (at the top of the screen, just beneath the status bar),
and a candidates view if I want it (just beneath that), and my main
input view at the bottom of the screen; but there's always a gap,
between the top of my main input view, and the bottom of the
candidates view, through which the application shows (even if I set
the imeFullscreenBackground attribute to a colour, which I then don't
see). The size of the gap varies according to the size of the other
views, etc., but is always at least 30 or so pixels high - if I make
the input view & candidates view big enough, it'll squash the extract
view up to next-to-nothing or remove it altogether to make space....

Anyone got a full-screen IME (in portrait mode!) working, and can tell
me how to do it? I've tried using HierarchyViewer to see if I can get
some idea of what's going on, but wasn't able to get the hierarchy at
all (debugging activities is easier than IMEs, hehe!); and I've had a
look at some source code for InputMethodService.java (the base class
I'm overriding) and come out none the wiser (perhaps I need to look at
the XML?)

Any help/info/pointers appreciated,

TIA, Alan


3. Disabling back button when the alert dialog box is on the screen

4. AccountManager.getAuthTokenByFeatures() with null authTokenType

5. Android - applications and data on memory cards

6. Get holiday list based on the locale.

7. Accuracy of location.getAccuracy() GSM and GPS - exact Meaning?