Android interface

How to communicate through an interface in Android between an activity and a custom view.

In this example we want to press a Button inside a custom-view and do something to another view on the activity layout.


Layout activity:


android:layout_height="wrap_content" />

Custom view Layout :

  android:layout_height="wrap_content" />



public class CustomView {
 private View mView;

 private void init() {
    mButton = (Button) findViewById(;
 mButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      //when click is done, it is called the interface method that
      //it is implemented in activity (not in custom view) so view is shown up/more
        if (mOnButtonListener != null) {

    //set the listener
    public void setListener(OnButtonListener listener) {
      mOnButtonListener = listener;

    //interface that communicate with the activity
    interface OnButtonListener {
      void onButtonPressed();

Inside the activity where the view has to be shown/other we have to implement the interface method.

[...] implements CustomView.OnButtonListener {

  //onCreate method
    mCustomView = (CustomView) findViewById(;
mView = (View) findViewById(;

  //implement interface
  public void onButtonPressed() {
   //do something with the mView

Grant|Revoke permission with adb [Android M]

Hey there, Android M RC is almost out and one of the new feature is “manage permissions“, let’s see hot the grant or revoke a permission with adb. In this example, I am going to show how to revoke the camera permission at the Google Camera app.

First of all, let’s connect a phone on your computer and let’s see how many apps are installed:

>> ./adb shell pm list packages

You will get a list of package names:



Now we are going to get the list of all the permissions:

>> ./adb shell pm list permissions -d -g

And here how to revoke the permission:

>> ./adb shell pm revoke  android.permission.CAMERA

and here how to grant it back:

>> ./adb shell pm grant android.permission.CAMERA




OpenSTF, share your testing devices and.. [part-1]

As a mobile QA Engineer, every times that a new features is created I need to check it on many devices (with different screen size, different O.S, and so on) what do I need to do in this case? Take every single devices, connect it to my computer, and begin testing…. Simple right? What can be wrong, nothing? The world is not always bright! Let’s see:

  • I do not find the device
  • Or it has battery low
  • Or I borrowed it to a collegue and he/she forget to give it back to me
  • I waste time to find, plug/unplaug the cable
  • [The list can be much longer, better stop here... You already got what I want to say]

Finally STF came to me! So far, my team and I try it with a raspberry PI, I can say it worked fine and we all got a positive feedback from it. So we want to push it at the next level and build our Smartphone Testing Farm. Some (so far) pro/cons of STF:


  • Devices always connected, fully charged and available
  • Easy to use
  • Sharable with everybody (Designers, Boss, …)


  • Need to invest some money for the smartphone farm (Hub usb, computer, cables)
  • The persone that use the device, need to release it when it is done, people can forget, so the devices keep locked

- Next -

  • Build a smartphone farm and use it in depth
  • Using SPOON to execute all the automated tests on all the devices

- Reference -

STF Website: OpenSTF

STF project source:

Spoon website: 


To be continued…

MODIFY /etc/hosts on Genymotion device

Need to be a root user:

 sudo su

Once you are a root user, need to execute:

./adb shell

When you are in, open the file and edit it.

 vi /etc/hosts

Espresso Google – Let’s see some examples

Doubt about Android testing? I show you what I did…
After long time using Robotium, I decide to try Google Espresso and I have to say I’m happy of this change.

Why I’m happy about it?

Pro Espresso:

    Byebye solo.sleep()! Yes you read well, Espresso understand when the UI is still running before let you fail the test
    Fast. Fast. Faaaaast.

Pro Robotium:

    Much easy to write code. The learning curve is faster than Espresso
    Easy to read test. They look more readable even from a non technical person

….But I did not find good example of Espresso, so I decide to create a public repository on Github with many simple (and not only) examples about using Espresso. Clone it or just start watching it:

Here a fast Espresso sheet:

How to get a View while testing [with Android Espresso]

I was trying Google Espresso for testing Android apps (it is pretty nice and fast), but I got stuck while I was trying to tap a custom (in my case was a checkbox and a linked text) view and the onView(withId(; fails every times. Text link was clicked but checkbox wasn’t checked.

Question? Can I get the view and use coords to perform click?
Answer: Yes!
Note: The custom view is not on Activity test rule

Let’s see how to do:

public class EspressoTest {

  public ActivityTestRule mHomeRule = new ActivityTestRule<>(Home.class);
 * Need to add HONEYCOMB because the clickXY() action needs
 * YOUR_ACTIVITY_NAME = the Activity that contains the view
  public void letsClickWithCoords(){
// Click the button that open the activity that contains our custom view
Instrumentation.ActivityMonitor monitor =  getInstrumentation().addMonitor(YOUR_ACTIVITY_NAME.class.getName(), null, false);
  Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
 [ something until the view is visible…]
 View v = currentActivity.findViewById(;
 onView(withId(, v.getY()));

 public static ViewAction clickXY(final float x, final float y){
   return new GeneralClickAction(
   new CoordinatesProvider() {
    public float[] calculateCoordinates(View view) {
     final int[] screenPos = new int[2];
     final float screenX = screenPos[0] + x;
     final float screenY = screenPos[1] + y;
     float[] coordinates = {screenX, screenY};
     return coordinates;

Menu Animation for Android

Published code for a cool menu animation.

Grab the code:

AdMob + Google Play Services + Android Studio

I have to admit it, I spent few hours before clearly understand how to include adMob banner inside an Android app using Android Studio, here it is how to do!

  1. Update build.gradle
  2. Add permission in AndroidManifest
  3. Modify layout
  4. Load the request in onCreate()
1. We have to first add a new dependencies in build.gradle (5.0.77 modify this number using the last play services available version)
dependencies {
    compile ''
2. Let’s add some mew permissions and somethings more:
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

        <meta-data android:name=""
            android:value="@integer/google_play_services_version" />
        <activity android:name=""
3. Now I’m going to modify the layout to add the banner – Remember to put your adUnitId (Get it from adMob website)
<LinearLayout xmlns:android=""
    ... >

    < android:id="@+id/adView"


4. In our last step we have to load the banner. I do it during onCreate.

    protected void onCreate(Bundle savedInstanceState) {

        // Look up the AdView as a resource and load a request.
 AdView adView = (AdView) this.findViewById(;
 AdRequest adRequest = new AdRequest.Builder().build();


And now you are ready to get money :)

Android Communication

In this example I’m going to show how to communicate between fragment-activity, activity-activity and how to set a fragment variable from the activity.

In short: fragment_1 –> activity_1 –> activity_2 –> fragment_2

In this first example we will see how a fragment (fragment_1) passes a value (list item position in our case) to the activity (activity_1) by using an interface.


public class ItemListFragment extends Fragment implements AbsListView.OnItemClickListener {
public static final String POSITION_MAIN_LIST = "ItemListFragment.list.position";
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ((OnFragmentInteractionListener) getActivity()).onFragmentInteraction(position);

    /* interface */
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(int position);



..and here how to get the position and start another activity (activity_2) passing the position value.

public class MainActivity extends ActionBarActivity implements ItemListFragment.OnFragmentInteractionListener {

    public void onFragmentInteraction(int position) {

        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra(ItemListFragment.POSITION_MAIN_LIST, position);



In this activity we get the position value by reading the Intent and pass it to the fragment_2 by calling a fragment method (updatePosition()) directly from the activity.

public class SecondActivity extends ActionBarActivity {

    int position;

    protected void onCreate(Bundle savedInstanceState) {

        Intent i = getIntent();
        position = i.getIntExtra(ItemListFragment.POSITION_MAIN_LIST, 0);


    public void onItemListSelected(int pos) {

        ItemGridFragment gridFragment = (ItemGridFragment) getSupportFragmentManager()

        if (gridFragment != null) {


..and this is the method called by the activity.

public class ItemGridFragment extends Fragment implements AbsListView.OnItemClickListener {
    int positionMain;
    public void updatePosition(int pos) {
        this.positionMain = pos;

Autoplay video after ajax call

Here I’m going to show how to play a video automatically after an ajax call.

// this is a snapcode of the js that let the audio play

document.getElementById(“srcVideo”).src=xmlhttp.responseText; document.getElementById(“srcVideo”).play();

And here the HTML code used for the player (video source)

<div id=”player-overlay”>

<video id=”video”>
<sourcesrc=”video_file.mp4″ type=”video/mp4″ id=”srcVideo” />