Badblog

welcome to our blog

We are Learncodz.


Posts

Comments

The Team

Blog Codz Author

Connect With Us

Join To Connect With Us

Portfolio

    Posted by: Unknown Posted date: 16:48 / comment : 0

    Android represents a big potential market. It is also the most open of the "big" phone platforms. All the tools you need to create an Android App are free. You don;t need to pay anything to join a club to create and even distribute your Android App. If you want to sell using a well known marketplace such as Google Play or Amazon then there is usually a small annual fee, but you don't have to use any particular distribution method.
    All that stands between you and your Android app is your imagination and programming ability. I can't do much to improve your imagination but I can help with the programming side of things. In this ebook I will show you the fundamentals of Android programming. Not the tips and tricks, but how to think about what is going on - that is the general principles that will make it possible for you to master anything that you encounter that is new in the future.
    What do you need to know to get started?
    You need to be able to program in a modern object-oriented language. Java would be best as it is the language used to create Android apps but C#, C++, VB or anything similar are close enough in spirit to Java for you to be able to cope. You might well need to look things up about the specifics of particular features of Java but most of the time it should be obvious or obvious with the help of a few comments.
    It isn't necessary to be an expert programmer because for a lot of Android programming you are simply using the features and facilities provided - that is a lot of Android programming is just a matter of following the rules. However if you hope to create something unique and useful you will at some point have to add something of your own - and here creativity and skill are required. So you might not need to be an expert programmer to get started but you need to become one by the time you create your amazing app.
    Fortunately practice is a good teacher and so learning to code for Android will actually help you learn to code.
    Getting Started
    This is a really good time to get started with Android because Google have released Android Studio which is an easy to use Android IDE - Integrated Development Environment. Before this you had to use the Eclipse IDE and setup the SDK and other pieces of software needed. This wasn't difficult but Android Studio makes it easy and it makes programming Android easy. Put simply it is the way of the future and so worth your investment in learning it.
    At the moment there is one simple drawback to using Android Studio - it is only available as a Developer Preview. This means that there are parts of it that are unfinished and there might well be bugs.
    However in practice it works well and it is based on well tested code. In other words it isn't too early to get started using it. As Android Studio changes the chapters of this ebook will be updated, but there is still a possibility of a slight discrepancy between description and the way the IDE works.
    So to get started. First make sure you have the latest Java JDK installed. If you don't have Java installed visit:
    http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Once you have Java setting up Android Studio is easy:

    Download the Android Studio package from Android Studio Home page.
    Install Android Studio which also intalls all of the Android SDK and tools you will need:
    Windows:

    Launch the downloaded EXE file, android-studio-bundle-<version>.exe.
    Follow the setup wizard to install Android Studio.

    Mac OS X:

    Open the downloaded DMG file, android-studio-bundle-<version>.dmg.
    Drag and drop Android Studio into the Applications folder.
    Linux:

    Unpack the downloaded Tar file, android-studio-bundle-<version>.tgz, into an appropriate location for your applications.
    To launch Android Studio, navigate to the android-studio/bin/ directory in a terminal and execute studio.sh.
    You may want to add android-studio/bin/ to your PATH environmental variable so that you can start Android Studio from any directory.
    On some Windows systems, the launcher script does not find where Java is installed and you will find that you can't run the newly installed Android Studio. It appears to start and then just vanishes.
    If you encounter this problem, you need to set an environment variable indicating the correct location of the Java JDK.
    Select
    Start menu > Computer > System Properties >
                           Advanced System Properties.

    Then open
    Advanced tab > Environment Variables,
                          add new system variable

    and setup JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.7.0_21.
    There is a second problem that stops some installations working. As well as JAVA_HOME being set to the jdk directory you also need a PATH set to the bin directory. Use the same method of setting the environment variables but select the existing path variable and use the Edit button to add a path to the jdk bin directory, for example ;C:\Program Files\Java\jdk1.7.0_21\bin.   Note, you have to use a semicolon separator between each of the directory paths specified as part of the PATH variable.
    Now you should be able to run Android Studio. If not the most likely cause of the problem is the JDK and so re-installation is a best first option.

    Your First program
    NOTE:

    One of the earlier versions of Android Studio provided a template for a Blank Activity that always added a Fragment to the user interface. This made things more complicated so we provided a custom template that didn't use a fragment. This is no longer necessary as long as you are using hte latest Android Studio. 

    The custom template is still available in the CodeBin and if you want to know more about how the template works see:Custom Projects In Android Studio. But, to be 100% clear, you no longer need it.

    When it finally gets going you will see the Welcome screen:

    If you have already created some programs you might well see them listed in Recent projects. Assuming this is your first project select the New project option.

    You can ignore the details of the new project for the moment. All you have to do is supply a name for your application - HelloWorld in this case. 
    By default the app will run on anything from Android 2.1 up and will be stored in your personal directory under AndroidStudio. You an also supply a package name which is used to identify the app - in theory forever. It is usual to use a domain name you own in reverse order so as to avoid naming clashes but notice that not all characters allowed in a URL are allowed in a Java package name.
    If you select the Create Custom Launcher Icon in the first dialog box you will be given the chance to specify a custom icon for your app. At this stage you have enough to worry about so don't select this option. There is time to work on custom icons when you have an app that is worth the effort so just click the Next button to see the next dialog box.

    The next dialog box asks you to select the type of Activity you want to create. Every Android app consists of at least one Activity -  you can think of it as the basic unit of app construction.
    We need to talk much more about Activities later but for the moment simply select Blank Activity - this is the simplest of all of the projects that Android Studio can generate for you.

    Finally you have to give your Activity a name - for the moment just accept the default of MainActivity. You can also use this dialog to select addtional features that might want such as a Fragment or an Action bar. For the moment leave this set to None as you really do need the simplest project to get started with. In fact you can carry on selecting None until you have discovered what all the other options are - and they are useful.

    When you click the Finish button Android Studio starts to build your project. It can take a few minutes, especially the first time you try it,  so don't assume it has crashed.

    Basic Project Structure

    When the project has finished building all of the files created can be viewed by opening the Projects tab. The main thing to notice is that there are a great many folders and files.
    It seem almost unbelievable that the simplest Android app you can create involves so many files.
    Don't panic.
    Most of the files that have been created are autogenerated and most of the time you don't need to know anything about them let alone open or edit them. In fact opening and editing autogenerated files really isn't a good idea.
    For our simple program there are only two important files one that determines the Activity's behavior
    MainActivity.java
    and one that determines most of the visual appearance
    Main_Activity.xml
    You can set which activity is the one that the system starts but by default it is the single activity that you created and named when you set up the project. In this case we named the activity MainActivity and its layout file Main_Activity - but you could change these defaults.

    The MainActivity.java file can be found in the src/main/java directory and the Main_Activity.xml file can be found in src/main/res/layout directory.
    The src directory is from your point of view where most of the construction of your app occurs so make sure you know where it is. The src/main directory is where you keep the files concerning the main Activity and the Java code goes in the java folder and the various resource files belong in the res directory.

    An Android app is made up of one or more Activities.
    You can think of an Activity as being something like a web page complete with HTML to determine what displays and JavaScript to determine what it does.
    In the case of an activity the layout is determined by the XML file in resource (res) directory and the behavior is determined by the Java  code in the Java directory.
    The XML can be thought of as a markup language much like HTML or XAML. 
    It defines an initial layout for the screen when the app first runs. It is possible to generate new layout components are run time from the Java file. In fact if you really want to you can dispense with the XML file and generate everything from Java but as you will discover the XML markup approach is much the best way to do the job - because of the availability of the Designer.
    Let's take a look at the two files that have been generated for our initial Hello World application beginning with the XML layout.
    Hello Designer

    First the XML layout file. Double click on activity_main.xml file in the Project tab and the file will open.
    What you see is an XML file that specified the layout in text form and a representation of the layout to the far right in another window. You can work with the XML directly to define where all the buttons and text goes and later you will learn at least how to edit it when things go wrong or to fine tune it but - Android Studio provides you with a very nice interactive editor and this is worth using.
    If you look at the bottom left you will see two tabs - Design and Text:

    You can swtich between editing the XML as text and editing it in the drag-and-drop designer simply by clicking on the tab.
    If you now click on the tab the window will display the designer but be patient the first time you do this it might take a few moments.
    The designer looks a little too much to take in when you first see it but you will quickly get used to it.
    On the left is a Palette of all of the components - buttons, text, checkboxes and so on - that you can place on the design surface.
    In the middle is the design surface and this defaults to the screen size and appearance of the Nexus 4. You can select other devices to work with.
    On the right you have the Component Tree which shows you the structure of your layout - its the same as the nesting structure of the XML file. Below the Component Tree you have the Properties window that can be used to set the properties of any component in the layout.
    If you have used any drag-and-drop designer then this one will be familiar to you. If you have struggled with the detailed layout using a markup language be it HTML, XAML or XML then you will like how easy the designer makes building and testing a UI.
    In the case of our sample program the only component uses is a single TextView already containing the text "Hello World".
    You can modify the greeting text if you want to. Select the TextView component either on the Nexus 4 image or in the Component Tree and use the Properties window to find its Text property. Change this to read "Hello Android World".
    Don't worry about the form of the original entry starting with an @ and looking complicated just type the string in as given.

    You can use the designer to create any UI you care to and you really don't have to get involved in the XML that corresponds to the layout.
    The designer will automatically generate the XML needed to create the layout for you and modify it as you change the layout. If you really want to see the XML then all you have to do is select the Text tab at the bottom of the designer window. You should find it fairly easy to understand but leave it to the designer to modify it.
    We will return to the designer and the XML it generates later.

    The Java

    If you double click on the MainActivity.java file you will see the code it contains. Some of the code might hidden but you can inspect it if you wan to by clicking the + buttons to expand it. The important part of the code is:
    package com.example.helloworld.app;import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    public class MainActivity extends ActionBarActivity {@Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    There are two other functions below the onCreate function but ignore these for the moment - they implement features you didn't really ask for - useful but not when you are just getting started. 
    The onCreate function is the only thing that matters at the moment.
    The onCreate function is called when your app is run and it is expected to create the view and do the whatever the Activity is concerned with.
    As our Activity doesn't really do anything much the only thing onCreate has to do is first call the inherited OnCreate i.e super.onCreate to do all the standard things and then use the setContentView function to select the XML file that determines the layout of the Activities screen. The line:
      setContentView(R.layout.activity_main);
    is the most important. It gets the resource object that represents the layout as defined by the XML file created by the designer and makes it the current ContentView i.e. it is what is displayed on the screen.
    That is it makes the connection between the layout you have defined using the designer and the user interface that appears on the devices screen.
    We have more to learn about the resource object R but you can see that its main role is to form a link between your Java code and the resources that have been created as XML files by the designer among others.
    As this is all our Activity does this is all the code we need.
    While I agree it is hardly an "activity" it is enough to see the basic outline of an Android app and to see how to get it running - which is our next job.
    Get Started with the Emulator

    There are two distinct ways of running an Android app using Android Studio. You can use the emulator or a real Android device. Eventually you will have to discover how to run an app on a real connected Android device because the emulator only allows you to test a subset of things.
    However for the moment running your first app on an emulator is quite enough to get started.
    Before you try to run the program you need to configure an Android Virtual Device AVD. You can essentially create as many AVDs as you like each with a different set of characteristics corresponding to different hardware.
    All you need to do is use the menu command:
    Tools,Android, AVD Manager.
    and wait while the AVD Manager loads.

    The simplest way to get an AVD ready to use is to select the Device Definitions tab and scroll down until you find the device you want to use for the test. Click to select it and then click the Create AVD and you will be given the opportunity to customize the resources that the device has.


    Now that you have an AVD simply select it in the Android Virtual Devices tab and click the Start button.
    It usually takes quite a time for the AVD to start because it not only has to initialize the emulator it also has to boot Android just like a real device. However you only have to do this once per debug session because the AVD can be reused and the app reloaded each time you make any changes and this is quite a quick operation. Most Android programmers get into the habit of starting an AVD when they first start using Android Studio so that it is ready for testing when the program is.
    Depending on the OS you are developing on you may have to reduce the amount of memory used by AVD  - Windows cannot work with more than 768Mb. You may also find that the emulator runs faster if you select the Use Host GPU option.
    Finally remember to wait until the Android operating system is loaded and you see the familiar home screen before you try to run your app and spend time wondering why it hasn't worked.

    icon allbkg

    Tagged with:

    Next
    Newer Post
    Previous
    Older Post

    No comments:

Comments

The Visitors says
Download Free Software Latest Version