Know your bugs in Android

In this post you learn to how to know your bugs in your application.

The first step
download library
and put the .jar in libs subfolder of your android project.

The second step
Add the permission of INTERNET in the manifest.xml of your android project.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.positionscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.thedeveloperwordisyours.acra"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The last step

add ACRA.init(this); in your class while extends Application

import org.acra.*;
import org.acra.annotation.*;

@ReportsCrashes(formKey = "", formUri = "http://www.yourselectedbackend.com/reportpath")
public class MyApplication extends Application {
  @Override
  public void onCreate() {
    // The following line triggers the initialization of ACRA
    super.onCreate();
    ACRA.init(this);
  }
}

RoboGuice for dependency injection in Android

RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google’s own Guice library.

The first you download
roboguice-2.0.jar
guice-3.0-no_aop.jar
jsr305-1.3.9.jar

Add the required libraries to the libs folder of you new android project.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TheDeveloperWorldIsYours" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
package com.thedeveloperworldisyours.inject;

import com.google.inject.Inject;

import android.os.Bundle;
import android.view.Menu;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;
import android.location.LocationManager;
import android.widget.TextView;

public class TheDeveloperWorldIsYours extends RoboActivity {

	@InjectView(R.id.text)
	  TextView name;
	
	@InjectResource(R.string.app_name)
	  String myAppName;
	
	@InjectResource(R.string.action_settings)
	  String myActionSettings;

	@InjectResource(R.string.hello_world)
		String myHelloWorld;
	
	  @Inject
	  LocationManager loc;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_developer_world_is_yours);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.the_developer_world_is_yours, menu);
        return true;
    }
    
}

Singleton in Android

A singleton in android it’s the same that in Java. A singleton is a class for which only one instance can be created provides a global point of access this instance. The singleton pattern describe how this can be archived.

Singletons are useful to provide a unique source of data or functionality to other Java Objects. For example you may use a singleton to access your data model from within your application or to define logger which the rest of the application can use.

public class Singleton {
    private static Singleton mInstance = null;
 
    private String mString;
 
    private Singleton(){
        mString = "Hello";
    }
 
    public static Singleton getInstance(){
        if(mInstance == null)
        {
            mInstance = new Singleton();
        }
        return mInstance;
    }
 
    public String getString(){
        return this.mString;
    }
 
    public void setString(String value){
        mString = value;
    }
}

In ActivityA We set the string value.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class ActivityA extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        //Show the string value defined by the private constructor
        Toast.makeText(getApplicationContext(),Singleton.getInstance().getString(), Toast.LENGTH_LONG).show();
 
        //Change the string value and launch intent to ActivityB
        Singleton.getInstance().setString("Singleton");
        Intent intent = new Intent(getApplicationContext(),ActivityB.class);
        this.startActivity(intent);
    }
}

In ActivityB We get the string.

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class ActivityB extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        //Show the string value of the singleton
        Toast.makeText(getApplicationContext(),Singleton.getInstance().getString(), Toast.LENGTH_SHORT).show();
    }
}

Listener in Android

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

The object which is being watched is called the subject.The objects which are watching the state changes are calledobservers or listeners.

How to make in android is the same that in java.

The first

1) To Create a class which name is MyListener.
-Interface.
-Variable (listener).
-Void.
-To call the void if (listener!= null).

package com.thedeveloperworldisyours.simplelistener;

public class MyListener {
	public interface Listener {
        public void onStateChange(boolean state);
    }

    private Listener mListener = null;
    public void registerListener (Listener listener) {
        mListener = listener;
    }

    private boolean myBoolean = false;
    public void doYourWork() {
    	myBoolean = true;
        if (mListener != null)
            mListener.onStateChange(myBoolean);
    }
}

The second
2)To use the listener
-Declare the variable
-new Listener
-to use the listener’s void

package com.thedeveloperworldisyours.simplelistener;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class TheDeveloperWorldIsYours extends Activity implements MyListener.Listener{

	private TextView mTextView;
	private MyListener mListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_the_developer_world_is_yours);

		mTextView = (TextView) findViewById(R.id.tvTheDeveloperWorldIsYours);
		mListener = new MyListener();
		mListener.registerListener(this);
		mListener.doYourWork();
	}
	@Override
	public void onStateChange(boolean state) {

		if (state) {
			mTextView.setText("on");
        } else {
        	mTextView.setText("off");
        }

	}

}

Download the example here

Place in Relative Layout

To place in  a relative layout is very esay. You can place to use two properties

android:layout_alignParent
android:layout_center

placeInRelativeLayout

If you want to do this, just follow this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TheDeveloperWorldIsYoursActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/leftTop" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/leftBottom" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="@string/right" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/rightTop" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/rightBottom" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/centerTop" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/centerBottom" />

</RelativeLayout>