Archive for October, 2011

How to implement lazy loading for markers on Google Maps in Android.

If your objects which you want to show on the Google Maps in Android have different markers. You need to use lazy loading for markers.

How to do it? It’s very easy. Just use AsyncTask . 

Below you can download source code of Android project.

When you start application you can see Google Maps with red default markers. When custom marker loaded the custom marker will replace default marker and update map overlay.

To see map you need specify your own Google Maps API Key in MapView  (see main.xml file)

< ?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<com .google.android.maps.MapView 
		android:id="@+id/mapView" 
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:clickable="true" 
		android:apiKey="YOUR_API_KEY" />
</linearlayout>

Other info you can see in the code. It is simple to understand. Please, add the questions in comments.

Here code of the AsyncTask class

Read the rest of this entry »

Tags: , , ,

How to set the different screen orientations for TabActivity children activities

Hi,
Here I want to show you how to make different screen orientation for TabActivity children activities.

You may say “Just specify this in Android Manifest file. That’s All”. But, it didn’t works. All child activities have same orientation with parent (TabActivity).

Below you can download source code of Android project

I will show you how I fix this for me.

  1. Specify the portrait or some else orientation for TabActivity in Manifest file. This will be default orientation for all child activities.
  2. Save reference to TabActivity as static field in TabActivity class
  3. Download the source code below. Pay attantion in onPause and onResume methods in SecondTabActivity class.
The code is so simple :)

TabActivitySampleActivity.java

package com.hrupin;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabActivitySampleActivity extends TabActivity {
	
	private static TabActivitySampleActivity INSTANCE;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        INSTANCE = this;
        TabHost host = getTabHost();
        
        TabSpec tab1 = host.newTabSpec("tab1");
        tab1.setIndicator("TAB 1");
        tab1.setContent(new Intent(this, FirstTabActivity.class));
        
        TabSpec tab2 = host.newTabSpec("tab2");
        tab2.setIndicator("TAB 2");
        tab2.setContent(new Intent(this, SecondTabActivity.class));
        
        host.addTab(tab1);
        host.addTab(tab2);
    }
    
    public static TabActivitySampleActivity getInstance(){
    	return INSTANCE;
    }
}

FirstTabActivity.java

package com.hrupin;

import android.app.Activity;
import android.os.Bundle;

public class FirstTabActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);
	}
}

SecondTabActivity.java

package com.hrupin;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class SecondTabActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		TabActivitySampleActivity.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		TabActivitySampleActivity.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
}

Download the source code Here  you can download the source code of Android project

Tags: , , , ,

Hire me!