1) For “static” files, you can use R class and use the browser to
render them
2) For runtime-generated files or “out-of-resouces” class, you can’t
use file:// URIs anymore
2)a) A possible solution: manually open the file and provide file
content as string, mimetype and encoding
2)a)issue) WebView must be used calling loadData method, which doesn’t
allow load content from network (so you can’t make it render external
images)
2)b) Another possible solution: create a content provider which
returns file content, mimetype and encoding by accessing a content://
URI. Theorically you can use WebView calling loadUrl method, which
renders the page correctly
Archive for August, 2009
How to display a local file in the browser?
August 28, 2009[Android] Developing Orientation-Aware Android Applications
August 23, 2009
ost modern, self-respecting mobile operating systems in today’s market support different screen orientations based on the position of the device. Android is no exception. While this feature is often taken for granted, it’s something on which developers spend extra time, ensuring that their applications work flawlessly regardless of screen orientation. This article will show you how screen orientation works in Android and some techniques to make your life easier.
Default Behavior for the Android G1
Create a new Android project and name it OrientationAware. For today’s current Android device (which at the time of writing is the T-Mobile G1), the screen orientation changes only when the keyboard is opened or closed. When the keyboard is closed, the screen is displayed in portrait mode; when it is opened, the screen orientation changes to landscape.
To test the concepts discussed in this article, connect the G1 to your computer. In Eclipse, press F11 to deploy the application that you have just created to the G1. Figure 1 shows the UI of the application when the keyboard is closed. When the keyboard is opened, the orientation changes automatically to landscape mode, as shown in Figure 2.
![]() Figure 1. Portrait: Here’s the device display in portrait mode. |
![]() Figure 2. Landscape: Here’s the device display in landscape mode. |
![]() |
|
| Figure 3. Error: This is an activity that does not know how to react to a change in orientation. |
Reacting to changes in screen orientation is important because it affects the look of your UI. The left side of Figure 3 shows an application designed to display in portrait mode. However, if special care is not taken to adapt the UI for landscape mode, it will look like the right side of Figure 3 when the device changes to landscape mode.
There are three general techniques you can employ to handle changes in screen orientation:
- Anchoring: The easiest way is to “anchor” your views to the four edges of the screen—when the screen orientation changes, the views can anchor neatly to the edges.
- Centralizing: Like anchoring, if your activity has only a few views, you might want to consider centralizing your views so that they are always displayed in the center of the screen.
- Resizing and Repositioning: Anchoring and centralizing are simple techniques to ensure views can handle changes in screen orientation. The ultimate is resizing each and every view according to the current screen orientation.
Anchoring and Centralizing
Anchoring and centralizing can be easily achieved by using RelativeLayout. Consider main.xml (see Listing 1), which contains five Button views embedded within the element.
Observe the following attributes found in the various Button views:
- layout_alignParentLeft: This aligns the view to the left of the parent view.
- layout_alignParentRight: This aligns the view to the right of the parent view.
- layout_alignParentTop: This aligns the view to the top of the parent view.
- layout_alignParentBottom: This aligns the view to the bottom of the parent view.
- layout_centerVertical: This centers the view vertically within its parent view.
- layout_centerHorizontal: This centers the view horizontally within its parent view.
Figure 4 shows the activity when viewed in the portrait mode.
![]() Figure 4. Portrait Mode: The image shows the activity in portrait mode. |
![]() Figure 5. Landscape Mode:. This is what it looks like when all views are aligned appropriately. |
When the screen orientation changes to landscape mode, the four buttons are aligned to the four edges of the screen and the center button is centered in the middle of the screen with its width fully stretched (see Figure 5).
Manual Repositioning
If you want to manually reposition each view in your activity when the screen orientation changes, use AbsoluteLayout. Consider the following content in main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/button1"
android:layout_width="121px"
android:layout_height="44px"
android:text="Button"
android:layout_x="13px"
android:layout_y="13px"
>
</Button>
</AbsoluteLayout>
Here, the activity has only one button view as shown in Figure 6.
![]() Figure 6. Solo: This shows the activity with one view. |
![]() Figure 7. Redone: The Button view is repositioned and resized when it is in landscape mode. |
When the screen orientation changes to landscape mode, you might want to reposition the button view. In this case, you need to write some code to determine the current screen orientation the activity is in and then manually set the layout parameter of the view to reposition.
To see how this is done, use the code in Listing 2 in Orientation.java.
The above program first checks the current screen size and determines the orientation. It then creates a new LayoutParams object (containing the new position to locate the view) to be used by the Button view.
Figure 7 shows the application in action—the Button view is repositioned and resized when the screen orientation changes to landscape mode.
Using the layout Folder
Apart from writing code, an easier way of customizing the UI based on screen orientation is to create a separate res/layout folder containing the XML files for the UI. To support landscape mode, you can create a new folder in the res folder and name it layout-land (representing landscape). Figure 8 shows that the new folder also contains the file main.xml.
![]() Figure 8. Downloading Data: The layout-land folder contains the UI to be displayed in landscape mode. |
![]() Figure 9. Emulate 3G: You can also apply the –land name extension to the drawable folder. |
Basically, the main.xml file contained within the layout folder defines the UI for the activity in portrait mode while the main.xml file in the layout-land folder defines the UI in landscape mode. The following shows the content of main.xml under the layout-land folder:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/button1"
android:layout_width="150px"
android:layout_height="60px"
android:text="Button"
android:layout_x="280px"
android:layout_y="180px"
/>
</AbsoluteLayout>
Basically, the main.xml file contained within the layout folder defines the UI for the activity in portrait mode while the main.xml file in the layout-land folder defines the UI in landscape mode. The following shows the content of main.xml under the layout-land folder:
<?xml version="1.0" encoding="utf-8"?&glt;
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
&glt;
<Button
android:id="@+id/button1"
android:layout_width="150px"
android:layout_height="60px"
android:text="Button"
android:layout_x="280px"
android:layout_y="180px"
/&glt;
</AbsoluteLayout&glt;
Using this method, when the orientation of the device changes, Android will automatically load the appropriate XML file.
Besides using the –land name extension on the res/layout folder, you can also use it on the res/drawable folder. For example, the res/drawable-land folder shown in Figure 9 contains images that are designed to be displayed in landscape mode while those in the res/drawable folder are designed to be displayed in portrait mode.
Persisting State Information During Configuration Change
One important concept you need to know when handling screen orientation changes is that when a screen changes orientation, the activity is destroyed and then recreated–the onCreate event is always called when the screen orientation changes. When this happens, the current state of the activity may be lost.
First and foremost, you should name all your views in your activity using the android:id attribute. This attribute is important for applications that change with screen orientation, because when Android destroys activities, it saves state only for named views. For example, suppose a user changes orientation in the midst of entering some text into an EditText view. When this happens, if you’ve named the EditText view using an android:id attribute, Android will persist any existing text inside the EditText view, and restore it automatically when the activity is recreated. In contrast, if the view does not have an android:id attribute, the system will not be able to persist the text, so when it recreates the activity, any already-entered text will be lost.
In addition, note that the onSaveInstanceState event is fired whenever an activity is about to be killed or put into the background. For example, when the orientation is changed, this event is fired so that the current state of the activity can be saved and restored later. This event is similar to the onPause event of the Activity class, but unlike the onPause event (which is called whenever the activity is being placed in the background or being killed), it is not fired when an activity is being unloaded from the stack (since there is no need to restore its state later on).
You can override the onSaveInstanceState event and save the information you need in this event. For example, the following code shows that you can save the string ID into the Bundle object during the onSaveInstanceState event:
@Override
public void onSaveInstanceState(Bundle outState)
{
//---save whatever you need to persist—
outState.putString("ID", "1234567890");
super.onSaveInstanceState(outState);
}
When an activity is recreated, the onCreate event is fired first, followed by the onRestoreInstanceState event. This onRestoreInstanceState event allows you to retrieve the state that you have saved previously in the onSaveInstanceState event:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
//---retrieve the information persisted earlier---
String ID = savedInstanceState.getString("ID");
}
While you can use the onSaveInstanceState event to save state information, the limitation is that you can only save your state information into a Bundle object. If you need to save more complex data structures, then this is not an adequate solution.
Another event handler that you can use is the onRetainNonConfigurationInstance event. This event is fired when an activity is about to be destroyed due to a configuration change. (Screen orientation changes are considered configuration changes, and by default, all configuration changes cause the current activity to be destroyed). You can save your current data structure by returning it in this event, like this:
@Override
public Object onRetainNonConfigurationInstance()
{
return("Hello");
}
Note that this event returns an Object type, which pretty much allows you to return any data type. To extract the saved data, you can extract it in the onCreate event, using the getLastNonConfigurationInstance() method, like this:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String str = (String) getLastNonConfigurationInstance();
}
Bypassing the Activity Destruction Process
Suppose you don’t want Android to go through the normal activity destroy-and-recreate process; instead, you want to handle recreating the views yourself. In this case, you can use the android:configChanges attributes on the <activity> element in AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.UIExample"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".UIActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter&glt;
</activity>
</application>
</manifest>
The above attribute indicates that you want to handle the flipping of the keyboard and the changes in the accelerometer (sensor) yourself. When a change in orientation occurs, the onConfigurationChanged event will be fired, which you can override to redraw your activity:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//---code to redraw your activity here---
//...
}
Changing the Screen Orientation Based on the Accelerometer
If you want to change the screen orientation automatically based on the positioning of the device, you can use the android:screenOrientation attribute in the AndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.leanr2develop.OrientationAware"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Orientation"
android:screenOrientation="sensor"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The above specifies that the screen orientation for the Orientation activity is based on the sensor (accelerometer) of the device. If you hold the device upright, the screen will be displayed in portrait mode; if you hold it sideways, it will change to landscape mode.
Changing the Screen Orientation Programmatically
There are times where you need to ensure that your application is displayed only in a certain orientation. For example, suppose you are writing a game that should only be viewed in landscape mode. In this case, you can programmatically force a change in orientation using the setRequestOrientation() method of the Activity class:
package net.learn2develop.OrientationAware;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
public class Orientation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---change to landscape mode---
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Besides using the setRequestOrientation() method, you can also use the android:screenOrientation attribute on the <activity> element in AndroidManifest.xml as follows to fix the activity to a certain orientation:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.UIExample"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".UIActivity"
android:screenOrientation="landscape"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The above example fixes the activity to a certain orientation (landscape in this case) and prevents the activity from being destroyed; that is, the activity will not be destroyed and the onCreate event will not be fired again when the orientation changes.
Now, you’ve seen the various ways to handle screen orientation changes in Android and how an activity gets recreated when an activity changes orientation. You should be able to use these methods to persist your state during transition.
Source: http://www.devx.com/wireless/Article/40792/1954
[Sqlite] Copy records from one DB to another
August 20, 2009sqlite3 db1.db > attach db2.db as db2; > insert into table1 select * from db2.table1; > .quit
[Ubuntu] Compiz Grid Plugin
August 14, 20091.
sudo apt-get install compiz-bcop compiz-dev compizconfig-settings-manager build-essential libtool libglu1-mesa-dev libxss-dev libcairo2-dev git-core
2.
mkdir ~/compiz
3.
cd ~/compiz
4.
git clone git://anongit.compiz-fusion.org/compiz/plugins/grid
5.
cd grid
6.
make
7.
make install
7.1 You may need to enable “Normal” or “Extra” under desktop effects in System->Preferences->Appearance first. (You may also need to chown -R user:user ~/compiz in order to get things to compile.)
8.
ccsm
9. Check “grid” under “Window Management”. Note you can actually click on the icon and “grid” to bring up keyboard shortcuts for placing the windows where you want them. Do not expect to have new icons come up at the top of the windows (next to minimize/maximize/close), this won’t happen. However, <ctrl><alt><kp 6> is quicker anyway.
10. Play around with what the different control-alt-numeric keypad combos do to get an idea of how to use this. It’s pretty intuitive.
In Ubuntu 9.04 you have to delete the folder ~/.compiz/ first and log in again
Source : http://ubuntuforums.org/showthread.php?t=1148397
[PHP] Debugging a CodeIgniter application with FirePHP
August 13, 2009FirePHP is an addon for FireBug extension for Firefox browser.
FirePHP extends FireBug functionalities to show log or error messages coming from your PHP application. These messages won’t be printed on the application interface but instead on the FireBug console.
What do you need to integrate this debugging feature in your application :
The next step is to open Firefox and visit the url where your application is located and activate the Net panel in FireBug, clicking the bottom right Firebug icon in Firefox window.

