Friday, September 21, 2012

Opengles Animation Example (Part I)

,
This example shows how to use opengles to create animation in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it OpenglesAnimationExample1.



2.) Create and write following into StaticTriangleRenderer.java:

package com.example.OpenglesAnimationExample1; import static android.opengl.GLES10.GL_CCW;
import static android.opengl.GLES10.GL_CLAMP_TO_EDGE;
import static android.opengl.GLES10.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES10.GL_DEPTH_BUFFER_BIT;
import static android.opengl.GLES10.GL_DEPTH_TEST;
import static android.opengl.GLES10.GL_DITHER;
import static android.opengl.GLES10.GL_FASTEST;
import static android.opengl.GLES10.GL_FLOAT;
import static android.opengl.GLES10.GL_LINEAR;
import static android.opengl.GLES10.GL_MODELVIEW;
import static android.opengl.GLES10.GL_MODULATE;
import static android.opengl.GLES10.GL_NEAREST;
import static android.opengl.GLES10.GL_PERSPECTIVE_CORRECTION_HINT;
import static android.opengl.GLES10.GL_PROJECTION;
import static android.opengl.GLES10.GL_REPEAT;
import static android.opengl.GLES10.GL_REPLACE;
import static android.opengl.GLES10.GL_SMOOTH;
import static android.opengl.GLES10.GL_TEXTURE0;
import static android.opengl.GLES10.GL_TEXTURE_2D;
import static android.opengl.GLES10.GL_TEXTURE_COORD_ARRAY;
import static android.opengl.GLES10.GL_TEXTURE_ENV;
import static android.opengl.GLES10.GL_TEXTURE_ENV_MODE;
import static android.opengl.GLES10.GL_TEXTURE_MAG_FILTER;
import static android.opengl.GLES10.GL_TEXTURE_MIN_FILTER;
import static android.opengl.GLES10.GL_TEXTURE_WRAP_S;
import static android.opengl.GLES10.GL_TEXTURE_WRAP_T;
import static android.opengl.GLES10.GL_TRIANGLE_STRIP;
import static android.opengl.GLES10.GL_UNSIGNED_SHORT;
import static android.opengl.GLES10.GL_VERTEX_ARRAY;
import static android.opengl.GLES10.glActiveTexture;
import static android.opengl.GLES10.glBindTexture;
import static android.opengl.GLES10.glClear;
import static android.opengl.GLES10.glClearColor;
import static android.opengl.GLES10.glDisable;
import static android.opengl.GLES10.glDrawElements;
import static android.opengl.GLES10.glEnable;
import static android.opengl.GLES10.glEnableClientState;
import static android.opengl.GLES10.glFrontFace;
import static android.opengl.GLES10.glFrustumf;
import static android.opengl.GLES10.glGenTextures;
import static android.opengl.GLES10.glHint;
import static android.opengl.GLES10.glLoadIdentity;
import static android.opengl.GLES10.glMatrixMode;
import static android.opengl.GLES10.glRotatef;
import static android.opengl.GLES10.glShadeModel;
import static android.opengl.GLES10.glTexCoordPointer;
import static android.opengl.GLES10.glTexEnvf;
import static android.opengl.GLES10.glTexEnvx;
import static android.opengl.GLES10.glTexParameterf;
import static android.opengl.GLES10.glTexParameterx;
import static android.opengl.GLES10.glVertexPointer;
import static android.opengl.GLES10.glViewport;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.SystemClock;
public class StaticTriangleRenderer implements GLSurfaceView.Renderer{
    public interface TextureLoader {
        void load(GL10 gl);
    }
    public StaticTriangleRenderer(Context context) {
        init(context, new RobotTextureLoader());
    }
    public StaticTriangleRenderer(Context context, TextureLoader loader) {
        init(context, loader);
    }
    private void init(Context context, TextureLoader loader) {
        mContext = context;
        mTriangle = new Triangle();
        mTextureLoader = loader;
    }
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        glDisable(GL_DITHER);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT,
                GL_FASTEST);
        glClearColor(.5f, .5f, .5f, 1);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
        int[] textures = new int[1];
        glGenTextures(1, textures, 0);
        mTextureID = textures[0];
        glBindTexture(GL_TEXTURE_2D, mTextureID);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D,
                GL_TEXTURE_MAG_FILTER,
                GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                GL_CLAMP_TO_EDGE);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
                GL_REPLACE);
        mTextureLoader.load(gl);
    }
    public void onDrawFrame(GL10 gl) {
        glDisable(GL_DITHER);
        glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
                GL_MODULATE);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTextureID);
        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                GL_REPEAT);
        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                GL_REPEAT);
        long time = SystemClock.uptimeMillis() % 4000L;
        float angle = 0.090f * ((int) time);
        glRotatef(angle, 0, 0, 1.0f);
        mTriangle.draw(gl);
    }
    public void onSurfaceChanged(GL10 gl, int w, int h) {
        glViewport(0, 0, w, h);
        float ratio = (float) w / h;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustumf(-ratio, ratio, -1, 1, 3, 7);
    }
    private Context mContext;
    private Triangle mTriangle;
    private int mTextureID;
    private TextureLoader mTextureLoader;
    private class RobotTextureLoader implements TextureLoader {
        public void load(GL10 gl) {
            InputStream is = mContext.getResources().openRawResource(
                    R.raw.robot);
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(is);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }
            GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
            bitmap.recycle();
        }
    }
    static class Triangle {
        public Triangle() {
            ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
            vbb.order(ByteOrder.nativeOrder());
            mFVertexBuffer = vbb.asFloatBuffer();
            ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
            tbb.order(ByteOrder.nativeOrder());
            mTexBuffer = tbb.asFloatBuffer();
            ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
            ibb.order(ByteOrder.nativeOrder());
            mIndexBuffer = ibb.asShortBuffer();
            float[] coords = {
                    -0.5f, -0.25f, 0,
                     0.5f, -0.25f, 0,
                     0.0f,  0.559016994f, 0
            };
            for (int i = 0; i < VERTS; i++) {
                for(int j = 0; j < 3; j++) {
                    mFVertexBuffer.put(coords[i*3+j] * 2.0f);
                }
            }
            for (int i = 0; i < VERTS; i++) {
                for(int j = 0; j < 2; j++) {
                    mTexBuffer.put(coords[i*3+j] * 2.0f + 0.5f);
                }
            }
            for(int i = 0; i < VERTS; i++) {
                mIndexBuffer.put((short) i);
            }
            mFVertexBuffer.position(0);
            mTexBuffer.position(0);
            mIndexBuffer.position(0);
        }
        public void draw(GL10 gl) {
            glFrontFace(GL_CCW);
            glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);
            glEnable(GL_TEXTURE_2D);
            glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);
            glDrawElements(GL_TRIANGLE_STRIP, VERTS,
                    GL_UNSIGNED_SHORT, mIndexBuffer);
        }
        private final static int VERTS = 3;
        private FloatBuffer mFVertexBuffer;
        private FloatBuffer mTexBuffer;
        private ShortBuffer mIndexBuffer;
    }
}
3.) Add android.pkm and robot.png files attached with this post to res/raw folder.
4.) Run for output.
Steps:
1.) Create a project named OpenglesAnimationExample1 and set the information as stated in the image.
Build Target: Android 4.0
Application Name: OpenglesAnimationExample1
Package Name: com. example. OpenglesAnimationExample1
Activity Name: OpenglesAnimationExample1Activity
Min SDK Version: 14


