Posts

Showing posts from June, 2012

Show the application only in portrait mode in Android

To show the app only in portrait, in the application tag of your manifest file of the application you need to add one more attribute. The attribute to show the app in portrait is, android:screenOrientation="portrait" Or if you don't want the entire application in portrait mode, only some screens(Activity) to be in portrait mode, then put the same tag, android:screenOrientation="portrait" to whichever activity you want in portrait mode. The activity tag will look like, <activity android:screenOrientation="portrait" android:name=".MyActivity"/> In activity class also, you can restrict to portrait mode by adding
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the onCreate() before calling setContentview()

<uses-feature> tag and required attribute in Android

In this tutorial I am going to explain <uses-feature> tag with respect to camera. When writing an Camera application, first in manifest we need to add the permission as <uses-permission android:name="android.permission.CAMERA"/> Also, we can give additional requirements in manifest file like, only those devices which have Camera can install our Camera app by <uses-feature android:name="android.hardware.CAMERA" required="true"/> If required attribute is false, then if CAMERA is not there in the device, then also app will install but once we mention required attribute is true in uses-feature then if CAMERA is not in the device then we can't install the app in the device.

Hide Title bar in Android

A simple but useful code snippet to hide title bar in Android. The code snippet is

requestWindowFeature(Window.FEATURE_NO_TITLE);

The important thing is, you need to put this code in onCreate() of the activity class. That also before setContentView(). If we put the requestWindowFeature() after setContentView() then we will get an exception. The exception will look like,

E/AndroidRuntime( 277): FATAL EXCEPTION: main E/AndroidRuntime( 277): java.lang.RuntimeException: Unable to start activity ComponentInfo android.util.AndroidRuntimeException: requestFeature() must be called before adding content

So the code will be like, public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); }

Referring Resources in an Application through URI

Normally we refer resources in an application using resource ID. There is an alternative way to refer resources in Android. That is through Uri. This will be useful when we want to load an image in webview using loadUrl() for zooming.

The format is,
android.resource://[package-name]/res-id
eg: Uri.parse("android.resource://com.smartandroidians.sample/"+R.raw.zoom_image);