The client (the browser), is now ready to accept the debugging messages, we have now to integrate the FirePHP library with the CodeIgniter application on server side :
- dowload the FirePHP Core Library from here
- extract the
FirePHP.class.phpfrom the archive, rename it tofirephp.phpand copy in thesystem/application/librariesdirectory of your CodeIgniter application
Now you can use FirePHP to debug your code :
- load the library :
$this->load->library('firephp') - send debug messages to the FireBug console :
$this->firephp->log($myvariable)or$this->firephp->error('Error at this line')
Detailed instructions to use FirePHP are available on FirePHP project website. The most useful method for me are :
$this->firephp->log($myvariable): send a dump of the variable$myvariableon the FireBug console$this->firephp->warn($myvariable): send a dump of the variable$myvariableon the FireBug console classified as a warning$this->firephp->error($myvariable): send a dump of the variable$myvariableon the FireBug console classified as an error
Here there is a little example on how you can use FirePHP with CodeIgniter :
<?php $this->load->library('firephp'); $myvariable = array ( 'language' => 'PHP', 'database' => 'MySQL', 'blogging platform' => 'WordPress', 'post' => 'CodeIgniter and FirePHP', ); $this->firephp->log($myvariable); ?>
And this is the output on the Firefox’s FirePHP console :

The debug messages can be disabled with the method : $this->firepgp->setEnabled(FALSE).
It is also possible to make the messages appear only for specific ip addresses :
<?php if($this->input->ip_address() =='1.2.3.4') { $this->firephp->setEnabled(TRUE); } else { $this->firephp->setEnabled(FALSE); } ?>
[Ubuntu] Share One Keyboard and Mouse Between Multiple Computers
August 12, 2009Have multiple keyboards and mice on your desk? With Synergy, you can seamlessly share one keyboard, mouse, and clipboard between multiple systems. Have a laptop to the right of your main display? Just move the cursor off the right edge of the screen and it will appear on the laptop.
QuickSynergy is a graphical application for setting up Synergy servers and clients. I used it to to share my mouse and keyboard between my desktop and Eee PC laptop. Synergy works flawlessly, even when the laptop is on wifi I can’t tell that I’m using a remote mouse and keyboard.

