Sunday 26 September 2010

Making Asynchronous calls in Android

I am sharing today a code snippet by which you can make an Asynchronous call in Android


It will be better that this Asynchronous class should be made as an inner class in an activity in which you wants to have an Asynchornous call and dont let UI hang till the process is completed.

private class AsyncClass extends AsyncTask {

        private final ProgressDialog dialog = new ProgressDialog(ABCActivity.this);
       
       
       
        @Override
        protected Void doInBackground(String... params) {
           
            //Make a call here during which you wants that UI shouldnt be hang. Making a server hit or saving something into DB etc. Remember the stuff added here should be non-UI.
           
            return null;
        }
       
       
        protected void onPreExecute() {
            dialog.setMessage("Fetching info...");
            dialog.setCancelable(false);
            dialog.show();






//Here you can add UI stuff which you wanted to show before making the sync call.
           
        }
       
        protected void onPostExecute(final Void unused) {
            if (dialog.isShowing()) {
                dialog.dismiss();
                               
            }
           
           //Here you can add UI stuff which you wanted to show after making the sync call.
       
        }

    }

Thursday 29 April 2010

Found shared references to a collection hibernate

 Today when i was working on my project i saw a very strange bug saying "Found shared references to a collection hibernate" with a long stacktrace. After wasting a long time browsing through different sites. I started seeing the code, there i found a very small thing which solved my problem and it was that i initialized the variable before starting FOR loop and was trying to add multiple objects of the same memory reference again and again. For e.g.

ArrayList list = new ArrayList();
String str= new String("test");
for(int i=0; i
    list.add(i);
}
here you will get the same Found shared references error. Therefore i simply brougth the initialization into the FOR loop and it got resolved

Thursday 22 April 2010

Disable the parent page when thickbox opens using jquery

I faced a problem that when the thickbox opens i can still TAB through the links by pressing tab button despite the model=true and all the stuff i can do through thickbox. After searching long i found that this is the issue all thickbox like libraries are facing that they cant block their parent page while the child div is open. So i did a workaround hope so that it will be useful for someone else too.

$('#content').live('keydown', function(e) {
  var keyCode = e.keyCode;

  if (keyCode == 9) {
    e.preventDefault();

  }
});

$('#footer').live('keydown', function(e) {
  var keyCode = e.keyCode;

  if (keyCode == 9) {
    e.preventDefault();

  }
});

$('#header').live('keydown', function(e) {
  var keyCode = e.keyCode;

  if (keyCode == 9) {
    e.preventDefault();

  }
});

This thing allows tabbing to be done in the child page while not allowing to tab on the parent page. Content, footer, and header are the id's of the divs in which i had my main content. footer and header

Tuesday 13 April 2010

Google launching android powered gpad

Google launching Android-based gPad???
After starting competition with Apple’s iPhone by throwing Nexus One into the market and giving the minimum standards what does Android based phone should have and showing that what can an Android based phone can do - does now Google thinking to launch Android based slate???
After seeing success of iPad, Google has started quickly working on Android based tablet which will be using Google’s branded hardware as Nexus One did.

Reports saying that Google has finally decided to launch its own Android-powered tablet which is right now in secrecy mode. The manufacturer yet is unknown but the people which are working on the project says that Google is testing with a few companies to explore delivery of books, magazines and other content on a tablet.

Sunday 28 March 2010

Gallery Example

Often we need in Android to make a view which contains all the images in a row and we can browse them by sliding them....Gallery is the control in android for this....

First of all you will have to make an inner class which extends BaseAdapter. Here's the code

public class ImageAdapter extends BaseAdapter {

       
        int mGalleryItemBackground;
        private Context mContext;

        private int[] mImageIds = {
                R.drawable.icon,R.drawable.android,R.drawable.array_03
        };


        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.default_gallery_android_galleryItemBackground ,mImageIds);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.default_gallery_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
}

Now the Activity class should look like this...

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
}


In your xml code you will just have to add these lines...

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

At the end your program will look like this