Android | Andreano Lanusse | Technology and Software Development http://www.andreanolanusse.com/en/development/android/ Where Andreano Lanusse talk about technology, software development, programming techniques, databases, games and more through articles, tutorials and videos Wed, 31 Jan 2018 04:24:27 +0000 en hourly 1 https://wordpress.org/?v=6.3.4 Android Studio preview – The new Google IDE is out http://www.andreanolanusse.com/en/android-studio-preview-the-new-google-ide-is-out/ http://www.andreanolanusse.com/en/android-studio-preview-the-new-google-ide-is-out/#comments Thu, 23 May 2013 03:14:03 +0000 http://www.andreanolanusse.com/en/?p=924 Last week at Google I/O, Google announced the Android Studio, their new Android development environment based on IntelliJ IDEA. Android Studio is FREE and can download the early preview version here. I just installed on my Mac, the IDE supports Windows and Linux as well. I played a little bit during the last 2 days and […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
Last week at Google I/O, Google announced the Android Studio, their new Android development environment based on IntelliJ IDEA.

Android Studio is FREE and can download the early preview version here. I just installed on my Mac, the IDE supports Windows and Linux as well. I played a little bit during the last 2 days and I’m very impressed.

Today’s Android development is supported in Eclipse through the ADT (Android Development Toolkit plugin), and every developer can take advantage of the Eclipse ecosystem. I have been using Eclipse for Android development, but certainly I see space for improvement and more specific features related to Android development.

Android Studio is built on top of IntelliJ’s community version and now with Google Engineer working on that, just imagine what they will be able to produce. I just played few hours with Android Studio, their source code editor ROCKS it goes beyond of features already applied in other IDEs, the Designer is awesome and gives you a view of your application not only for one device, but for many others include tablets. Also, you can easily import your projects from Eclipse in to Android Studio.

Android Studio

Android Studio multi device preview

Watch the following video and you will have a pretty good idea about what I’m talking about.

That’s it for now.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/android-studio-preview-the-new-google-ide-is-out/feed/ 11
Android findViewById vs @InjectView http://www.andreanolanusse.com/en/android-findviewbyid-vs-injectview/ http://www.andreanolanusse.com/en/android-findviewbyid-vs-injectview/#comments Wed, 02 Jan 2013 05:16:38 +0000 http://www.andreanolanusse.com/en/?p=899 When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById inconvenient, as your start using you tend to repeat the same code several times, it add more lines of code, make difficulty to read, maintain, etc. However there is a solution for that […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
When developing Android apps to obtain a reference of view objects represented on your layout you use findViewById. I personally consider findViewById inconvenient, as your start using you tend to repeat the same code several times, it add more lines of code, make difficulty to read, maintain, etc. However there is a solution for that called @InjectView and it’s part of RoboGuice and eliminate this issue and make your code cleaner, smaller and replace the use of findViewById, let’s see how it works.

Let’s assume you defined the following edit text:


We would use the following code to access the edit text using findViewById.

class Main extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        EditText firstname = null;
        firstname = (EditText) findViewById(R.id.fistnameEditText);
        firstname.setText( "My first name is Barack"); 
    } 
}

In order to use @InjectView we have to extend from RoboActivity class (download the RoboGuice from here), we just need to declare our class fields using @InjectView, set each field to the correspondent view object declared on our R.Java, the code will look like this.

class Main extends RoboActivity { 
     @InjectView (R.id.firstnameEditText)      EditText firstname; 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        firstname.setText( "My first name is Barack" ); 
    } 
}

In layouts where we have multiple objects, it will became more visible the benefits of replacing findViewById per @InjectView, check out the following code where I add some extra controls to my layout.

class Main extends RoboActivity { 
     @InjectView (R.id.firstnameEditText)  EditText firstname; 
     @InjectView (R.id.lastnameEditText)   EditText lastname; 
     @InjectView (R.id.photoImage)         ImageView photo; 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        firstname.setText( "My first name is Barack" ); 
        lastname.setText( "My last name is Obama" ); 
        photo.setImageBitmap( null ); 
    } 
}