Install QuickSynergy from the package quicksynergy (click the link to install), or by running the command below in your terminal:
sudo apt-get install quicksynergy
Open QuickSynergy from Applications->Accessories->QuickSynergy.
On the Synergy server:
In the Share tab, type the hostnames of the clients (you must specify a hostname and not a IP address) into the appropriate boxes to position them around your main display. Click Start and the server will start (QuickSynergy will close). Open QuickSynergy again if you want to stop the server.
On the clients:
In the Use tab, type the IP address of the server. Select the Settings tab and select the Keep synergy running option so you can close the QuickSynergy window without disconnecting. Click Start to connect.
Tips:
- Find your IP address in System->Administration->Network Tools in the Device tab. (You’ll need to select your network device from the drop down.)
- Find your hostname in System->Administration->Network in the General tab.
Source : http://tombuntu.com/index.php/2008/09/15/share-one-keyboard-and-mouse-between-multiple-systems/
[Ubuntu] Automount Hard Drive Partitions Everytime You Login to Ubuntu Linux
August 11, 2009Hello people,
This is Bharath Ram. I wanted to share with you an information I just found. This indeed solved my problem so I thought it would solve yours too.
In ubuntu (and may be in other distros) every time you login you never get your drives auto mounted (don’t you?). This is a real pest especially when you would have set your wallpaper from an external drive or some other reason you want to use external drives for.
I have just found out a solution for this problem. That is no big deal but a small software called “pysdm”. It was there for me in the ubuntu repositories but i seriously don’t know about other distros. Install this software and if necessary open it after install (alt+f2 -> type “pysdm” -> enter) and in the options there say to automount your partition(s) at startup. Now the drives of ur hard disk will be mounted whenever you login. You won’t have to manually/using computer mount them.
Hope this helps
Source: http://sathyasays.com/2008/10/08/how-to-automount-hard-drive-partitions-everytime-you-login-in-linux/
[Ubuntu] How to Mount iso, Bin And Cue Files Directly From Nautilus
August 10, 2009We have covered the easy way to mount iso images in Windows. This time round, we are going to cover the easy way to mount an iso, bin or cue file in Ubuntu without having to hit the terminal every time.
With the use of fuesiso and nautilus-actions, we can easily create an option in Nautilus to mount the CD images right from the context menu (mouse right click).
Here it goes:
First, install fuseiso and nautilus actions
sudo apt-get install fuseiso nautilus-actions sudo usermod -a -G fuse username
Change the username to your login name
Logout and back in.
Download the userisomount.sh script to your home folder.
Copy it to /usr/local/bin/ folder and make it executable.
sudo mv ~/userisomount.sh /usr/local/bin/ sudo chown root:fuse /usr/local/bin/userisomount.sh sudo chmod 754 /usr/local/bin/userisomount.sh
Download the Nautilus Actions Schemas for MOUNTING and UNMOUNTING disk images.
2. Locate the line with the code <default>1.1</default>
3. Replace it with <default>2.0</default>
4. Save and close the files.
Open up Nautilus Actions Configuration (System->Preferences->Nautilus Actions Configuration). Import the two files into Nautilus Actions.
Save and close Nautilus Actions.
In your terminal, restart Nautilus
killall nautilus
You should be able to find a Mount Disk Image on the context menu of your Nautilus now.

