Sunday, March 31, 2013

How to enable MAT (Eclipse Memory Analyzer) in your Eclipse?

When talk about out of memory crashing issue in my Java app, I really felt helpless in finding the root cause. Even though MAT can be an alternative in helping me getting closer to the issue, but in the end of the day, all still depending on the code review and the best practice during the development.

Below are steps to setup the MAT.

1. To install MAT, open Eclipse, click on Help -> Install New Software ->
2. Enter "Add" button and enter "Name" and "Location" into the dialog box
Name: MAT
Location: http://download.eclipse.org/mat/1.2/update-site/
3. Check the "Memory Analyzer for Eclipse IDE" and "Standalone Memory Analyzer" and click on "Next" button. Follow the step as shown in the wizard, the MAT will be installed into the Eclipse once the Eclipse is restarted.


4. To allow the application to generate hprof file, right click on the project, Run As -> Run Configurations, select "Arguments" tab and enter "-XX:+HeapDumpOnOutOfMemoryError" into "VM arguments" text box.

5. When the Java/Android app crashes, hrof will be generated.

6. Double click on the hprof file, MAT will be launched and select "Leak Suspect Report" option and click on "Finish" button.

7. Leaks Suspect report will be generated.

If you encounter "Parsing heap dump" while opening the hprof file, open the eclipse.ini file which is located at Eclipse home folder. Increase the -Xms and -Xmx size (until the message disappear), refer to the highlighted paragraph as shown below:-


-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20120522-1813
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
1024m
--launcher.defaultAction
openFile
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
1024M
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Dhelp.lucene.tokenizer=standard
-Xms8192m
-Xmx10240m


To produce Android hprof file for MAT analysis, launch the DDMS, select device follow by running application. Click on dump HPROF file, the MAT analyzer will be launched.





Sunday, March 17, 2013

How to use Android fragment (1)?

To cut short and make this tutorial simple and practical, I have made use of the component from https://github.com/chrisbanes/Android-BitmapCache/ to demo-ing fragment in my usage.

Note: This component does help in solving the jerkyness of the app while loading the image through network.

Below are the steps to include Android-BitmapCache component

1. Edit previous project with the below configuration
Application Name: TechTips
Project Name: TechTips
Package Name: com.example.techtips

2. Import the library from Android-BitmapCache component. Please refer to the screen as shown below:


3. Add 3 classes into the TechTips project as shown in the screen below
- NetworkedCacheableImageView.java
- SampleApplication.java
- SDK11.java

4. activity_tech_tips.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TechTipsActivity" >

   <fragment
        android:id="@+id/techtipsfragment"
        android:name="com.example.techtips.TechTipsFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</RelativeLayout>
5. fragment_tech_tips.xml.
Refer to the fragment_tech_tips.xml, add in the ImageView with network access

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <uk.co.senab.bitmapcache.samples.NetworkedCacheableImageView
        android:id="@+id/nciv_pug"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/app_name"
    />
</LinearLayout>


6. TechTipsActivity.java
package com.example.techtips;

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

public class TechTipsActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_tech_tips);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_tech_tips, menu);
  return true;
 }
}


7. TechTipsFragment.java
package com.example.techtips;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import uk.co.senab.bitmapcache.samples.*;

public class TechTipsFragment extends Fragment{
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tech_tips, null); 
        NetworkedCacheableImageView currentImageView = (NetworkedCacheableImageView)v.findViewById(R.id.nciv_pug);
        final boolean fromCache = currentImageView.loadImage("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSjsDTbniCrwOUKFiOF4i06fpMplrqjFMPi3s7bhm5K-jWGqK-p", false);

        if (fromCache) {
         Toast.makeText(getActivity().getApplicationContext(), "From Memory Cache", Toast.LENGTH_LONG).show();
        } else {
         Toast.makeText(getActivity().getApplicationContext(), "From Disk/Network", Toast.LENGTH_LONG).show();
        }
        return v;
    }
}

Saturday, March 16, 2013

How to use Android Fragment?

When talk about Android Fragment, I don't see there is a need to use it initially. My perception is, if you have activity, why you need to use it? After more involve in the Android development, then I realize that it is quite useful for modular activity design.

What is Fragment?
- Fragment represent a sub-activity that added into a main activity
- It must be added into an activity and only working if it is added into an activity
- It has its own lifecycle, but much depends on the parent activity lifecycle
- It has its own layout which I find it is useful for modular activity design
- You can dynamically add/remove into an activity during runtime. (This is a great feature)


The table below explain the lifecycle of the fragment.

Fragment Callbacks
onAttach()
onCreate
onCreateView
onActivityCreated
onStart()
onResume
onPause
onStop
onDestroyView
onDestroy
onDetech


Below is the sample source code explain the simple concept of Fragment

1. Create a project with the below configuration
Application Name: TechTips
Project Name: TechTips
Package Name: com.example.techtips

2. TechTipsActivity.java

package com.example.techtips;

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

public class TechTipsActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_tech_tips);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_tech_tips, menu);
  return true;
 }
}

3. TechTipsFragment.java
package com.example.techtips;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TechTipsFragment extends Fragment{
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tech_tips, null);
        return v;
    }
}

4. activity_tech_tips.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TechTipsActivity" >


   <fragment
        android:id="@+id/techtipsfragment"
        android:name="com.example.techtips.TechTipsFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</RelativeLayout>
5. fragment_tech_tips.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/app_name"
    />
</LinearLayout>
Run on your device/emulator, you will see the "Tech Tips" wording is displayed. Explore the code, you will find out that, the "Tech Tips" wording is being handle by fragment layout xml file. First snippet code with basic concept to move on to the next and more complicated sample. More to come for Android Fragment...

Friday, March 8, 2013

How to update your Figure Number in mass in your Microsoft document?

This is basic but may cost you time if you update your figure number 1 after another.

1. Select the whole Word document by pressing "Ctrl + A".
2. Press F9, a dialog box will be popup.
3. Select "Update entire table" option and click on "Ok" button
4. All the figure number and table of content will be updated.

Monday, March 4, 2013

How to access camera in Nexus 7?

When first time heard about Nexus 7, really impressed by all the news and comments on how good it is, how cheap it is and how worth it is, but when I come to use the the camera in the device, I'm a bit lost. Really doubt the news is too pro to this device?

To enable me and my application to use the camera, I have to download "Camera Launcher For Nexus 7" in Playstore. Only install this app, then I can use the front face camera to take picture.


Sunday, March 3, 2013

How to use the LinearLayout in controlling the Android interface?

Just a brief description of the LinearLayout usage after going through couple of tutorial from few blog or website.

1. Create a project with the below configuration

Application Name: LinearLayout
Project Name: LinearLayout
Package Name: com.example.linearlayout

How to use layout_weight parameter?

2. Replace the layout as shown below to activity_main.xml with the layout xml as shown below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 <Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="Button 1" />
 <Button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="2"
  android:text="Button 2" />
 <Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="Button 3" />
</LinearLayout>
Once the project is compiled and run, it will be displayed in the emulator as shown in the screen below. The size of each of the button will be controlled by the layout_weight ratio.
How to use gravity parameter?

3. Replace the layout as shown below to activity_main.xml with the layout xml as shown below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:gravity="left"
     android:background="#99F55500">
     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="City"/>
 </LinearLayout>
 
 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="#99055500"
     android:layout_weight="1"
     android:gravity="right">
     <TextView
         android:id="@+id/textView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="New York"/>
 </LinearLayout>
</LinearLayout>

Once the project is compiled and run, it will be displayed in the emulator as shown in the screen below. You will notice that the textview is set to right alignment.