Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

Monday, 12 November 2012

Quickly implement constructors based on a superclass in Eclipse

package com.bc.android;

import android.view.View;

public class MyCustomView extends View
{
}


How annoying is it when you inherit from another class and you get this error:

Implicit super constructor View() is undefined for default constructor. Must define an explicit constructor

This means we need to implement at least one of the constructors from the superclass in the subclass.

Turns out Eclipse makes it easy to pick and choose what constructors you want to implement and automatically inserts these constructors into your code.

Open the Source menu and select "Generate Constructors from Superclass..."


Now set a check mark on every constructor you want to implement in your subclass and hit OK.


And there you have it! Three constructors automatically implemented that use the correct parameter list and call the constructor of the superclass.

Here's the final result:


package com.bc.android;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class MyCustomView extends View
{
    public MyCustomView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyCustomView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyCustomView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }
}

How to attach Android source to Eclipse

Don't you hate it when you hit F3 on an Android class and you get Eclipse's ugly class viewer instead of Android's actual source for that class?



There's an easy way to fix it.


In Package Explorer expand your Android library entry (in the case above it's Android 4.1) and right click the android.jar file.

Select Properties...

Fill out the information like I've done here.


In summary, select Java Source Attachment in the left pane.
Mark the External location radio button.
Enter the path of the Android source you've downloaded through the Android SDK Manager.

If you don't have the contents of <android_sdk_location>/sources/<android_version> then you'll need to open your Android SDK Manager and download the source for the appropriate version.

Now next time you want to Open Declaration for an Android class, it'll be in a format that's not going to burn out your eyes. :]

Monday, 2 July 2012

How to debug on an Android device in Eclipse

 You have your application written and it works great on the emulator, but you want to try it on a real device. Here's how. The original article is here.

Install the Google USB Driver

Open the Android SDK Manager (from Eclipse, select Window > Android SDK Manager)

Make sure that the Sort By: radio selection is set to API Level and the Updates/New option is selected.

The last folder in the Packages tree view should be titled Extras. In that folder is an item called Google USB Driver. Check that and select "Install packages...". Agree to the terms and conditions and select Install.

Once that's complete, close the Android SDK Manager window.

Set up your Device for Development

Turn on USB Debugging on your device.
For Android versions less than 4.0, the setting is in Settings > Applications > Development
For 4.0 and above it lives in Settings > Developer Options

Install the OEM USB Driver

Here comes the tricky part.

Connect your device over USB.

Open up Computer (Start > Computer in Windows 7)

Right click Computer in the left pane and click Manage in the context menu.

A Computer Management window will appear. Click on Device Manager in the tree view in the left pane. It's under the System tools item.

Your device should be in this list, under Other. Right click it and select Update Drivers. From here you need to manually navigate to the <android-sdk-root>/extras/google/usb_driver folder.

Once selected the driver will install and you'll be able to debug using your device! 

How to update to Android API 16, Jelly Bean 4.1

What's in Android 4.1 Jelly Bean

There's a lot in the new update. Here's a selection of the goodies available.

  • Live wallpapers - users can select a live wallpaper directly from your app
  • More powerful App stack navigation
  • Gapless playback on separate MediaPlayer objects
  • Android Beam supports large file transfers
  • Copy and Paste with Intents
  • Notifications are more customisable
  • Roboto has more font style variants

If you want to see the whole list, check out the the official version page.

How to update Android for API 16


Grab the Android API 16 by running the Android SDK Manager.
For Windows users, run <android install directory>/tools/android.bat.

Make sure to Sort by API Level and check the Updates/New and Installed boxes.


Download all updates you need from the Tools and Extras directories. Make sure you get the Google USB Driver so you can continue to debug on device.

Once the Tools and Extras have been updated and installed, check the Android 4.1 (API 16) box and hit "Install [x] packages..."

Select the radio button "Accept All" then hit Install.

Nearly there! Once the packages are installed, close the Android SDK Manager window and open up Eclipse.

Open the Help menu and select Check for Updates. This should always be done after updating the Android SDK so Eclipse can run smoothly with Android.

Once all your modules are up to date, restart Eclipse for good measure and continue developing for Android!