To mount a iso, bin or cue file, simply right click and choose Mount Disk Image from the menu.
Reference: Ubuntu community documentation
[Ubuntu] How To Share Files In VirtualBox With Vista Guest And Ubuntu Host
August 10, 2009For some of you who have setup Windows Vista as the guest VM in your Ubuntu Virtualbox, you might have some difficulties in getting the Vista guest to access the shared folder in your Ubuntu host. If you are having this problem, here is the way to mount the shared folder on your Vista guest.
First of all, make sure you have already installed both VirtualBox and Windows Vista guest. If you have not, here is the complete installation guide.
Secondly, make sure you have updated your VirtualBox to the latest version (2.0.4). There are some issues in the earlier version which will cause the mounting to fail.
In your Ubuntu machine, open up Virtualbox (Applications -> System Tools -> virtualBox).
Highlight the Vista VM entry (make sure that it is in Powered off state) and click on the Settings icon at the top.

On the left, click on the Network. On the right, make sure that the Enable Network Adaptor and Cable Connected are checked.

On the left, click on the Shared Folders. Then, click on the
icon on the far right.
Select the folder that you want to use as a share point. Give it a name.


Click OK to close the Settings window.
Boot up your Vista VM.
Install the guest addition. (Skip this step if you have already done so)

Restart the Vista VM.
Open your Windows Explorer, click on the Map Network Drive

