How to get the current version of your application programatically in android?

Here is a sample code to get the version of your application in android.
Try changing the version in the AndroidManifest file and check the result in the console.

package pack.coderzheaven;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

public class GetVersionDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int version_no = getVersionNumber(GetVersionDemo.this);
        System.out.println("Version Number : " + version_no);
    }

    public int getVersionNumber(Context context) {
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo("pack.coderzheaven", PackageManager.GET_META_DATA);
            return pInfo.versionCode;
        } catch (NameNotFoundException e) {
            return 0;
        }
    }
}

Note : “pack.coderzheaven” should be replaced by your application package name.

How to make your application FullScreen? or How to hide the Title-Bar in ANDROID?

This simple example makes your activity full screen by hiding the Title-Bar and gives more space for your application.

package pack.FullScreenActivity;

import android.app.Activity;
import android.os;
import android.view.Window;
import android.view.WindowManager;

public class FullScreenActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
    }
}

ANDROID Full screen example

ANDROID Full screen example

;

How to sign Android project apk

This is a very tricky part and initially i was also ran in to strange problems . Before uploading to android market you must do the following steps.

  • Create a Certificate
  • Signing your application apk
  • Finally Zip align the signed apk

First the application you created should be converted to apk format. For this if you are using Eclipse

Right click the project –> Android Tools –>Export Unassigned Application Package.

Then choose the path and Save.

Create a Certificate

For this specify keytool path in Environment Variable as

C:Program Files (x86)Javajdk1.6.0_14bin

The Open CommandPrompt and type

>keytool -genkey -v -keystore myCertificate.keystore -alias m
yKey -keyalg RSA -keysize 2048 -validity 20000

myCertificate.keystore is the name of the Certificate
Validity is given in Days

Now a prompt will ask for

  • Password
  • First and lastname
  • Name of Organization unit
  • Name of Organization
  • City
  • State
  • Country

After entering these fields we get our Certificate

Signing your application

>jarsigner -verbose -keystore myCertificate.keystore D:Listv
iew.apk myKey

Listview is the name of the apk i have exported. Now it will ask for the password and then it will replace the apk with the signed one.
Also you can check your app is signed or not by

$ jarsigner -verify Listview.apk

if Signed the Jarsigner prints “jar verified”

Zipalign the  signed apk

The zipalign tool is provided with the Android SDK, inside the tools/ directory. To align your signed .apk, execute:
So sett up Environment variable as done above

>zipalign -v 4 D:Listview.apk D:SignedListview.apk

Now your apk is ready for upload….