Saturday, June 18, 2011

Generate Random Numbers in Android

In your apps (especially in game apps) , you may come across the need to generate random numbers. For generation of psuedo random numbers you can use random class from package java.util

First of all you can construct a random number generator using this

        mRandom = new Random(seed);

The seed should be unique for better results. You can use current time for that

                    Time t = new Time();
        t.setToNow();
        mRandom = new Random(t.toMillis(false));

Now to generate a random number from 0 to n, you can use mRandom.nextInt(n). Make sure that n is not zero.
          int rand = mRandom.nextInt(10);

The above line generates a random number between 0 to 9.

Random class also provides other functions such as nextFloat(), nextDouble, nextLong.

Thursday, June 9, 2011

How to publish your android app in Android market

I faced some difficulties when trying to publish my app. So I thought this post may help others.
First you need to have a google account. Then you should pay 25$ (US dollars 25) fees for registration with your credit card.
Next step will be creation of your android app. That is relatively easy. ;-)
But remember your app must have version code and version name. 
Next step is signing your app. This must be done by creating a cryptographic key for your app. To do this you can use the java command keytool.
Again this command at command prompt may give a problem. May be class path variable is not set properly. No problem. You can still go to your jdk directory, then bin subdirectory within  that. Now you have your keytool command. 
You can use the keytool command like
keytool -genkey -v -keystore your-keystore-name -alias your-alias-name -keyalg RSA -keysize 2048 -validity 10000
You must provide a password for the keystore.
Once key is generated, you can go to eclipse IDE and use the option EXPORT in the context menu of project (right click on project name) . You will be prompted for project name, keystore name and location and its password. You should also give the alias name and password and apk file name and location. The apk thus generated is the signed app which can be published into market.
Funnily for me I used to get the error saying the key is not valid till future. I tried many times without much success. Then I realized the reason could because of time zone in India is the cause. So I changed my system date to 1 day in past. Then created the key again. This hack worked. 
You should also upload two screen shots of the app.
Finished all these steps? Published also? Now keep your fingers crossed and pray that people will download and install your app. :-) 

ViewSwitcher in Android - switching layouts for activity

Let us say you want to change the layout in same activity e.g. you want to show a list of urls and when one of them is clicked, you want to show webview on the entire screen. One solution could be to use a dialog, but a dialog will not use the screen area effectively.
In such cases you can use ViewSwitcher.  A viewswitcher can have two child views and only one of them is shown at a time.
The two views you want to use must be wrapped in the viewswitcher  as shown in the xml file below.
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher
   xmlns:android="http://schemas.android.com/apk/res/android"
       android:id = "@+id/viewswitcher"
   ---
   -----
>
 <LinearLayout
    android:layout_width="fill_parent"
    ------->
   
    <GridView 
        android:layout_width="fill_parent"
         -------------------
        ------------------->
    </GridView>     
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
     ------------------>
   
    <WebView
    android:layout_width="fill_parent"
   ---------------------->
    </WebView>
 </LinearLayout>
 
</ViewSwitcher>

In this xml file, the two linear layouts are child views of view switcher. One of them has a gridview and the other has a webview.

Add this line in your onCreate function of the activity to get the viewSwitcher from xml

mViewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);

To start with, first child view will be shown. Now you can display the other child view, in this case webview by calling viewswitcher.showNext() and viewswitcher.showPrevious()

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            -------
            mViewSwitcher.showNext();
                               -------
        }
@Override
    public void onBackPressed(){
        if(mWebView.isShown()){
             mViewSwitcher.showPrevious();
           ---------
        }
    }