Tuesday, 22 March 2016

Alert dialog with items list in android



private void opendialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(ctxt);
   builder.setTitle("Title");
   builder.setItems(new CharSequence[]
           {"button 1", "button 2", "button 3", "button 4"},
           new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item
                   switch (which) {
                       case 0:
                           Toast.makeText(ctxt, "clicked 1", 0).show();
                           break;
                       case 1:
                           Toast.makeText(ctxt, "clicked 2", 0).show();
                           break;
                       case 2:
                           Toast.makeText(ctxt, "clicked 3", 0).show();
                           break;
                       case 3:
                           Toast.makeText(ctxt, "clicked 4", 0).show();
                           break;
                   }
               }
           });
   builder.create().show();

}

Wednesday, 6 January 2016

Android Development basics


Android Basics,Android Studio installation,Creating “Hello World” Android Project,Create and Run Emulator.

  • The standard IDE for Android used to be Eclipse, now being replaced by Google’s own Android Studio.
  • Before proceeding lets know about DVM and ART. Dalvik is a discontinued process virtual machine (VM) in Google's Android operating system that executes applications written for Android. Dalvik is open-source software, originally written by Dan Bornstein, who named it after the fishing village of DalvĂ­k, Iceland.
  • Programs for Android are commonly written in Java and compiled to bytecode for the Java virtual machine, which is then translated to Dalvik bytecode and stored in .dex (Dalvik EXecutable) and .odex (Optimized Dalvik EXecutable) files.
  • The successor of Dalvik is Android Runtime (ART), which uses the same bytecode and .dex files (but not .odex files), with the succession aiming at performance improvements transparent to the end users.
  • The new runtime environment was included for the first time in Android 4.4 "KitKat" as a technology preview, and replaced Dalvik entirely in later versions; Android 5.0 "Lollipop" is the first version in which ART is the only included runtime.
Android Studio installation
  • Install - Java Development Kit (JDK) -link to download http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • once jdk is installed check by typing java-version in command prompt.
  • to download android studio go to link https://developer.android.com/intl/es/sdk/index.html
  • click on green button, download, agree terms and conditions, click next, once loaded, prompts to setup wizard , check standard and click next, accept licence and click next one last time to begin downloading the extra components you will need.After a few minutes you will have everything you need to begin building fantastic Android Apps and will move to the welcome screen.You can skip to the next section if you are new to Android development.
  •  set the PATH of your Android software development kit (SDK).
  • From the Android Studio welcome screen, click Configure. Click the SDK Manager option, a new window will present itself to you with checkboxes, folders and statuses across the page.For now lets download the previous version of Android, that being Android 4.4.2 (API 19).
    Click the tick box next to the folder icon for Android 4.4.2, this will select everything within that directory for download.
  • Click Install x packages (x being the amount of packages checked for download) on the bottom right of the SDK Manager. A new window will appear with a drop down list of the packages you wish to install, simply click the root of the drop down list and then click the Accept License radio button in the bottom right.
    You may need to do this for multiple licenses depending on what packages you download.Finally, click the install button at the very bottom right of the window to begin your download.
  • Once the SDK Manager has finished downloading and installing your items you can move onto creating your first Android App!
