Friday, March 30, 2012

java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

Avoiding Memory Leaks


http://developer.android.com/resources/articles/avoiding-memory-leaks.html

http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android


Thursday, March 29, 2012

ListView whose id attribute is 'android.R.id.list

The solution to this problem is


<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

the id should be "list" and it should be like "@android:id/list" not "@+id/list"

Monday, March 19, 2012

setSelection on Spinner based on rowId


http://stackoverflow.com/questions/2559611/setselection-on-spinner-based-on-rowid

If you want to set the selection of a Spinner thats backed by a CursorAdapter, you can loop through all the items in the Cursor and look for the one you want (assuming that the primary key in your table is named "_id"):
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new SimpleCursorAdapter(...));
for (int i = 0; i < spinner.getCount(); i++) {
    Cursor value = (Cursor) spinner.getItemAtPosition(i);
    long id = value.getLong(value.getColumnIndex("_id");
    if (id == rowid) {
        spinner.setSelection(i);
    }
}
If you want to get the rowid of a selected item, you can do something similar:
Cursor cursor = (Cursor) spinner.getSelectedItem();
long rowid = cursor.getLong(cursor.getColumnIndex("_id"));


Array Adapters

http://stackoverflow.com/questions/2390102/how-to-set-selected-item-of-spinner-by-value-not-by-position


You have to access the ArrayAdapter directly: Suppose your Spinner is named mySpinner, and it contains as one of its choices: "some value". To find the position of "some value" in the Spinner use this:
String myString = "some value"; //the value you want the position for
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
mySpinner.setSelection(spinnerPosition);

How To Build A Dashboard User Interface In Android

How to check internet connectivity in android


 public boolean isConnectedToNet() {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }
   return false;
    }

How to check internet connectivity in android


 public boolean isConnectedToNet() {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }
   return false;
    }
manifest

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

Regenerating R.java

In Eclipse, under the Project menu, is an option build automatically. That would help you build the R.java file everytime modifications are made. The Clean... option is also there under Project.




http://blog.burnayev.com/2009/11/android-developer-tip-regenerating.html

 I've just spent half an hour trying to fathom how to get back my auto-generated R.java that magically disappeared at some point.

The usual ways of resolving issues such as clean build, restarting IDE, and wiping the screen clean with a damp cloth didn't work.

Googling around did reveal a lot of similar complaints and revived a stark deja vu feeling. The hilarious thing is that once the R guy is gone, a good chunk of your code goes red thus adding to the insult.

Rather than continuing the discovery of all the conceivable strains of the issue found in the wild, I decided to leverage the tried-and-true approach of staring at the code. Having done so for a while I spotted a few red guys under res folder. They were a leftover of my current redesign work that I was about to delete before things went awry. Sure enough, as soon as I hit Del on them the darn R thing automagically reappeared.

The moral of the story is there's just so much magic Google can do for us. Bad resources (e.g. layouts with errors) are not treated gracefully by Google Android Eclipse plugin and can deceive it into deleting R.java with no sound reason.