Android Gallery with Dots On Scroll

In this tutorial I will show you how to create an Android Gallery and how to create dots or little circles when you scroll the images. So let’s start:

1. First you have to create a new project.
2. After the project is created go to res -> drawable and put your images in the drawable folder. If you don’t have a drawable folder you must create it by pressing right click – new directory and name it “drawable”.

NOTE: You have to run the project on the device if the images are too large. And you should resize your images!


3. Now you have to create another class called “ImageAdapter”. This class will extend the BaseAdapter class and you will have to implement all the methods that the IDE suggests you. After that you will have to create a constructor for the class and an Array of integers where you will store your images ids.

The code will look like this:

4. Now you will have to make some changes in the XML layout. Go to reslayout – main.xml and open the main.xml. Here delete all you have and put the following code :

5. Now go to your activity class (mine is called MyActivity) and put the following code:

Now let’s see the result of our project ūüôā


 

References:http://tjkannan.blogspot.com/2011/09/gallery-with-paging-dots.html
http://developer.android.com/resources/tutorials/views/hello-gallery.html

Run Android Lint from command line

Not many of us use android Lint tool to scan the projects for possible performance or best practices tips. Eclipse has the Lint tool included into adt plugin so Eclipse users can take advantages of this. Ok but what about developers that use intellij IDEA or other IDE for android projects? The answer is simple, you just need the android sdk environment to be installed on your computer. The steps to run a scan on your android project and see the report in a html page are:

  • open command line (cmd in windows)¬†
  • ¬†navigate to “tools” directory located in android installation directory¬†
  • here you just have to type :¬†

command – ¬†¬†lint –html < html_output_path > < your_android_project_path >¬†
¬† ¬† ¬†eg: ¬† ¬† ¬† ¬† ¬† ¬† ¬† lint –html C:report.html C:yourAndroidProject¬†

  • go to html_path and open the html test report ūüôā

Note: that the html_output_path is automatically created by Lint however you can type an already existing html for output and Lint will override the existing html with the new report.

For those that get java.io.FileNotFoundException: C:report.html < Access is denied > it means that the html report file could not be saved on that path.. you could try to change the path to other partition or path other than C: root. ¬†Or you could just create a directory in C: using Windows Explorer, call it “reports” and then you could try again the command like this :¬†lint –html C:reportsreport.html C:yourAndroidProject .

One thing I found. In the results.html I got¬†UnusedResources¬†warning, please make sure that those resources are really unused¬†because¬†many of those resources were used in my project…

You could find more about Lint tool here.

UPDATE: The new Intellij IDEA (version 11.1) has integrated Lint tool. If you want to run the Lint analysis in intellij, just click on the Analyze menu and then Inspect code.
Btw, it seems that has shortcuts to ddms and nine patch too!

Android How to put markers/pins on the map

We’ve already explained how to make an application with a map here. So, now we will try to put markers or pins (or however are they called) on the map, by knowing the Latitude and Longitude, in case you will ever need it and someday you will ūüôā . So lets start:

1. After you create your Android project you have to create another class besides the main one. Let’s call it MyItemizedOverlay. Well after you made this you must extend ItemizedOverlay class and override some methods. The code is just as bellow:

2. Now you have to get a marker image (you can find it on google images) and copy it into the drawable folder (you should name your marker image marker.png like we did for simplicity). If you don’t have a drawable folder press right click on the res directory and create a folder/directory called “drawable”.

3. After you got the image you can go to your main activity class(mine is called MyActivity and extends MapActivity, not Activity don’t forget about this) and put the following code:

And now lets see the result of this code ūüôā

Now as you can see we have 2 locations(point and point2). To first one is shown to us because we set here

So to see the other location which is very far away (in Japan) you need to move the map.

NOTE: The code is mostly from the android developer site

Android PreferenceActivity & SharedPreferences tutorial

I will start with an example. Let’s assume that we want the user to change the date format of your application. For this you must have a class that extends PreferenceActivity, but first you have to create an XML file like this:
1. Make a directory in res called “xml”
2. In the xml directory create a xml FILE called “preferences.xml “put your code for creating the preferences. In our example it looks like this:

3.¬† In res -> values¬† directory create a xml called “array.xml”. Here you will put the items which the the user can select. In our example the code looks like this:

4. Create an activity which extends PreferenceActivity. In this class you will put the layout created at step 2, “preferences.xml” and you will get the name of the item that the user selects to show in the main list just below the title of each preference. The code looks like this:

5. And now if you want to get the data format selected by user into a String to use it in other activities you can do this by using SharedPreferences. Here is an example:

Android PreferenceScreen

If you encounter this problem: “CheckBoxPreference is not allowed here” or other preference tag and your xml file is all red is because you create the xml in res/layout and you must create it in res/xml.
So you have to make a new directory in res called “xml” and this is where your preferences xml will be created. Here is a tutorial.

Android: Scrolling to the bottom of a ScrollView

Iv’e had a lot of trouble scrolling a ScrollView all the way to the bottom in android and I finally got the way to do it ! Here is the code:

Hope it helps somebody !

Android How To Make Google Maps Application

NOTE: DEPRECATED. You should use Google Maps Android API V2
Sometimes in our applications we have to add new features like Maps. So in today’s tutorial I will show you how to create a map for your application and what you need in order to create it.
Firts of all you have to run the application with Google APIs add-on!¬†To do this, go to emulator settings and change the platform to GoogleApis.(example: if you have Android 2.1 Platform you must change it to Google APIs). But if you don’t have it you must download this add-on as is described here:
http://code.google.com/android/add-ons/google-apis/installing.html
Step 1: If you test your application with the emulator you must find¬†debug.keystore. To find it, press the Windows start button and make a search for¬†debug.keystore and press right click on it and select Open File Location. If you can’t find it in this way you should know that in general the debug.keystore is localized in C:\Users\name_of_your_PC\.android. After you find it, make a folder in “C:\”, called “Android”, so you have something like this: “C:\Android”. In this folder copy the¬†debug.keystore

Step 2: Now open cmd and change the default path to where your Java is installed. In my case it is installed in C:\Program Files\Java\jre6\bin. So, to get to this path you have to use the command “cd..” until you have only “C:\”. After this, type “cd Program Files”-press enter button – “cd Java” – press enter button – “cd jre6” – press enter button – “cd bin”- press enter button. Of course if your Java files are installed somewhere else you have to change the directory to where they are. You have to get to Java\bin because here is the keytool.

Step 3: Enter the following in the cmd code and press enter:

keytool.exe -list -alias androiddebugkey -keystore “C:\android\debug.keystore” -storepass android -keypass android

Now you have obtained your certificate fingerprint.

Step 4: Copy the certificate fingerprint obtained and navigate your web browser to http://code.google.com/android/maps-api-signup.html. Here you have to introduce your certificate and google will generate an api key which you will have to use in your android application.

 

Step 5: Now you have to go to your android project and modify the AndroidManifest.xml file by adding the  element together with the Internet permission , like in the picture below:

As you can see here you put the apikey obtained. So the row with the key you will have to change with your key.

Step 6: In your main.xml layout you have to modify the LinearLayout to RelativeLayout and put the code like in the below example:

NOTE: If you are not allowed to put the 

in the RelativeLayout then you should delete the RelativeLayout and let only the maps code like is describe here at step 5.
Step 7: Now you must modify your activity class. To do so, extend your class to MapActivity instead of Activity.

 

And now that you learned how to show a map from your application you should also see the result:

I hope this tutorial helped you.

Android How To Make an AlertDialog

To make an AlertDialog you have to write this code:

And now the result: