++dave;: Cucumber for Android!
Very useful post that's helping us probe the mysteries of android + cucumber.
How to control orientation changes for fragments in a FragmentActivity with multiple fragments
I wasted several hours trying to fix this problem so once again i'm paying it forward in the hope it's helpful to someone else.
The problem is that it's difficult to control configuration changes in a fragment activity with multiple fragments when you need fragments to behave differently.
Here is my scenario:
I have a fragment activity that displays different fragments one at a time. The first fragment displayed is not allowed to change orientation, it must be locked in portrait.
However the next fragment I show in the same activity needs to be able to change on both portrait and landscape orientations.
Sure I could have just rewritten my code to have the other fragment start in a different activity but that's besides the point.
Anyway, what you have to do is make sure you can get configuration changes
Manifest code:
Activity code:
//
//
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(fragment!=null && fragment.isResumed()){
//do nothing here if we're showing the fragment
}else{
setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT); // otherwise lock in portrait
}
super.onConfigurationChanged(newConfig);
}
The code above doesn't work by itself. You would think so but there's a problem where you never receive more onConfigurationChanged callbacks after you call setRequestedOrientation.
Luckily we can take advantage of the lifecycle of our fragment to help our activity get out of it's funk.
When our fragment resumes we can do the following:
Fragment code:
//
//
@Override
public void onResume() {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
..}
I also make sure that my activity goes back to portrait mode if I exit this fragment.
//
//
@Override
public void onPause() {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // set the activity back to //whatever it needs to be when going back.
super.onPause();
}
This ensures that the activity can go back to being stubborn about orientation changes while letting an individual fragment take over configuration changes while it's active.
I hope this is helpful! cheers!
More on testing automation in Android
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.
Testdroid – a great test solution for Testing Automation in Android
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!
Lawrys Digital Dinner Bell – Android Market
My first major android app!
Over 10k downloads already!
adb command line: How to launch an app from adb
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
Android: How to fix Seekbar bar thumb centering issues.

If you ever need to change the height of your Seekbar you may find that the thumb is not centering properly.
The fix for this is actually pretty simple. It just took me a while to find, so i'm posting this in the hopes of helping fellow developers cut down on their search time.
After trying things like gravity and margins and such with no success, i wondered well how the heck does android do it anyway?
That led me to seeing how they define their default seekbar style.
- false
- @android:drawable/progress_horizontal
- @android:drawable/progress_horizontal
- 20dip
- 20dip
- @android:drawable/seek_thumb
- 8dip
- true
Which i then compared to mine.
- fill_parent
- 60dp
- @drawable/slider_thumb
- @drawable/seekbar
What immediately caught my eye was the minHeight and maxHeight attributes. It's possible that when this widget is being laid out android takes these values into account so I tried it.
- fill_parent
- 60dp
- 60dp
- 60dp
- @drawable/slider_thumb
- @drawable/seekbar
Lo and behold it worked!

So what's the answer?
If you need to adjust the height of your seekbar make sure you define the minHeight and maxHeight attributes or your Seekbar because android takes them into account when placing the thumb.

