Wednesday, June 5, 2013

How to increase the heap size available to Eclipse ?

Open your eclipse path D:\backup\Android Setup\eclipse\

Open eclipse.ini

Change to

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20120913-144807
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Xms512m
-Xmx1024m
-XX:+UseParallelGC
-XX:PermSize=256M
-XX:MaxPermSize=512M


With referance link

Tuesday, April 30, 2013

Some method that we using every day in Android like ResizedBitmap,countDistance,isNetworkAvailable,getLogoImage,convertStreamToString

public static Bitmap getResizedBitmap(Bitmap bm,  int newWidth,int newHeight)
    {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }


    public static Double countDistance(double lat1,double lng1, double lat2, double lng2)
    {
        Location locationUser = new Location("point A");
        Location locationPlace = new Location("point B");
        locationUser.setLatitude(lat1);
        locationUser.setLongitude(lng1);
        locationPlace.setLatitude(lat2);
        locationPlace.setLongitude(lng2);
        double distance = locationUser.distanceTo(locationPlace);
        return distance;
    }
    public static final boolean isNetworkAvailable(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable()) {
            if (wifi.getState() == NetworkInfo.State.CONNECTED)
            {
                //    Toast.makeText(context, "Wifi connection.", Toast.LENGTH_LONG).show();
                return true;
            }
            else
            {
                //     Toast.makeText(context, "No Connect using wifi connection.", Toast.LENGTH_LONG).show();
                return false;
            }
        } else if (mobile.isAvailable()) {
            if (mobile.getState() == NetworkInfo.State.CONNECTED)
            {
                //       Toast.makeText(context, "Connected using GPRS connection.", Toast.LENGTH_LONG).show();
                return true;
            }
            else
            {
                //      Toast.makeText(context, "No Connect using GPRS connection.", Toast.LENGTH_LONG).show();
                return false;
            }

        } else {
            //      Toast.makeText(context, "No network connection.", Toast.LENGTH_LONG).show();
            return false;
        }      

    }



    public static byte[] getLogoImage(String url){
        try {
            URL imageUrl = new URL(url);
            URLConnection ucon = imageUrl.openConnection();

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(500);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (Exception e) {
            Log.d("ImageManager", "Error: " + e.toString());
        }
        return null;
    }



    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

Tuesday, April 23, 2013

Show progress dialog on webview


public class WebViewDemo extends Activity{
WebView webView ;
ProgressDialog progressDialog;
Activity activity ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
webView = (WebView)findViewById(R.id.webview);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setSupportZoom(true);

progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);

webView.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
       progressDialog.show();
       progressDialog.setProgress(0);
       activity.setProgress(progress * 1000);
       progressDialog.incrementProgressBy(progress);
       if(progress == 100 && progressDialog.isShowing())
           progressDialog.dismiss();
   }
});
webView.loadUrl("http://www.google.co.in/");
}

}

Add permission in menifest

    <uses-permission android:name="android.permission.INTERNET" />



Saturday, March 23, 2013

Store and Retrieve a class object in SharedPreference in Android


We can do this using gson.jar
Download this jar here
SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);
For save
        Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(MyObject);
        prefsEditor.putString("MyObject", json);
        prefsEditor.commit();
For get
    Gson gson = new Gson();
    String json = mPrefs.getString("MyObject", "");
    MyObject obj = gson.fromJson(json, MyObject.class);
 Download sourcecode-here 

Saturday, March 2, 2013

Make a link in the Android browser start up my app?

After whole day stuff i found that its simple to call android app using link.

1.Open your project AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pc.demo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="9" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".URIDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
// Add your url scheme  
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:host="www.parag-chauhan.com"
                    android:path="/test"
                    android:scheme="http" />
            </intent-filter>
        </activity>
    </application>
</manifest>

And its done.

2. For test create another project and run below code.

Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse("http://www.parag-chauhan.com/test"));
startActivity(i);

now your application will open .


3. For Pass Parameter in link.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pc.demo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="9" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".URIDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
// Add your url scheme  
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
               <data
                    android:host="www.parag-chauhan.com"
                    android:scheme="http" />               

           </intent-filter>
        </activity>
    </application>
</manifest>

 In URIDemoActivity

    Uri data=getIntent().getData();
    
    if (data !=null)  {
        String Name = data.getQueryParameter("Name");
        String Country = data.getQueryParameter("Country");
       
    }

 

4. For test pass parameter using link create another project and run below code.

Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse("http://www.parag-chauhan.com/?Name=Parag&Country=India"));
        startActivity(i);

now your application will open .