Hope this small tip can help you.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/android-findviewbyid-vs-injectview/feed/ 4
Android apps connected to Delphi DataSnap Server http://www.andreanolanusse.com/en/android-apps-connected-to-delphi-datasnap-server/ http://www.andreanolanusse.com/en/android-apps-connected-to-delphi-datasnap-server/#comments Wed, 11 Jan 2012 08:15:55 +0000 http://www.andreanolanusse.com/en/?p=771 On the following video I’m showing how you can create DataSnap REST Server in Delphi, expose server methods and call them from an Android application. The video shows all the steps to create the server and the Android application. This is a short recap of the video. In order to have other platforms connecting and […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
On the following video I’m showing how you can create DataSnap REST Server in Delphi, expose server methods and call them from an Android application. The video shows all the steps to create the server and the Android application.


This is a short recap of the video.

In order to have other platforms connecting and interacting with your DataSnap Server you need to enable the REST Interface, and for mobile platforms you can use the DataSnap Connectors that generate proxy classes for Android, BlackBerry, Windows Phone and iOS. In the video I’m showing how to create the Server, let’s take a look at the Server Method implementation.

The first server method called InsertCountry, connect on my InterBase database and add a new record to the Country table, see the implementation below:

function TServerMethods1.InsertCountry(Country, Currency: String;
  out Error: String): Boolean;
var
  Comm: TDBXCommand;
begin

  Result := False;

  DBConn.Open;
  Comm := DBConn.DBXConnection.CreateCommand;
  Comm.CommandType := TDBXCommandTypes.DbxSQL;
  Comm.Text :=
    Format('Insert Into Country (COUNTRY, CURRENCY) Values ( ''%s'', ''%s'')',
    [Country, Currency]);

  if not Comm.IsPrepared then
    Comm.Prepare;

  try
    Comm.ExecuteUpdate;
    Comm.Free;
    Result := True;
  except on E: Exception do
    Error := E.Message;
  end;

end;

The second server method is called GetCountries, and returns all the records I have in the COUNTRY table on my InterBase database. This method returns a DBXReader and DataSnap will convert this automatically to JSON at the moment the Android app calls this method, but since we will use the new DataSnap Connector to generate the proxy class, we won’t need to handle the JSON object and I will explain that after.

The following code represents the GetCountries implementation.

function TServerMethods1.GetCountries: TDBXReader;
var
  Comm: TDBXCommand;
begin

  DBConn.Open;
  Comm := DBConn.DBXConnection.CreateCommand;
  Comm.CommandType := TDBXCommandTypes.DbxSQL;
  Comm.Text := 'Select * from Country';

  if not Comm.IsPrepared then
    Comm.Prepare;

  Result := Comm.ExecuteQuery;

end;

The next step is to create the proxy class for Android using the DataSnap connectors, which is a new feature in Delphi XE2. The generation can be done through command line or via HTTP request on your DataSnap server, and in the video I’m showing how to do that.

The proxy generator generates only the DSProxy.java, all the other files part of the DataSnap Connectors are the representation of DataSnap Framework in Java, for example you won’t need to parse the DBXReader as JSON object in Java, just because we have the DBXReader class in Java and it parses the JSON object for you.

The first step to create a REST connection with your DataSnap Server, which will give me a instance of DSRESTConnection. If your DataSnap Server requires authentication you can use the methods SetUserName and SetPassword to define the credentials.

	private DSRESTConnection getConnection() {
		DSRESTConnection conn = new DSRESTConnection();
		conn.setHost("192.168.254.128");
		conn.setPort(8080);
		return conn;
	}

Now, let’s take a look and see how to execute the server methods, starting with the InsertCountry method as you can see in the following code.

After instantiation of the DSRESTConnection, we pass the REST connection object to the TServerMethods1’s instance, which is the client representation of TServerMethods1 on the server, and from there just call InsertCountry.

Since InsertCountry returns Boolean and also the Error message parameter is passed as reference, InsertCountry methods will return a InsertCountryReturns, which is a static class with two parameters: error that represent the error message and returnValue which represent the value return by InsertCountry.

