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;
    }
}

No comments: