Tuesday, December 3, 2013

Formatting String in Android

Formatting strings

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Wednesday, November 6, 2013

how to display MapFragment inside the fragment(NestedFragment):

At this point I believe you have
1. added necessary permission on manifest
2. added google play service as a lib project
3. api key in manifest file.

where.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.03"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapwhere" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>




public class WhereFragment extends SupportMapFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.where, null, false);
initilizeMap();
return root;
}

private void initilizeMap()
{
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapwhere);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
   }
if (mSupportMapFragment != null)
{
googleMap = mSupportMapFragment.getMap();
if (googleMap != null)
googleMap.setOnMapClickListener(new OnMapClickListener()
{
@Override
public void onMapClick(LatLng point)
{
//TODO: your onclick stuffs
}
});
}
}
}


Documentation:
Nested Fragments
You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().

The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
source:http://developer.android.com/about/versions/android-4.2.html#NestedFragments


This will also fix for:

11-06 11:36:01.509: E/AndroidRuntime(6309): FATAL EXCEPTION: main
11-06 11:36:01.509: E/AndroidRuntime(6309): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.abc.android.ui.WhereFragment.onCreateView(WhereFragment.java:60)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2414)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2133)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1286)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1497)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Handler.handleCallback(Handler.java:725)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Looper.loop(Looper.java:137)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.app.ActivityThread.main(ActivityThread.java:5328)
11-06 11:36:01.509: E/AndroidRuntime(6309): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 11:36:01.509: E/AndroidRuntime(6309): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-06 11:36:01.509: E/AndroidRuntime(6309): at dalvik.system.NativeStart.main(Native Method)
11-06 11:36:01.509: E/AndroidRuntime(6309): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

Thursday, October 31, 2013

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Name: java.lang.IllegalStateException
Reason: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Stack Trace:
0 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
1 at android.database.CursorWindow.nativeGetString(Native Method)
2 at android.database.CursorWindow.getString(CursorWindow.java:438)
3 at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
4 at android.database.CursorWrapper.getString(CursorWrapper.java:114)
5 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:****)
6 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:1)
7 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
8 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
9 at android.support.v4.content.Loader.deliverResult(Loader.java:104)
10 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
11 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
12 at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
13 at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
14 at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
15 at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
16 at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
17 at android.os.Handler.dispatchMessage(Handler.java:99)
18 at android.os.Looper.loop(Looper.java:137)
19 at android.app.ActivityThread.main(ActivityThread.java:5328)
20 at java.lang.reflect.Method.invokeNative(Native Method)
21 at java.lang.reflect.Method.invoke(Method.java:511)
22 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
23 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
24 at dalvik.system.NativeStart.main(Native Method)



Fix

0. Try to position cursor by moveToFirst before reading data from it.

1. Close the cursor after if ( c.moveToFirst()) {}

2. check for null.e,g; if (c != null && c.moveToFirst()) {}

3. check for count.e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}

Tuesday, October 15, 2013

Google V2 Maps Zoom Control

The code will look like:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {



public void onCameraChange(CameraPosition arg0) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
googleMap.setOnCameraChangeListener(MySampleMapActivity.this);
}
});

Explanation:


public final void setOnCameraChangeListener (GoogleMap.OnCameraChangeListener listener)

Sets a callback that's invoked when the camera changes.
Parameters
listener The callback that's invoked when the camera changes. To unset the callback, use null.
Also,
public static interface
GoogleMap.OnCameraChangeListener
com.google.android.gms.maps.GoogleMap.OnCameraChangeListener
Class Overview
Defines signatures for methods that are called when the camera changes position.

Summary
Public Methods
abstract void onCameraChange(CameraPosition position)
Called after the camera position has changed.
public abstract void onCameraChange (CameraPosition position)

Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.

This is called on the main thread.

Parameters
position The CameraPosition at the end of the last camera change.


And Finally,

public final void animateCamera (CameraUpdate update, int durationMs,GoogleMap.CancelableCallback callback)

Moves the map according to the update with an animation over a specified duration, and calls an optional callback on completion. See CameraUpdateFactory for a set of updates.

If getCameraPosition() is called during the animation, it will return the current location of the camera in flight.

Parameters
durationMs The duration of the animation in milliseconds. This must be strictly positive, otherwise an IllegalArgumentException will be thrown.
callback An optional callback to be notified from the main thread when the animation stops. If the animation stops due to its natural completion, the callback will be notified withonFinish(). If the animation stops due to interruption by a later camera movement or a user gesture, onCancel() will be called. The callback should not attempt to move or animate the camera in its cancellation method.
public final void animateCamera (CameraUpdate update,GoogleMap.CancelableCallback callback)

Animates the movement of the camera from the current position to the position defined in the update and calls an optional callback on completion. See CameraUpdateFactory for a set of updates.

During the animation, a call to getCameraPosition() returns an intermediate location of the camera.

Parameters
update The change that should be applied to the camera.
callback The callback to invoke from the main thread when the animation stops. If the animation completes normally, onFinish() is called; otherwise, onCancel() is called. Do not update or animate the camera from within onCancel().
public final void animateCamera (CameraUpdate update)

Animates the movement of the camera from the current position to the position defined in the update. During the animation, a call to getCameraPosition() returns an intermediate location of the camera.

See CameraUpdateFactory for a set of updates.

Parameters
update The change that should be applied to the camera.


Wednesday, October 2, 2013

ActionBarSherlock


ActionBarSherlock

ActionBarSherlock is an standalone library designed to facilitate the use of the action bar design pattern across all versions of Android through a single API.
The library will automatically use the native ActionBar implementation on Android 4.0 or later. For previous versions which do not include ActionBar, a custom action bar implementation based on the sources of Ice Cream Sandwich will automatically be wrapped around the layout. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up.
See http://actionbarsherlock.com for more information.