2.) Open OpenglesAnimationExample1Activity.java file and write following code there:
package com.example.OpenglesAnimationExample1; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.ETC1Util;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
public class OpenglesAnimationExample1Activity extends Activity {
    private final static String TAG = "CompressedTextureActivity";
    private final static boolean TEST_CREATE_TEXTURE = false;
    private final static boolean USE_STREAM_IO = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLView = new GLSurfaceView(this);
        mGLView.setEGLConfigChooser(false);
        StaticTriangleRenderer.TextureLoader loader;
        if (TEST_CREATE_TEXTURE) {
            loader = new SyntheticCompressedTextureLoader();
        } else {
            loader = new CompressedTextureLoader();
        }
        mGLView.setRenderer(new StaticTriangleRenderer(this, loader));
        setContentView(mGLView);
    }
    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }
    private class CompressedTextureLoader implements StaticTriangleRenderer.TextureLoader {
        public void load(GL10 gl) {
            Log.w(TAG, "ETC1 texture support: " + ETC1Util.isETC1Supported());
            InputStream input = getResources().openRawResource(R.raw.androids);
            try {
                ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                        GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, input);
            } catch (IOException e) {
                Log.w(TAG, "Could not load texture: " + e);
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
    }
    private class SyntheticCompressedTextureLoader implements StaticTriangleRenderer.TextureLoader {
        public void load(GL10 gl) {
            int width = 128;
            int height = 128;
            Buffer image = createImage(width, height);
            ETC1Util.ETC1Texture etc1Texture = ETC1Util.compressTexture(image, width, height, 3, 3 * width);
            if (USE_STREAM_IO) {
                try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ETC1Util.writeTexture(etc1Texture, bos);
                    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                    ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                            GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, bis);
                } catch (IOException e) {
                    Log.w(TAG, "Could not load texture: " + e);
                }
            } else {
                ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
                        GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, etc1Texture);
            }
        }
        private Buffer createImage(int width, int height) {
            int stride = 3 * width;
            ByteBuffer image = ByteBuffer.allocateDirect(height * stride)
                .order(ByteOrder.nativeOrder());
            for (int t = 0; t < height; t++) {
                byte red = (byte)(255-2*t);
                byte green = (byte)(2*t);
                byte blue = 0;
                for (int x = 0; x < width; x++) {
                    int y = x ^ t;
                    image.position(stride*y+x*3);
                    image.put(red);
                    image.put(green);
                    image.put(blue);
                }
            }
            image.position(0);
            return image;
        }
    }
    private GLSurfaceView mGLView;
}

