Skip to main content

Posts

Showing posts from June, 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.

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 t

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> <Line