Wednesday, December 8, 2010

Multiple substitutions specified in non-positional format

Since Android SDK 2.3 there's a new error:
error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?

Description:
This error indicates you're using a string resource with %s in it. See how to use it here under 'Formatting strings'. In general, instead of:
Hello, %s! You have %d new messages.
Should be:
Hello, %1$s! You have %2$d new messages.

Tuesday, December 7, 2010

Can not find adb.exe after Android SDK update

After updating the Android SDK I got the following message when I opened Eclipse:
Can not find C:\Program Files\Google\Android SDK\tools\adb.exe
Please check the Android plugin's preferences

And when I checked the adb.exe was really missing!

Solution:
Update the Android add-ins for Eclipse, in Eclipse: "Help" -> "Check for Updates".

The reason for this error (in my case) was that since SDK 2.3 the 'adb.exe' was moved to:
C:\Program Files\Google\Android SDK\platform-tools\adb.exe

UPDATE: ddms.bat does not work as well (can not find adb.exe). The work-around I'm using until I'll find a better solution is adding the platform-tools folder to the environment PATH variable.

Friday, November 26, 2010

AdMob as shows in Landscape but not in Portrait

This was really weird. I added AdMob Ad to an Android application, and it didn't show. I tried rotating to landscape & the ad appeared.

Solution: I found the solution here - the AdMob ad must not be padded, or be in a padded container.

Monday, November 8, 2010

Android application installation stuck on "Downloading"/"Installation"

Problem: Application installation hangs/stuck on "downloading" or "installing" from the Market. Reboot does not help.

Solution: Clearing the Android Market application cache -
* Menu => Settings => Applications => Manage Applications => "All" tab => Market
* Click on "Clear cache" (you might get 'Force Close' on the Market application after that).
* Reopen the market & download

Note: This is an issue with the Android Market application not the application you're downloading.

Thursday, October 14, 2010

Attachment disappears after e-mail sent programatically

I banged my head in the wall for about an hour to fix this issue.

I wrote a small function in Android that let the user send an e-mail from my application. Everything worked just fine, but when the e-mail was sent the attachment disappeared (the attachment appeared within the GMail client on the android device before sending).

Solution: use the external storage (SD Card) instead of the application cache. The GMail client probably can't access the application cache folder, buf for some reason it shows as if the attachment is OK in the client).

Tuesday, October 12, 2010

Android's ListView performance for large list

I needed to use a large ListView however the performance of the ListView loading time was very poor. Since I've used SimpleAdapter to map the data to the layout I was forced to populate all the data when creating the SimpleAdapter.

The solution I've decided to take is creating an adapter specific for the task, which populate the data from the objects only during creation of the item's view.

This is the pseudo code (note that the object data retrieval should by lazy too):

public class MyObjectAdapter extends BaseAdapter {
private ArrayList mData;
private int mResource;
private LayoutInflater mInflater;

public TimeZoneDisplayAdapter(Context context, ArrayList data,
int resource) {
mData = data;
mResource = resource;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
return mData.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null)
v = mInflater.inflate(mResource, parent, false);
else
v = convertView;

bindView(position, v);
return v;
}

private void bindView(int position, View view) {
MyObject o = mData.get(position);

((TextView)view.findViewById(R.id.list_item_resource_id)).setText(o.getData1());
......
......
......
}
}

Note: there are few examples online how to load data to ListView using threads. This wasn't my problem, and it's like killing a fly with a sledge-hammer. The loading time of a specific item was very short, the problem was that there were simply too many items to load at construction time.

Friday, October 8, 2010

Making TextView clickable (http)

The problem: I had a TextView which I wanted to make clickable, and open a web browser.

1. Brute force solution: change the TextView style to blue & underline. Set the onClick event to open web browser.

2. The following code:

SpannableString s = SpannableString.valueOf(entryText);
URLSpan span = new URLSpan(urlText);
s.setSpan(span, 0, entryText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(s);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());