3.) Compile and build the project.

Output




(android-tutorial)

Wednesday, September 19, 2012

10 Best Open Source Android Applications for every Android developer

,
following up a selection of good open source Android apps development and considered itemizing them here in order that it could be useful for many others.
Example Apps by Android Team.
Could there be a better way to start off without going through the code of the developers who developed the framework? These are 15 various android sample apps developed by the primary developers of the Android framework. These include a few games, photostream, time display, home display shortcuts etc.
url : http://code.google.com/p/apps-for-android/


Remote Droid
RemoteDroid is undoubtedly an android app which turns your phone into a wireless keyboard and mouse along with touchpad, using your own wireless network. You can learn lot of things like linking to a network, managing user finger motion etc from its source.
url: http://code.google.com/p/remotedroid/

TorProxy and Shadow
TorProxy is an implementation of Tor for Android mobiles. Along with Shadow, it enables you to surf internet site anonymously through your cell phone. You can study regarding tunnelling socket connections, managing cookies etc by reading it’s source code.
url: http://www.cl.cam.ac.uk/research/dtg/code/svn/android-tor/ and http://www.cl.cam.ac.uk/research/dtg/android/tor/

Android SMSPopup
It is an Android app that will intercepts incoming text messages and shows them in the pop-up window. Besides becoming a time saver, this app also shows us the best way to interface with the built-in application that handles SMS.
url: http://code.google.com/p/android-smspopup/

Standup Timer
Standup Timer is an Android application that behaves as a basic, stand-up meeting stopwatch. It can be used to ensure that your stand-up assembly completes on time, and provides each of the members the same share of time to state their progress. You can learn how to operate the timer features simply by reading through the source code. In addition this applications has clear distinction between view, model etc and has large amount of util procedures that we can reuse in our app.
url: http://github.com/jwood/standup-timer

Foursquare
It is a four square client for android. This app is basically divided into two components; Reading through the source code you can discover how to make
url: http://code.google.com/p/foursquared/

Pedometer
The pedometer app attempts to take the number of steps you take every day. However the count isn’t precise, you can learn different things such as interacting with accelerometer, doing voice updates, running background services etc by just studying its source code.
url: http://code.google.com/p/pedometer/

opensudoku-android
OpenSudoku is an easy open source sudoku game. You can learn the best way to show things in a grid in your view and also how to interact with a website by reading its source code.
url: http://code.google.com/p/opensudoku-android/

ConnectBot
ConnectBot is a Secure Shell client for the Android platform. You will find lot of good stuff about this app’s source code. Check it out for your self :)
http://code.google.com/p/connectbot/

WordPress for Android
How can a person expect a list of apps from me without mentioning WordPress ;) This android app is from the official WordPress development team. You can learn steps to make XMLRPC calls (as well as other cool things) by reading its source code.
url: http://android.svn.wordpress.org/trunk/

If you got worthwhile open source android apps from where we could learn something, then do leave a comment and I will include them up here, If anyone interested in learning Android app programming can visit EDUmobile.ORG


(android-tutorial.com)

Using Facebook SDK in Android development