Creating “Hello World” Android Project

  • Android Studio has a nice little step-by-step tool to help you make your project. Click Start a new Android Studio Project from the Welcome to Android Studio screen: 
  • Studio will present you with your first project creation screen:Enter OMG Android in Application name as shown above. Feel free to put your own name in the Company Domain textfield. As you type, you’ll notice the Package Name will automatically change to create a reverse domain style name based on your Application name and Company Domain.
    The Package Name is used to uniquely identify your app amongst other apps, that way any work an Android device has to be perform on behalf of the app always knows its source and can’t get confused between two apps. iOS developers will recognize this concept as being similar to the Bundle Identifier.
    You can set the Project location to any location on your hard drive – you do not need to follow the screenshot for that one :]
    Click Next at the bottom of the window to progress to the next part of the project setup.
  • For your first app, you’ll use the default – API 16, which is Android 4.1 (Jelly Bean). Every app will have different requirements and you may want to choose something else, depending on the situation.click Next , This screen lets you choose a default Activity for your app.Select the Blank Activity option and click Next.
  • Activity Name. This will give your Activity a name to refer to in code. Once the project setup is complete Android Studio will create a .java class and use the contents of this textfield to give the class a name. This is the name you will use to refer to your Activity inside your code.
  • Layout Name. You’re going to define your Activity in Java, but the layout of everything it will show to the user is defined in a special sort of Android XML. You will learn how to read and edit those files shortly.
  • Click Finish
  • You see your project name, which is familiar. But then there is this Gradle word, and then a mention of Maven in the URL. The benefit of having a modern IDE like Android Studio is that it handles a lot for you. But as you’re just beginning to learn how to use the software, it’s good to know, in general, what it’s doing for you.
  • Gradle is a new build tool that is easy to use, but it also contains a lot of advanced options if you investigate it further. It takes your Java code and XML layouts, and then uses the latest Android build tools to create the app package file, known as an APK file. You can customize your configurations to have development or production versions of the app that behave differently, or add dependencies for third-party libraries.
  • Maven is another project build tool, and it can also refer to the Maven Central repository of java libraries. It is absurdly easy to use Gradle and Maven Central in concert with Android Studio to incorporate all sorts of functionality from the Android development community. Those with an iOS background will know how cool this sort of thing can be from using the CocoaPods tool. You’ll learn more about Maven in Part Three of the tutorial.
  • After a brief moment, Android Studio will finish building your project. The project is pretty empty, of course, but it still has everything it needs already set up so that it can be launched on an Android device or emulator. You will be dropped off in this spot.
  • Android Studio will contain three windows. To the left you have your project folder structure, the middle contains a preview of your layout on a Nexus 5 device and finally the right shows your layout hierarchy as well as attributes if you have a part of your layout hierarchy selected. Before you get into any programming, let’s talk about how you’re going to get this app running. It’s time to say “Hello world!”

Create and Run Emulator - Running Hello world in Emulator
  • Android Studio comes free with the ability to set up a software-based Android device on your computer and run apps on it, browse websites, debug and everything you would expect. This capability is known as the Android Emulator.
    You can set up multiple emulators and set the screen size and platform version for each one to whatever you like. This is great, as with the diversity of the Android platform, you’d otherwise need a large amount of devices for testing.
  • Click AVD Manager. It’s the button in the toolbar that has an Android popping its head up next to a device with a purple display.Android Studio has already created an AVD for you to use. You’ll see a few details about it, notably what type of emulator it is, what API it is using and what CPU instruction set it uses.
  • Lets run through creating a new AVD. Click Create Virtual Device… in the bottom left to begin configuring a new virtual device.Select ex: Nexus S in the list of devices available to you from the phone category and click Next.Your next decision is to decide what version of Android you want your virtual device to run. You will already have one or two available to you thanks to the setup wizard, so lets use one of them.
    Select Lollipop and make sure the one selected has the value x86 in the ABI column. We want the emulator to be running as fast as possible on our x86 computers. :)
  • Click Next once this is done to move to the final screen. The last screen is simply a confirmation of your previous choices with the option to configure some other properties of your device such as device name, startup orientation, and RAM size. For now leave these set as their defaults and click Finish.Congratulations! a fresh virtual device is ready for use to test out your new app.
Now, close the AVD Manager to go back to Android Studio’s main view. Now that you’ve configured everything, there’s but one step left…
Click the Run button. It looks like a “play” icon.A new window will appear, asking you to choose the device you wish to test your App on. You currently have no devices running, so lets start the AVD you just created. Ensure the Launch Emulator radio button is checked and your AVD is selected in the drop down menu, then click OK. U can see Hello world app on emulator.