protected void Insert() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	try {
		InsertCountryReturns ret;
		ret = sm.InsertCountry(editCountry.getText().toString(), editCurrency.getText().toString(), "");

		if ( ! ret.returnValue ) {
			System.out.println(ret.error);
		}

	} catch (DBXException e) {
		e.printStackTrace();
	}

	// Hide keyboard
	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(editCountry.getWindowToken(),0);

}

The next method is GetCountries. We start as before by getting the DSRESTConnection instance. The GetCountries method returns a DBXReader and the way we work with the DBXReader class in Java is very similar the way we work in Delphi, use the Next method to interact through the result, and getValue/GetAsXXXXX to get the column value. See the following code:

protected void Refresh() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	TDBXReader reader;
	try {
		reader = sm.GetCountries();

		ArrayList COUNTRIES = new ArrayList();

		while (reader.next()) {
			COUNTRIES.add(reader.getValue("COUNTRY").GetAsString());
		}

		// Add the COUNTRIES array in to the list view
		list.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, COUNTRIES));

	} catch (DBXException e) {
		e.printStackTrace();
	}

}

This is just a short tutorial explaining how to use DataSnap Connector with Android, take some time and watch the video where I’m going through more details.

The source code is available for download here and download the video from here.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/android-apps-connected-to-delphi-datasnap-server/feed/ 23
Android apps connected to C++Builder DataSnap Server http://www.andreanolanusse.com/en/android-apps-connected-to-cbuilder-datasnap-server/ http://www.andreanolanusse.com/en/android-apps-connected-to-cbuilder-datasnap-server/#comments Thu, 22 Dec 2011 01:48:16 +0000 http://www.andreanolanusse.com/en/?p=747 As part of the 31 days of RAD Studio XE2 Tutorials, in today’s video I’m showing how you can create DataSnap REST Server in C++Builder, expose server methods and call them from an Android application. The video shows all the steps to create the server and the Android application. This is a short recap of […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
As part of the 31 days of RAD Studio XE2 Tutorials, in today’s video I’m showing how you can create DataSnap REST Server in C++Builder, expose server methods and call them from an Android application. The video shows all the steps to create the server and the Android application.

This is a short recap of the video.

In order to have other platforms connecting and interacting with your DataSnap Server you need to enable the REST Interface, and for mobile platforms you can use the DataSnap Connectors that generate proxy classes for Android, BlackBerry, Windows Phone and iOS. In the video I’m showing how to create the Server, let’s take a look at the Server Method implementation.

The first server method called InsertCountry, connect on my InterBase database and add a new record to the Country table, see the implementation below:

bool TServerMethods1::InsertCountry(String country, String currency,
	String &error) {
	TDBXCommand *comm;

	bool result = false;
	DbConn->Open();
	comm = DbConn->DBXConnection->CreateCommand();
	comm->CommandType = TDBXCommandTypes_DbxSQL;

	TVarRec vr[] = {country, currency};
	comm->Text =
		Format("Insert Into Country (COUNTRY, CURRENCY) Values ( '%s', '%s')", vr, 2);

	if (!comm->IsPrepared) {
		comm->Prepare();
	}

	try {
		comm->ExecuteUpdate();
		result = true;

	}
	catch (Exception* e) {
		error = e->Message;
	}
	delete comm;
	return result;

}

The second server method is called GetCountries, and returns all the records I have in the COUNTRY table on my InterBase database. This method returns a DBXReader and DataSnap will convert this automatically to JSON at the moment the Android app calls this method, but since we will use the new DataSnap Connector to generate the proxy class, we won’t need to handle the JSON object and I will explain that after.

The following code represents the GetCountries implementation.

TDBXReader* TServerMethods1::GetCountries() {

	TDBXCommand *comm;

	try {
		DbConn->Open();
		comm = DbConn->DBXConnection->CreateCommand();
		comm->CommandType = TDBXCommandTypes_DbxSQL;
		comm->Text = "Select * from Country";

		if (!comm->IsPrepared) {
			comm->Prepare();
		}
		return comm->ExecuteQuery();
	}
	catch (Exception* e) {
		return NULL;
	}
}

