Now how on earth did I miss this puppy? Well that's twice this week I've missed out on knowing something about Google (This and also my quick US Weather Alert system for tornado watches and tornado warnings - will post about this at a later date though)
Now when I'm fiddling around and programming for my Android phone, I usually use Eclipse as my IDE and use it with the Android SDK (API Level 8 and API Level 15 for Android 2.2 and Android 4.0.3 respectively), however upon searching around the internet for some tutorials to further my JAVA knowledge (which in all fairness is very poor) I came across this. It's Google's Android Studio.
Google's Android Studio was released to the public as a "preview release" on May 29th, 2013, so I am going to expect bugs along the way, but I'm hoping it'll be a great IDE to play around in as it'll be more targeted towards Android (whereas Eclipse is a more complex IDE for a lot more languages). Now I've only just installed it, got the screenshot for the above picture and was immediately told that an update was available (thankfully the updates are only tiny, I think it was only 8MB). I've now updated it to the latest version (which at the time of writing this is v0.1.9 Build 130.729444). Also from what I can tell, this IDE runs as JAVA application, so you will need to make sure JAVA is updated on your computer.
Once loaded you are greeted with a simple Quick Start screen, so I'll start with a New Project...
You'll then be given a screen to put your Android application - and remember the module name cannot contain symbols and your package name should be (to keep some order) something like com.AUTHOR.APPNAME, so with my application being called Dale's Demo, my package name is com.dalehay.dalesdemo. I'd also say leave the API Levels as they are set as default as this caters for anyone using Android 2.1 through to Android 4.2.
The next page you get to allows you to set your applications icon. For this demo test, I'm just going to leave it as the default, image, though you can select an image, piece of clipart or just some text, and I will change the icon's shape to a circle and give it a nice purple background colour, then I'll continue and click "Next..."
Proceeding to the next page now gives you various activities that you can select, for this demo, I'll just choose "Blank Activity". (If you're unsure what an Activity is, think of it as a page - so with the options given at the moment, you can choose either a blank page, a page that's fullscreen, a login page, a detailed page, which would best suit a tablet as opposed to a phone, and finally a settings menu).
On the next page, you'll be asked what you wish to name the activity as, which is very handy when you've got loads of activities within your application, but for this step - as usual - we'll leave the default information.
Hit "Finish" and Android Studio will begin building your application project (you will need to be connected to the internet may I add, as it requires Gradle's binary files. After a minute or so the main IDE will open up and once you've clicked "Close" on the "Tips Of The Day" dialog, you'll be presented with this... your workspace.
Now the fun starts...
To those who are familiar with Eclipse and/or JAVA programming, will be able to easily navigate to where they need to in the "Project View" (which you can get to by either clicking the "1: Project" option on the left-hand side, or pressing the shortcut ALT + 1). If you're not familiar, then your application at the moment contains just one page, which is split into two main files (one being the actual JAVA coding and the other being the layout). If you kept things as the default settings earlier, then you will be able to find your main JAVA programming file (fig 1):
[project name] + [module name] + src + main + java + com.*blah*.*blah* - MainActivity
And your layout file can be found (fig. 2):
[project name] + [module name] + src + main + res + layout - activity_main.xml
For the demo, we will sort the layout of it first, so find your layout file ("activity_main.xml") and double-click it to open up. You will then be shown your layout and how it will look one a phone. You can change the resolution/dimensions of the preview phone aswell by clicking the dropdown menu that's currently set as "Nexus 4". On the right, you'll notice a "Component Tree", which is where a list of all the objects that you're currently using on your application will be listed for quick editing. On the left you will be shown all the objects that you can use in your application (and they're easy to use, you just drop and drop). The default layout shows you your application's title ("Dale's Demo"), next to it is the logo that we set up earlier and also a text string saying "Hello World!".
So in this demo tutorial, we will add an extra line of text to the application. So in the left-hand palette, go into Widgets and drag Plain TextView into the center (it should automatically snap into place) and will say "New Text". We will edit this to say something else, however, even though you can manually hardcode the text there, you will get an error along the lines of:
[I18N] Hardcoded string "New Text", should use @string resource
So why do you get this error? Well every piece of text in an Android application comes from a XML file full of strings. This file can be found by going here:
[project name] + [module name] + src + main + res + values - strings.xml
Double click to open it and you'll see something like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Dale\'s Demo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> </resources>
Understanding the strings file is very simple. Currently it's holding 3 different strings and each name is how it gets retrieved in your application, so "action_settings" would return the string "Settings". So now we need to make our own new string, so below the "Hello world!" line, we will add a new string titled "greeting" and have its value as "Hello! I hope you're alright?". Please note that there can be NO spaces in the strings name and all quotation marks need to be escaped (backslashed). After you've done that, click the "Save" button (or by going to File -> Save All). Your strings.xml file should now look like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Dale\'s Demo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="greeting">Hello! I hope you're alright?!</string> </resources>
Let's switch back to your layout ("activity_main.xml") file and double-click the line of text that we previously dragged into the center and a small box will appear with two boxes (one saying "text" and one saying "id"). Click the button with three dots on it ([...]) and a resources window will appear witht he list of the strings from the string.xml file. Select "greeting" and click "OK", and now your application's text will have changed.
Now we've added that string of text to the application, let's build, compile and test the file.
To run your application you will need the Android SDK (which you should already have had otherwise Android Studio wouldn't work), then either an Android Emulator (launched through the AVD Manager) or have your Android phone connected by USB to your computer/laptop (make sure you've also got your phones drivers installed too otherwise your computer will not be able to read the phone).
If you've got one of the above, then just head into the top menu and select "Run", then "Run 'app_name'" (or use the shortcut SHIFT + F10). Gradle will then compile everything together and you will be prompted how you wish to launch your application.
I have my phone connected, so I will use that... and hey presto! It works (in horizontal and vertical view).
I believe that's enough to start off with, considering I was only supposed to talk a bit about Google's Android Studio and it seems to have turned out as a beginner's basic tutorial. I may, over time, do some more tutorials for Android Studio, so please feel free to bookmark my blog or the Android Studio tag results page. There is also a RSS feed available to use from the site too if you wish to subscribe to that too.
For now, have a good day and I hope this hasn't been too boring or dreary.