Droidin' out tips and tricks from an android developer

11Sep/110

More on testing automation in Android

Posted by noslen

Test automation has been a particularly interesting topic for me in the past few weeks. I wanted to share some of the info I've gathered hoping that it will be useful to someone else. In my previous post I talked about Testdroid which makes use of robotium and also can make MonkeyRunner tests The plugin provided by Testdroid is essentially a way to record and script UI events.
It will generate test cases for you where you can then go in and add assertions. It's pretty neat.

Now i'd like to talk a little bit about Robolectric. Robolectric is created and maintained by folks at Pivotal labs. Robolectric runs within your VM which makes it very cool.
This means you don't need the emulator to run tests. That's pretty cool! I definitely recommend giving this a try. I'm going to deep dive into Robolectric to use on my current personal project.
I'll post some of those results later.

Filed under: general No Comments
5Sep/110

How to ensure views are set below the fold on a device.

Posted by noslen

Did you ever have a view that you needed to make sure started off below the screen so the user could scroll to it later?
Sounds weird but I had to do that for a designer's crazy ass UI.
Wasn't as simple as I first thought so I wanted to share the code.
Turns out that onLayout() doesn't always work. you sometimes don't get all of the dimensions for the views you're interested in and it's just a mess. I guess all the measures() for each view haven't happened yet. Who knows, or rather who cares. It didn't work so I had to find a way that did.

Turns out you can listen for layout change events by adding a listener to the ViewTreeObserver of a view, which notifies of many layout changes on your view. If you add a OnGlobalLayoutListener to the ViewTreeObserver of the view you're interested in you are guaranteed to have gotten the measure() values at that point.
You can then use any way you want to set the position of your view. In my case I chose to use padding. Anyone have a better suggestion?

	ViewTreeObserver vto = bottomContainer.getViewTreeObserver();
	vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
	    @Override
	    public void onGlobalLayout() {
		Display display = getWindowManager().getDefaultDisplay();

		LinearLayout bottomContainer = (LinearLayout) findViewById(R.id.footer_container);
		ViewGroup profileBar = (ViewGroup) findViewById(R.id.profileBar);
		RelativeLayout sv = (RelativeLayout) findViewById(R.id.main);
		bottomContainer.setPadding(0, display.getHeight(), 0, 0);

		bottomContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		bottomContainer.forceLayout();

	    }
	});
Filed under: android No Comments
5Sep/110

Testdroid – a great test solution for Testing Automation in Android

Posted by noslen

Because of fragmentation in Android it is very important to test you apps in as many devices as you can get your hands on. You've heard it all before right?

So have I. Testing mobile apps still remains quite a daunting task. For larger projects such as the one I'm currently working on Test Automation becomes a necessity very quickly. It is simply too time consuming, error prone and difficult to test manually. So what can you do? What tools are available to help you automate UI testing?

Luckily, Android comes to the rescue. Android's testing framework is top notch and easy to work with. They make life worth living.

Android's testing toolset is based on JUnit and is very well integrated for android projects into Eclipse. Android instrumentation provides a way to hook into android components and load applications.

The testing framework also provides a way to create Mock objects to help you isolate parts of your application for specific tests.

You also get the monkeyrunner tool, a python framework for executing android apps.

All in all you get a very powerful set of tools that can enable you to produce detailed and efficient testing solutions.

But wait! there's more! Since of course this framework is so great, there are developers building great testing solutions based on these tools. One that i'm starting to dig into is robotium. Robotium is a test framework  aimed at test case developers to write function, system and acceptance tests scenarios for apps.

Adding yet more value to this are companies like bitbar who provide a plugin for Eclipse called testdroid which makes recording a UI test of your app, child's play. Very cool and worth checking out!

Filed under: general No Comments
6Aug/110

Sandip Chitale’s Blog: LinearLayout gravity and layout_gravity explained

Posted by noslen

 

 

Sandip Chitale's Blog: LinearLayout gravity and layout_gravity explained.

Very helpful visualization of how gravity and layout_gravity play with each other.

Filed under: android No Comments
8Jul/112

Android bluetooth example

Posted by noslen

I'm starting more detailed work on Bluetooth sharing.
I found this snippet of code on the dev guide that looked very similar to a TCP socket which I did a while ago.
The code below is taken from :
http://developer.android.com/guide/topics/wireless/bluetooth.html

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main Activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

    /* Call this from the main Activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

 

Filed under: android 2 Comments
7Jul/110

Lawrys Digital Dinner Bell – Android Market

Posted by noslen

My first major android app!

Over 10k downloads already!

Lawrys Digital Dinner Bell - Android Market.

Filed under: android, general No Comments
31May/111

android and JDK compliance – how to fix @override errors

Posted by noslen

 

 

From time to time when working with other people's projects I run into build errors that are caused by the @override annotation. It took me a while to understand why that was happening as I was usually able to solve it by simply deleting the @override annotation or adding it where needed.

an example of an error caused by @override

Turns out the problem is because of of a mismatch in the JDK different developers are using.

Starting in Java 6 the @override annotation no longer applies to interface methods. So most methods derived from an interface no longer need them. They are however needed with Java 5.  The problem arises if a dev with Java 5 creates an android project, By default the project will get set with whatever JDK compliance is valid in their environment, in this  Java 1.5. If a developer with java 6 opens the same project all  @override annotations will be flagged as errors when appearing above interface methods.

Thankfully, this is easy to fix.

You simply need to go to your android project properties, choose the java compiler and select the appropriate JDK compliance level for your environment.

Project Properties > Java Compiler

Click enable project specific settings to be able to select the appropriate JDK version to use.

Don't know which hava version you have? No problem. Simply type java -version in your command line to get that info. It will look like below

how to check your jdk version

Don't want to have to do this every time you and another developer have mismatching JDKs? You can configure it as a default on your workspace.

From the same Java Compiler properties window, click "Configure Workspace Settings"

That will bring you to the window below where you can choose your default Compiler compliance level for your JDK Compliance.

configure your default workspace settings

As per usual i'd like to thank the fine folks @ stackoverflow for their help.

Filed under: android 1 Comment
25May/110

And AA steps it up a notch

Posted by noslen

image

Filed under: general No Comments
22May/110

Android units – pixels, density, dpi, dip, dp, dps, sp, sip | Edwin Evans

Posted by noslen

A bit more on dealing with android units.

Android units – pixels, density, dpi, dip, dp, dps, sp, sip | Edwin Evans.

 

 

Filed under: android No Comments
15May/110

adb command line: How to launch an app from adb

Posted by noslen

If you ever need to launch an app from the adb command line here's how.

First, if you haven't added the platform tools to you %PATH% environment you should. It's a great time saving shortcut.

Ok now that you're cool like us on to the adb command:

You need to use the am command which is an adb shell command. adb has its own set of commands, adb shell has its own other set.

So what does the syntax for the am command look like? First i'll show you the command and then i'll explain it.

Ok so there it is. adb shell am start -a android.intent.action.MAIN -n com.rga.sony/.ZeusMain

Here is also a pretty great explanation:

http://www.anddev.org/using_the_am-tool_start_activities-intens_from_a_shell-t368.html

 

Filed under: android, general No Comments