,
Part I:
SDK Version: 
M3
fbConnecting to Facebook from an Android application is not as easy, as it looks. This guide will help you through some problems that you will propably encounter, with a clear and simple solution.
1. Step
First of all, download the official Facebook SDK from this site: https://github.com/facebook/facebook-android-sdk/
After that, create an application in this http://www.facebook.com/developers/ site, to get an APP ID.
2. Step
You need to create the key hash value of your signature and your android debugkeystore (for the develop stage), and than add them to your Facebook Application, in the application site. (Edit Settings -> Mobile and Devices section)
To do this, we need Openssl, download from: http://code.google.com/p/openssl-for-windows/downloads/list, and extract to a folder (in my case, c:\openssl).
To create this hash values, you need to navigate to your JAVA jdk folder, where the keytool.exe is. (In my case, in windows is: c:\Program Files(x86)\Java\jdk 1.6.0_24\bin)
Copy your debug.keystore to there from the (in my case) c:\Users\MyUserName\.android folder. In the jdk/bin folder, open a command prompt, and execute the following:
keytool -exportcert -alias androiddebugkey -keystore debug.keystore > c:\openssl\bin\debug.txt
Prompt1
(Use your openssl folder, and hit enter when asking password)
Navigate to the openssl/bin folder, and we have a debug.txt here, which contains the keystore values, but not in the expected format! Open a command promt from there, and execute the following commands:

  1. openssl sha1 -binary debug.txt > debug_sha.txt
  2. openssl base64 -in debug_sha.txt > debug_base64.txt
Prompt2
And now we are DONE! The debug_base64.txt contains the hash value, we need to copy it to the application site, in the Mobile and Devices section.
AddHash
You need to do the this hash creating flow with your signature too, to make working apk-s!
3. Step
Finally we can start coding... In the next part!



Part II: 

In this part, I show you an android application, which logging in to Facebook, then get the Facebook ID.
Please go through the first part of the tutorial, before reading this post.
1. Step
Create a new Android project in Eclipse, choose "create project from existing source", and set the location to: "Your Facebook SDK"\facebook. In my case it's D:\facebook android sdk\facebook.
sdk
2. Step
Create a new empty Android project, and open project properties/android. Hit Add in the Library section, add the facebook project.
sdk
3. Step
Change the main.xml like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <TextView android:layout_width="fill_parent" android:id="@+id/textFacebook"
  6.                 android:gravity="center_horizontal" android:layout_height="wrap_content"
  7.                 android:text="@string/welcome" android:layout_alignParentTop="true" />
  8.         <Button android:text="@string/enter" android:id="@+id/buttonLogin"
  9.                 android:layout_below="@+id/textFacebook"
  10.                 android:layout_centerHorizontal="true" android:layout_width="wrap_content"
  11.                 android:layout_height="wrap_content" android:layout_marginTop="30dip"></Button>
  12.         <ProgressBar android:id="@+id/progressLogin"
  13.                 android:layout_centerInParent="true" android:layout_width="wrap_content"
  14.                 android:visibility="gone" android:layout_height="wrap_content"></ProgressBar>
  15. </RelativeLayout>
And add Internet Permission to the AndroidManifest.xml:

  1.         <uses-permission android:name="android.permission.INTERNET">
  2.         </uses-permission>
My strings.xml looks like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="welcome">Welcome </string>
  4.     <string name="app_name">FacebookTest</string>
  5.     <string name="enter">Log in to Facebook</string>
  6. </resources>