The next step is to create the proxy class for Android using the DataSnap connectors, which is a new feature in C++Builder XE2. The generation can be done through command line or via HTTP request on your DataSnap server, and in the video I’m showing how to do that.

The proxy generator generates only the DSProxy.java, all the other files part of the DataSnap Connectors are the representation of DataSnap Framework in Java, for example you won’t need to parse the DBXReader as JSON object in Java, just because we have the DBXReader class in Java and it parses the JSON object for you.

The first step to create a REST connection with your DataSnap Server, which will give me a instance of DSRESTConnection. If your DataSnap Server requires authentication you can use the methods SetUserName and SetPassword to define the credentials.

	private DSRESTConnection getConnection() {
		DSRESTConnection conn = new DSRESTConnection();
		conn.setHost("192.168.254.128");
		conn.setPort(8080);
		return conn;
	}

Now, let’s take a look and see how to execute the server methods, starting with the InsertCountry method as you can see in the following code.

After instantiation of the DSRESTConnection, we pass the REST connection object to the TServerMethods1’s instance, which is the client representation of TServerMethods1 on the server, and from there just call InsertCountry.

Since InsertCountry returns Boolean and also the Error message parameter is passed as reference, InsertCountry methods will return a InsertCountryReturns, which is a static class with two parameters: error that represent the error message and returnValue which represent the value return by InsertCountry.

protected void Insert() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	try {
		InsertCountryReturns ret;
		ret = sm.InsertCountry(editCountry.getText().toString(), editCurrency.getText().toString(), "");

		if ( ! ret.returnValue ) {
			System.out.println(ret.error);
		}

	} catch (DBXException e) {
		e.printStackTrace();
	}

	// Hide keyboard
	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(editCountry.getWindowToken(),0);

}

The next method is GetCountries. We start as before by getting the DSRESTConnection instance. The GetCountries method returns a DBXReader and the way we work with the DBXReader class in Java is very similar the way we work in C++, use the Next method to interact through the result, and getValue/GetAsXXXXX to get the column value. See the following code:

protected void Refresh() {

	DSRESTConnection conn = getConnection();

	TServerMethods1 sm = new TServerMethods1(conn);

	TDBXReader reader;
	try {
		reader = sm.GetCountries();

		ArrayList COUNTRIES = new ArrayList();

		while (reader.next()) {
			COUNTRIES.add(reader.getValue("COUNTRY").GetAsString());
		}

		// Add the COUNTRIES array in to the list view
		list.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, COUNTRIES));

	} catch (DBXException e) {
		e.printStackTrace();
	}

}

This is just a short tutorial explaining how to use DataSnap Connector with Android, take some time and watch the video where I’m going through more details.

The source code is available for download here and download the video from here.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/android-apps-connected-to-cbuilder-datasnap-server/feed/ 8
Video: Connecting Android applications with DataSnap Server – Delphi Conference Brazil 2010 http://www.andreanolanusse.com/en/video-connecting-android-applications-with-datasnap-server-delphi-conference-brazil-2010/ http://www.andreanolanusse.com/en/video-connecting-android-applications-with-datasnap-server-delphi-conference-brazil-2010/#comments Tue, 21 Dec 2010 20:05:16 +0000 http://www.andreanolanusse.com/en/?p=422 In this second Delphi Conference 2010 Brazil video, Daniele Teti from Italy explain the Android architecture and how to connect Android applications with Delphi DataSnap Servers, take some time and watch/download this presentation, you will learn a great content and see how open is DataSnap.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
In this second Delphi Conference 2010 Brazil video, Daniele Teti from Italy explain the Android architecture and how to connect Android applications with Delphi DataSnap Servers, take some time and watch/download this presentation, you will learn a great content and see how open is DataSnap.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/video-connecting-android-applications-with-datasnap-server-delphi-conference-brazil-2010/feed/ 3