[Unity] Keep a gameObject into a specific area

If we have a gameObject that needs to move into a specific area, we can use the Mathf.Clamp(value, min, max), this check that the value we have is min < x < max.

In the example below, we take the gameObject position X and Z and said that must be inside a range.


  Rigidbody rb;

  void Start() {
rb = GetComponent<Rigidbody>();

  void FixedUpdate() {

float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;

rb.position = new Vector3 (
                  Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)


[Unity] Serializable classes for a better interface

If we are going to have a long list of “public” params, so it looks something like this:

we can organize them by grouping into a class (in this case called Boundary), so we can have a better and clear game interface like this:


How to do it?

Just create a new class and remember to add [System.Serializable] on top of your class otherwise Unity is not able to recognise it and you are not going to see anything on the interface

public class Boundary {
public float xMin, xMax, zMin, zMax;

public class PlayerController : MonoBehaviour {

public Boundary boundary;


// Use it like this: boundary.xMin, etc..





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(R.id.button);
 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(R.id.custom_view);
mView = (View) findViewById(R.id.view);

  //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 com.google.android.GoogleCamera  android.permission.CAMERA

and here how to grant it back:

>> ./adb shell pm grant com.google.android.GoogleCamera 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: github.com/openstf/stf

Spoon website: square.github.io/spoon/ 


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: https://github.com/onivas/EspressoPlayGround

Here a fast Espresso sheet: https://code.google.com/p/android-test-kit/wiki/EspressoV2CheatSheet

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(R.id.checkbox_id)).perform(click()); 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);
 [..do something until the view is visible…]
 View v = currentActivity.findViewById(R.id.checkbox_id);
 onView(withId(R.id.paid_options_tos)).perform(clickXY(v.getX(), 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: https://github.com/onivas/MenuAnimation

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 'com.google.android.gms:play-services:5.0.77'
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="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name="com.google.android.gms.ads.AdActivity"
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="http://schemas.android.com/apk/res/android"
    ... >

    <com.google.android.gms.ads.AdView 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(R.id.adView);
 AdRequest adRequest = new AdRequest.Builder().build();


And now you are ready to get money :)