Wednesday, May 21, 2014

Integrating Admob to LibGDX based games

Recently, I had to find out about how to put mobile advertisements of Admob on Android based games which are based on LibGDX. After searching on the web and reading different online articles, I figured it out. Since, I will be using this thing in most of the future LibGDX based games, I decided to write down the procedure I followed in a blogpost so that I will not forget it. So, here I write down the sequence of steps I went through to achieve it. 

1) The very first thing we have to do is creating an account in the Admob website in the URL: https://apps.admob.com
After creating the account, you should login to your account and goto the monetize tab. There you can find the "Monetize new app" option which you should select. Now, you should create a new ad unit by filling the necessary information. Copy the ad unit ID to some temporary place which you need later in your programming codes. 

2) Open your eclipse IDE with the workspace where your LibGDX based game project resides. From the Android SDK manager, install the "Google Play services" library which is listed under the 'Extra' category. Now you have to import this installed library into your eclipse workspace.
Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it. (It should be copied from the original location which is  <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/)

3) Make the imported "google-play-services_lib" a library for the android project by right-clicking on the android project, and following the rest of the instructions in the following URL.
https://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject

4) Add these lines to the android manifest file,

under <application> tag,
    <meta-data android:name="com.google.android.gms.version"             android:value="@integer/google_play_services_version" />

    <activity
            android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

under <manifest> tag,
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

5) Right-click on the android project, select properties. Goto the Android tab and set the build target API to something high as 15. (screenSize & smallestScreenSize attributes are not available in SDK 10.They have been introduced in API level 13.)

6) Now the background configurations are ready. The only thing left is to put some lines of codes to your LibGDX based game. Open the 'MainActivity.java' source file in Android project and add the following content to the 'onCreate' function.

 super.onCreate(savedInstanceState);  
 AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();  
 cfg.useGL20 = false;  
 cfg.useAccelerometer = true;  
 RelativeLayout layout = new RelativeLayout(this);  
 requestWindowFeature(Window.FEATURE_NO_TITLE);  
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);  
 View gameView = initializeForView(new Main(), cfg);  
 AdView adView = new AdView(this);  
 adView.setAdSize(AdSize.BANNER);  
 adView.setBackgroundColor(Color.WHITE);  
 adView.setAdUnitId("our-admob-adunit-id-should-go-here");  
 AdRequest adRequest = new AdRequest.Builder().build();  
 adRequest.isTestDevice(this); //comment out later  
 adView.loadAd(adRequest);  
 layout.addView(gameView);  
 RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
 adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
 adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
 layout.addView(adView, adParams);  
 setContentView(layout);  

For more information, please refer to the URL: https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

Cheers! :)

Sunday, May 4, 2014

Creating e-books from printed books without using a scanner

There are times where we cannot find a printed version of a book which we really want to read or refer. E-books or the soft versions of those books is the only solution in such moments. However, not all books available as soft copies in the Internet. Then, how people make e-books with those printed books? The best way would be scanning each page of a book and get a pdf document. But what if we don't have a scanner? Then the low quality option is to take photographs of each page and then create an e-book with those photographs. Obviously, these photograph based e-books are not going to be very convenient like real e-books. Is there any way to create some modest quality e-books from printed books without having a scanner? Yes, there is.

Recently, while navigating here and there on Google Play from my Android device without exacting knowing which app I'm looking for, I encountered something interesting. There is an app called CamScanner for Android. You can take pictures of any document you want and convert the image to a PDF instantly. It provides a very convenient interface to select the border of the document within the picture so that CamScanner app will zoom in, enhance the image and provide a scanned-like PDF document with an excellent quality as compared to just a photograph of the page. In this way, I can photograph all the pages of a book and get a PDF page for each of printed page.

Now we are just almost there. We just have to put all those individual PDF pages together to make a single PDF document. There are so many useful applications available for PC to merge PDF pages to create PDF documents. Since I'm running on Linux, I prefer using PdfMod for this purpose. I have mentioned this tool some time back in an old blogpost when I had to manipulate PDF pages for some other purpose. We can use it to merge those separate PDF pages we took from CamScanner and create a single PDF book. It's kind of cool to make PDF books from the printed books I have and share with those who don't have access to the printed book.

One more little thing. Its not that little as we think. We need to care and respect
about the rights of the authors of the books and printed materials in case we are going to distribute such stuff by creating soft versions of them. It's up to you to think and find out about the copy rights before you try it yourself.

Copying multiple music files from multiple directories to a single destination

I came across another requirement of doing some task quickly by using the power of Linux shell commands. I had a directory called "Entertainment" which contained a huge collection of various kinds of files. One contained a collection of English songs while another contained Sinhala songs. Some other directories contained some miscellaneous things such as relaxing piano tunes, movies and stuff.

Suddenly, oneday I thought I should copy the mp3 music I have into my phone so that I can listen to them on the go. Filtering out the mp3 files from the complex directory hierarchy and copying them to a single destination directory seemed a hard work if I do it manually one by one. This is where the power of Linux shell comes into play a role. I had to search in the web to find the correct combination of commands to do the trick on the way I wanted it. Here is what finally I used to copy the mp3 files distributed over a hierarchy of directories inside the "Entertainment" directory into a single destination which is "~/Desktop/mp3music/".

find Entertainment/ -iname "*.mp3" -type f -exec cp {} ~/Desktop/mp3music/ \;