In the Folder input text, enter
\\vboxsvr\sharename
where sharename is the name of the share folder that you add just now. Make sure that the Reconnect at logon is checked. Click Finish

You should now see the shared folder mounted as a network drive.

That’s it.
Source : http://maketecheasier.com/share-files-in-virtualbox-between-vista-guest-ubuntu-host/2008/11/12
[Android] Get Phone State When Someone is calling using BroadcastReceiver Example
August 6, 2009In this article we shall try to listen to the phone state when contacts are calling us.
First of all we need to set our Manifest file to listen to the Phone State, to do that we need to edit our it.
<application>
.....
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
Here you can see that we created a receiver xml node inside our application and have the java class ServiceReceiver to listen to it. What it would listen to is the PHONE_STATE and thus we need the permission of READ_PHONE_STATE. Then our ServiceReceiver Class would look like this.
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener=new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
For the full class including the imports, please download the files below. In here we have another class called MyPhoneStateListener, which would be shown at the bottom. What this class would do is execute the phoneListener when the telephony.listen has received a LISTEN_CALL_STATE.
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
break;
}
}
}
What we have is a function called onCallStateChanged which would be fired when the LISTEN_CALL_STATE dispatches it. The states are either, ringing(CALL_STATE_RINGING), answers (CALL_STATE_OFFHOOK), or hang up/end call (CALL_STATE_IDLE). To see the logs in eclipse. Go to Window -> Show View -> Other -> Android -> LogCat
Hope it helps.
Source : http://almondmendoza.com/2009/01/22/get-phone-state-when-someone-is-calling-using-broadcastreceiver-example/








