Month: May 2013

  • How to create custom background for InfoWindow on Google Maps V2 for Android

    How to create custom background for InfoWindow on Google Maps V2 for Android

    Here is short tutorial how to create custom background for InfoWindow (balloon) on Google Maps V2 for Android.

    As you can see the InfoWindowAdapter have two methods : getInfoWindow() and getInfoContents().

    To use default background for InfoWindows you need use getInfoContents().

    And if you want to use custom background for InfoWindows you need use getInfoWindow().

    Lets see screenshot below:

    The source code of InfoWindowAdapter is here:

    package com.hrupin.sample.googlemapsv2customballoons;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.TextView;
    
    import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
    import com.google.android.gms.maps.model.Marker;
    
    public class BalloonAdapter implements InfoWindowAdapter {
    	LayoutInflater inflater = null;
    	private TextView textViewTitle;
    
    	public BalloonAdapter(LayoutInflater inflater) {
    		this.inflater = inflater;
    	}
    
    	@Override
    	public View getInfoWindow(Marker marker) {
    		View v = inflater.inflate(R.layout.balloon, null);
    		if (marker != null) {
    			textViewTitle = (TextView) v.findViewById(R.id.textViewTitle);
    			textViewTitle.setText(marker.getTitle());
    		}
    		return (v);
    	}
    
    	@Override
    	public View getInfoContents(Marker marker) {
    		return (null);
    	}
    }
    Download it from github
  • How to fix java.lang.VerifyError in Android for ADT-22

    How to fix java.lang.VerifyError in Android for ADT-22

    I had one problem when migrated to ADT-22.

    I’ve renamed “lib” dir to “libs” and project compiled.
    And when I run project on Android device the “java.lang.VerifyError” error crashes my app.
    In the LogCat I saw that some of classes were rejected by Verifier: “Verifier rejected class …..”

    And I found solution.

    There problem in two checkboxes in project properties. Lets look the screenshots.

    This screenshot show properties before fix:

    And this screenshot after fixing:

    To fix it you need put ticks near:
    – Android Private Libraries
    – Android X.X.X

    Hope it help you

  • How to setAlpha for View in Android 2.3 (API-10) and lower

    How to setAlpha for View in Android 2.3 (API-10) and lower

    As you can see there existing setAlpha() method in API-11. Using this method you can change Alpha for View.

    What we gonna do in API-10 and lower to change View Alpha?

    I’ve got one solution. The solution is AlphaAnimation

    Please take a look my code:

    package com.hrupin.samples.alpha;
    
    import com.hrupin.samples.halfopacitywidgets.R;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.AlphaAnimation;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
    	private Button buttonWithAlpha;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		buttonWithAlpha = (Button)findViewById(R.id.buttonWithOpacity);
    
    		setAlphaForView(buttonWithAlpha, 0.5f);
    	}
    
    	private void setAlphaForView(View v, float alpha) {
    		AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
    		animation.setDuration(0); 
    		animation.setFillAfter(true); 
    		v.startAnimation(animation);
    	}
    }

    In this way you can simply change alpha for your view in Android API-10 and lower.

    Download it from github
  • How to change Android SDK path in Android Studio for Mac

    How to change Android SDK path in Android Studio for Mac

    Today I want to change default Android SDK path in Android Studio. To change default path to Android SDK you need do the next steps. I describe it via screenshots.

    1. Open Android Studio.

    2. Configure Android Studio.

    3. Click Project Defaults.

    4. Cofigure Android SDK path. Click to enlarge. Go to SDKs -> Android SDK

    5. Cofigure Android SDK path. Go to Project -> Project SDK

    Enjoy

  • How to fix “Can not perform this action after onSaveInstanceState” on Android Fragments

    How to fix “Can not perform this action after onSaveInstanceState” on Android Fragments

    Recent I’ve this bug on my project.
    This bug comes when app awake from sleep.
    I’ve spent a lot of time to fix it. And finally I got the solution.

    My project is Activity with Fragments.
    The error text is:
    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3113)
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3062)
    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3035)
    at android.app.ActivityThread.access$800(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1268)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4945)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
    at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:501)
    at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:180)
    at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155)
    at

    To fix it you need save instance of your Fragments. To do this you need just override onSaveInstanceState method for your Fragment.

    @Override
    public void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	outState.putString(BundleKeys.MESSAGE, message);
    }

    Also you need restore fragment state in onCreateView of onActivityCreated methods

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	if (savedInstanceState != null) {
    			if (!savedInstanceState.isEmpty()) {
    			Loger.i(TAG, "!savedInstanceState.isEmpty()");
    			if (message == null) {
    				message = new Gson().fromJson(savedInstanceState.getString(BundleKeys.MESSAGE), Message.class);
    			}
    		}
    	}
    }

    Hope it help you.

  • Eclipse ADT Rev 22 doesn’t generate R.java

    Eclipse ADT Rev 22 doesn’t generate R.java

    I had this problem when updated to ADT-22 on Eclipse.

    To fix it you need download NEW additional component from google. This is “Android SDK Build-tools”.

    Open Your SDK Manager and download it.

    Restart Eclipse