The world has changed. We are in the new digital age where, every day, more and more apps are available to the users. So, what really grabs attention, since we have so many options available? Features, user experience, rich design? We can say that, with Delphi, we have all the ability to develop rich applications, both graphically and in terms of functionality.
A major challenge for Delphi developers is the paradigm shift between desktop and mobile platforms. The way to develop and draw a form is different, of course; however, many developers program as if they were still working with a Delphi 7 IDE.
If you are this type of developer and want to learn more about responsive design and how to change your desktop mindset for mobile, then this is an ideal chapter for you.
We have chosen an application already on the market—Instagram—to better elucidate these insights and this paradigm shift.
In this Delphi version of Instagram, you will be introduced to the following topics:
- Mindset between desktop and mobile
- Creating responsive layouts
- Creating buttons with Scalable Vector Graphics (SVG) icons
- Interacting with the camera
- Sharing photos
The knowledge necessary for understanding this chapter is basic. Experience with Delphi is not necessary; minimal knowledge of the IDE and some components, such as buttons, edits, and list views, is sufficient.
To get started, you must have a version of Delphi installed on your computer. For the examples, we will use Delphi Rio, but you can use your version of Delphi, provided it is from Seattle 10 or later.
The code files for this chapter are present on GitHub at https://github.com/PacktPublishing/Delphi-Programming-Projects/tree/master/Chapter01.
The goal of this project is to enable you to better understand the use of visual components and the concept of responsive design, and introduce the concepts of actions and use them to take beautiful photos.
The estimated build time is two to five minutes.
It is necessary to have access to the internet to access sites that offer SVG.
No download is required.
When smartphones became essential and the emergence of the three major mobile OS competitors (iOS, Android, and Windows phone—this last one has been discontinued) took shape, developers were confused. After all, who was not accustomed to responsiveness or smaller screens? Before diving into this new world, you need to understand some important things.
Developing for the Windows desktop, we have some benefits—available memory, ease of interface design (usually in resolutions of at least 1,024 x 768), and others. Most of these features make the programmer's life much more focused on the logic of the application without worrying too much about available computer resources or the like.
When web applications began to emerge with their HTTP protocol in the best request-response style, a new concept of development was created. The programmer had to adapt to the new markup languages (HTML and CSS) and had to separate their application in what we call the server/client side. So, anyone who previously programmed into a single programming language (such as Java or C#) had to adapt to learn markup languages. And, with a need for insertion in the web world, the programmer needed to deal with screen sizes. On the other hand, the applications should be more and more responsive to work more in the varied sizes of screen, from tiny phones to tablets and desktops; not to mention the limitations of the hardware, since they have less processing power and memory than desktop computers.
Mobile has combined the advantages of the previous two along with their disadvantages. If it was said in the web application that everything was on the web, that is to say, for any user access just entering the site, the mobile brought back the desktop concept. The application is installed in the device, and, if you have to upgrade the application, you have to do so for each device. We return to the same situation as desktop applications.
Having limited hardware, including reduced screen size and the absence of peripherals means that the challenge for the mobile developer is more complex
Before you start reading into this topic, you must abandon certain preconceptions, such as the idea that developing for mobile is the same as doing so for web or desktop. No, it's not! Remember that your user is on a limited device and does not have any peripherals for assistance, such as a mouse or keyboard.
Follow these steps to learn about viewing for different screen types:
- Open the Delphi IDE and create a Multi-Device Application. Note that different screen types will have different viewing characteristics:
- Select one Android or iPhone view.
When you work with a different view of the master, Delphi will create a new .FMX file in your project folder. This file will contain the positions of the components for that particular screen format. The paradigm shift is not just about the size or the screen format, but the Delphi IDE helps us identify those nuances.
There are a range of sizes used to determine the view that is used in Android apps. Note that the runtime does not require an exact match to select a view. The runtime chooses the closest match. The following table shows the ranges for each view, specified in landscape coordinates:
|Android 3.5'' Phone||800x500||1,000x600||320|
|Android 4'' Phone||1,1168x730||1,360x850||320|
|Android 5'' Phone||1440x900||1,708x960||320|
|Android 7'' Tablet||1708x960||1,920x1,200||320|
|Android 10'' Tablet||2,400x1,500||2,560x1,600||320|
It is possible to visualize the layout of the forms in the device in real time by looking at the Berlin version design time. Fantastic, isn't it? To do this, download the Fire UI Live Preview application from your platform store (Google Play: http://docwiki.embarcadero.com/RADStudio/Rio/en/FireUI_App_Preview, or Apple Store: https://itunes.apple.com/us/app/fireui-app-preview-10.1-berlin/id1090861997) or install directly via the C:\Program Files (x86)\Embarcadero\Studio\20.0\LivePreview\ path.
FireUI Live Preview is a server/client multi-device tool that allows you to broadcast the active form of your application, in real time, to several devices simultaneously.
In this topic, you will learn how to work with responsive layouts in Delphi using FireMonkey (FMX). For the examples, we will use Delphi Tokyo. Let's have a look at the required steps:
- To start, open Delphi and create a new project by navigating to File | New | Multi-Device Application-Delphi and select Blank Application:
Adjust the style for Android; for example, for the view, we will continue in the Master view.
- Add a Toolbar component to your form.
- Create two buttons inside Toolbar.
- Select the two buttons and align to the left.
- Modify the margins to 5 on each property.
- Save your project and run the application for a Windows platform.
- Then, select the first button that is already configured and modify the StyleLookup property by selecting the cameratoolbutton option. Notice that the button icon is then changed to a specific camera icon.
- Add another Toolbar component, but leave it with bottom alignment. In this toolbar, add a button, align it to the right, and change its margins to 5, as in the top buttons. This button will exit the application.
- Finally, add a TLayout component to the center of your form so that it fills the empty area and align it when using Client.
- Also, modify StyleLookup for the missing buttons. For the button next to the camera, select searchtoolbutton, because, through this, we will search the already-saved images in the device. For the last button, select escapetoolbutton in the StyleLookup property, because, with this button, we will leave the application.
The FireMonkey components have an owner, parent, and children. If you put any component on a form, it becomes the owner and parent of the component.
Using properties such as Position, Align, Margins, and Padding with anchors, you will turn your app in to a responsive application.
To achieve rich interfaces using TLayout, use more than one layout and the organization properties of the child controls in the layouts.
If everything went well here, you will have a form as shown in the following screenshot:
Try creating new views and see the resulting alignment. This is the basic principle for developing a responsive application in FireMonkey.
If you want to view the format with the screen rotated, there is a button to rotate the screen. With this, you can view the layout on the screen in a different format as shown in the following screenshot:
Look at the different types of layouts and their characteristics. For project types where you need to insert many components, use the ScrollBox so you can have a scrolling effect on your form.
In the previous project, we created the main basic layout. Later on, we created photo and sharing events. In this project, we will work with the use of SVG icons, which are infinitely smaller in size in relation to the allocation of space.
SVG is an XML-based vector graphics format that can scale to any size without losing clarity. SVG images and their characteristics are defined in XML text files. That is, they can be searched, indexed, scripted, and compressed. Like XML files, SVG images can be produced and modified with any editing software.
We will use 100% native components in order to use an SVG image. To do this, follow this step by step:
- From the Tool Pallet, select TRectangle. Add in the toolbar created previously, use the property, and align the rectangle by setting it to the center. You will now have a rectangle centered in the toolbar.
- Add a speed button inside this same rectangle. Note that in the Structure pane, the button is contained within the shape. Do the same with the TPath component, but leave it as the child of the speed button. Align both to Center.
- Clear the Text property of the new speed button:
The structure of your application should be like this, with the components nested.
- Okay, now that you have the basic structure assembled on your form, it's time to choose an SVG icon to illustrate this button. The purpose of this button is to execute the share function when clicked. So, we can now share a photo. You can search the internet for various SVG icons. In this example, you can copy the SVG code at http://materialdesignicons.com/ to use in our application.
- The icon used is Instagram. It is important to note that in certain tools or websites, such as materialdesignicons.com, you have the functionality to view the SVG code and also to be able to download the binary file. A simpler way to get the SVG code is to view it online:
In this case, to copy the correct information from the SVG vector, go directly to the d= property and copy its content. This property contains the icon code:
- Once you have the content of the SVG vector, you can include it in your Delphi application using the TPath component. To do so, follow these steps:
- Select the TPath component.
- Select the Data property.
- Input the SVG content and save as shown in the following screenshot:
This is what the Instagram icon looks like:
You can color the icon in the Fill property, including Gradient. To remove the border, just leave it as None in the Stroke.Kind property at Object Inspector:
In the first two steps, we created a new button and centered the toolbar. Notice that this button has a new component, the TPath. The TPath is inside our button. This causes the image, which we copy from the internet appears contained in the component. In steps three, four, and six, we located the SVG icon and copied its content through the d property. With the contents of the image copied, we then go to the TPath component, making it display the same icon that was selected, in this case, Instagram.
There are at least two ways to take a picture with FireMonkey—the first, for the lazy, is to use an ActionList, linking the action of taking a photo to a visual component, such as a button. In this case, your application will request access to the device's camera application.
The second form, however, gives us more freedom in terms of functionality, using TCameraComponent. With it, you can set the resolution, specify the type of camera (front or back) that can be used, choose to use the flash, and you can even create your own flashlight application.
Now that we have the main screen properly built with its main components anchored, we can work on the main functionality—recording moments! Using the same project as the previous recipes, we will finally develop the code to capture images:
- First, add a TImage component to your form and set its alignment to alClient, filling all layout content. We will use this TImage to display the photo. Now, do the same for a non-visual component, called ActionList. Enter this into the form. Change the TImage property and name it as ImgInsta.
- With the right-click on ActionList, open the ActionList Editor option.
With the keyboard, click Ctrl + Insert to open the dialog containing the options; you can also go with the mouse on the New button and insert the new action. The action we want is TTakePhotoFromCameraAction:
In this project, we will capture the image in two ways, the first with a standard action and the other using the IFMXCameraService interface.
- Let's create the DoDidFinish procedure:
procedure TForm1.DoDidFinish(Image: TBitmap); begin ImgInsta.Bitmap.Assign(Image); end;
- In this procedure, the image parameter, which will come from the camera, will be assigned to the TImage component in our form. Remember to assign this procedure to the OnDidFinishTaking event of TTakePhotoFromCameraAction:
procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
In the next few steps, you will understand why this is redundant.
- We can create a new default action, which will fetch images already saved in the device library. Repeat the process to add a new action to your ActionList; however, this time select the TTakeFromLibrary action.
- When selecting the event of this new action, the code is exactly the same code that was made in TTakePhotoFromCameraAction:
procedure TForm1.TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap);
- Bind the respective actions to their execution buttons:
- Let's increase the project a bit more, including an extra button added to the top in the first toolbar, only this time, right-aligned. This button will also take a picture, but, using the IFMXCameraService interface. To perform such an act, include the following units in the uses clause of your form:
uses FMX.MediaLibrary, FMX.Platform, System.Messaging;
- Add another procedure to capture the message coming from the device and thus identify an action to take photos:
procedure DoMessageListener(const Sender: TObject; const M: TMessage);
Its implementation is shown in the following code snippet:
procedure TForm1.DoMessageListener(const Sender: TObject; const M: TMessage);
if M is TMessageDidFinishTakingImageFromLibrary then
- Encode the FormCreate procedure:
procedure TForm1.FormCreate(Sender: TObject);
- To conclude, let's encode the onClick event of the newly added button:
procedure TForm1.Button3Click(Sender: TObject);
Params.Editable := True;
// Specifies whether to save a picture to device Photo Library
Params.NeedSaveToAlbum := True;
Params.RequiredResolution := TSize.Create(640, 640);
Params.OnDidFinishTaking := DoDidFinish;
ShowMessage('This device does not support the camera service');
We divide this project into two parts, where, in the first part, we use an ActionList to take a picture. These actions require very little programming; however, they will not work on Windows platforms. In the second part, when using the IFMXCameraService interface, we have the freedom to implement our codes with a few more lines but greater freedom. We can also include parameters that allow us to, for example, set the minimum resolution and define whether we will save the image to the device as well.
You should also check the TCameraComponent component. You can take your photos without even directing them to your device's camera, including video recordings.
FireMonkey's basic sharing service does not differ from the way we interact to take a photo; that is, we can use a standard action in our ActionList1 to create a new event where we share the photo you just took.
Now that our application is responsive and is properly recording the most important moments through photographs, let's share! Using the same example from previous recipes, we will now develop the button event we created with the SVG icon.
The following steps will show you how to share the photos using FireMonkey:
- Create a new action in the ActionList1, in the Media Library category, to display the share center. Select the ShowShareSheetAction1 action:
- The event is now different; in order to share, it is necessary to assign what you want to share. In our project, we will share a photo. For this, we must program the OnBeforeExecute event of this action:
The following is the code for the OnBeforeExecute event:
procedure TForm1.ShowShareSheetAction1BeforeExecute(Sender: TObject);
- Now, to finish, just assign the newly coded action the Action property of our button. You can use Structure pane and select the TSpeedButton (which has the TPath component is inside it) component with the Instagram logo. With everything ready, when taking a photo it will be filled through the TImage component that can be shared.
The following screenshot shows the form view at development time:
The following screenshot shows the form view running on an Android mobile:
Finally, the application images are still at design time in Delphi and are already running on an Android device.
We have introduced through this mini Instagram clone how to work with basic responsive design, making sure that, regardless of the size of the screen, our controls adjust according to the screen. Then, through the TPath component, we included icons in SVG format, drastically reducing the size of the images and icons thanks to vectorization.
Our code was simple and consisted of only three lines. At a minimum, we can take a photo, choose a photo from the library, and share with friends. All this is done using ActionList and interfaces already done in the FireMonkey library.
In the next chapter, you'll be introduced to the wonderful world of APIs. Not only is it possible to consume Facebook API services, but you can develop your application on the platform by interacting with Delphi.
Delphi is this simple, RAD, and practical!
For more information, I recommend that you read the official Embarcadero layouts material available at http://docwiki.embarcadero.com/RADStudio/Rio/en/FireMonkey_Layouts_Strategies.