3. Step
Create an abstract class, for the Facebook connection. Don't forget to insert your app id!

  1. public abstract class FBConnectionActivity extends Activity {
  2.         public static final String TAG = "FACEBOOK";
  3.         private Facebook mFacebook;
  4.         public static final String APP_ID = "INSERT YOUR APP ID";
  5.         private AsyncFacebookRunner mAsyncRunner;
  6.         private static final String[] PERMS = new String[] { "read_stream" };
  7.         private SharedPreferences sharedPrefs;
  8.         private Context mContext;
  9.  
  10.         private TextView username;
  11.         private ProgressBar pb;
  12.  
  13.         public void setConnection() {
  14.                 mContext = this;
  15.                 mFacebook = new Facebook(APP_ID);
  16.                 mAsyncRunner = new AsyncFacebookRunner(mFacebook);
  17.         }
  18.  
  19.         public void getID(TextView txtUserName, ProgressBar progbar) {
  20.                 username = txtUserName;
  21.                 pb = progbar;
  22.                 if (isSession()) {
  23.                         Log.d(TAG, "sessionValid");
  24.                         mAsyncRunner.request("me", new IDRequestListener());
  25.                 } else {
  26.                         // no logged in, so relogin
  27.                         Log.d(TAG, "sessionNOTValid, relogin");
  28.                         mFacebook.authorize(this, PERMS, new LoginDialogListener());
  29.                 }
  30.         }
  31.  
  32.         public boolean isSession() {
  33.                 sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
  34.                 String access_token = sharedPrefs.getString("access_token", "x");
  35.                 Long expires = sharedPrefs.getLong("access_expires", -1);
  36.                 Log.d(TAG, access_token);
  37.  
  38.                 if (access_token != null && expires != -1) {
  39.                         mFacebook.setAccessToken(access_token);
  40.                         mFacebook.setAccessExpires(expires);
  41.                 }
  42.                 return mFacebook.isSessionValid();
  43.         }
  44.  
  45.         private class LoginDialogListener implements DialogListener {
  46.  
  47.                 @Override
  48.                 public void onComplete(Bundle values) {
  49.                         Log.d(TAG, "LoginONComplete");
  50.                         String token = mFacebook.getAccessToken();
  51.                         long token_expires = mFacebook.getAccessExpires();
  52.                         Log.d(TAG, "AccessToken: " + token);
  53.                         Log.d(TAG, "AccessExpires: " + token_expires);
  54.                         sharedPrefs = PreferenceManager
  55.                                         .getDefaultSharedPreferences(mContext);
  56.                         sharedPrefs.edit().putLong("access_expires", token_expires)
  57.                                         .commit();
  58.                         sharedPrefs.edit().putString("access_token", token).commit();
  59.                         mAsyncRunner.request("me", new IDRequestListener());
  60.                 }
  61.  
  62.                 @Override
  63.                 public void onFacebookError(FacebookError e) {
  64.                         Log.d(TAG, "FacebookError: " + e.getMessage());
  65.                 }
  66.  
  67.                 @Override
  68.                 public void onError(DialogError e) {
  69.                         Log.d(TAG, "Error: " + e.getMessage());
  70.                 }
  71.  
  72.                 @Override
  73.                 public void onCancel() {
  74.                         Log.d(TAG, "OnCancel");
  75.                 }
  76.         }
  77.  
  78.         private class IDRequestListener implements RequestListener {
  79.  
  80.                 @Override
  81.                 public void onComplete(String response, Object state) {
  82.                         try {
  83.                                 Log.d(TAG, "IDRequestONComplete";);
  84.                                 Log.d(TAG, "Response: " + response.toString());
  85.                                 JSONObject json = Util.parseJson(response);
  86.  
  87.                                 final String id = json.getString("id");
  88.                                 final String name = json.getString("name");
  89.                                 FBConnectionActivity.this.runOnUiThread(new Runnable() {
  90.                                         public void run() {
  91.                                                 username.setText("Welcome: " + name+"\n ID: "+id);
  92.                                                 pb.setVisibility(ProgressBar.GONE);
  93.  
  94.                                         }
  95.                                 });
  96.                         } catch (JSONException e) {
  97.                                 Log.d(TAG, "JSONException: " + e.getMessage());
  98.                         } catch (FacebookError e) {
  99.                                 Log.d(TAG, "FacebookError: " + e.getMessage());
  100.                         }
  101.                 }
  102.  
  103.                 @Override
  104.                 public void onIOException(IOException e, Object state) {
  105.                         Log.d(TAG, "IOException: " + e.getMessage());
  106.                 }
  107.  
  108.                 @Override
  109.                 public void onFileNotFoundException(FileNotFoundException e,
  110.                                 Object state) {
  111.                         Log.d(TAG, "FileNotFoundException: " + e.getMessage());
  112.                 }
  113.  
  114.                 @Override
  115.                 public void onMalformedURLException(MalformedURLException e,
  116.                                 Object state) {
  117.                         Log.d(TAG, "MalformedURLException: " + e.getMessage());
  118.                 }
  119.  
  120.                 @Override
  121.                 public void onFacebookError(FacebookError e, Object state) {
  122.                         Log.d(TAG, "FacebookError: " + e.getMessage());
  123.                 }
  124.  
  125.         }
  126.  
  127.         @Override
  128.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  129.                 mFacebook.authorizeCallback(requestCode, resultCode, data);
  130.         }
  131. }
4. Step
Add the connection activity to the manifest:

  1. <activity android:name=".FBConnectionActivity&quot; android:label="@string/app_name"></activity>
5. Step
Change the Main activity like that:

  1. public class Main extends FBConnectionActivity {
  2.         private TextView txtUserName;
  3.         private ProgressBar pbLogin;
  4.         private Button btnLogin;
  5.        
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.        
  11.         txtUserName = (TextView) findViewById(R.id.textFacebook);
  12.         pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
  13.         btnLogin = (Button) findViewById(R.id.buttonLogin);
  14.                 btnLogin.setOnClickListener(new OnClickListener() {
  15.                         @Override
  16.                         public void onClick(View arg0) {
  17.                                 pbLogin.setVisibility(ProgressBar.VISIBLE);
  18.                                 setConnection();
  19.                                 getID(txtUserName, pbLogin);
  20.                         }
  21.                 });
  22.     }
  23. }
And see the result:
sdk

(helloandroid)
 

Android Development Tutorials Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger Templates