ListView: onListItemClick and On Touch

My app starts with an empty list and user has to click “Add” to create a first item.  First time users can’t figure out the Add (plus sign) button and they smack the finger on a blank screen, maybe something will come up to guide. Ok that’s a task: when user touches empty area on a list we want to treat it as Add button was clicked. 

My activity already implements onListItemClick and it’s void call

There is onTouch which is boolean. Let’s try

@Override
public boolean onTouch(View arg0, MotionEvent event) {

Log.d(TAG, “onTouch list”);

boolean ret=false;
return ret;
}

Simple enough though this is called 3 times when I touch once. 

Reading. I see, there are 3 event. I’ll handle the Action Up.

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//code
break;
case MotionEvent.ACTION_MOVE:
// code
break;
case MotionEvent.ACTION_UP:
if (lView.getAdapter().getCount()<=0){
startEditActivity(-1);
ret=true;
}
break;
}

This sounds simple but I spent some time figuring what the condition is for me to return true and when to return false, so that onListItemClick is still called. Ended up limiting this feature to a condition when list has no items. 

Good enough. 

Take care.

Going to read release procedures next. 

Please Don't Touch

Please Don’t Touch (Photo credit: Wikipedia)

 

 

In App Advertisements

Google has AdSense and AdWords

There is MoPub that people use and talk about. Only experience can tell (or do You?) what is better.

I had code sample for MoPub, let’s try it out.

1. MoPub Integration part1

http://help.mopub.com/customer/portal/articles/285186

This crashes at runtime…

Though I used MoPub successfully earlier (in sprint). Spent 2-3 and then switched to AdMob.

I have low tolerance to now this works now it doesn’t game.

2. AdMob by google. Easy to integrate, seem to be working well so far.

 

 

Send mail from the app

We want to implement Contact Us button. 

User will be able to send an e-mail to my designated mailbox. [I might check it :)]

1. 

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
} 

But this requires e-mail client (?) and doesnt have sender address. How would I respond then?

2.
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email"); intent.putExtra(Intent.EXTRA_TEXT, "Body of email"); intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app. startActivity(intent);
3. http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application
more code examples.
One of these should work I hope.

Preferences/Settings

Preferences (Settings) are used for storing values between sessions. These values could be save in a database but when values are just a few it can ge in preferences

1. Create

private void saveMyPreferences(String key, String value) {

SharedPreferences preferences = getApplicationContext().getSharedPreferences(A_PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
return ;
}

2. retrieve
private String getMyPreferences(String key) {
SharedPreferences preferences = getPreferences(A_PREFERENCES_NAME);

String defaultValue=””;

English: An Inglehart-Welzel Cultural Map of t...

English: An Inglehart-Welzel Cultural Map of the World: World Secular-Rational and Self Expression Values as a map of world cultures based on World Values Survey data. Svenska: En Världskulturkarta av Inglehart-Welzel typ. (Photo credit: Wikipedia)

return preferences.getString(key,defaultValue);
}

Unhandled Exceptions

After my app crashes on device it become unresponsive.  I have to go to setup and force stop.

No one would do this.. Let’s see if there is a way to kill the app and restart. Would be great to catch a crash log and e-mail it or copy to google docs. Will look into that later. For now I want the app to be always responsive.

Thread.UncaughtExceptionHandler onRuntimeError=
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread,
Throwable ex) {
Log.d(TAG, “uncaughtException!!!!!!!”);
/* ex.printStackTrace();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);*/
// here I do logging of exception to a db
PendingIntent myActivity = PendingIntent.getActivity(getBaseContext(),
192837, new Intent(getBaseContext(), TilesActivity.class),
PendingIntent.FLAG_ONE_SHOT);
// PendingIntent myActivity = PendingIntent.getActivity(this.getInstance().getBaseContext(), 0,
// new Intent(getIntent()), getIntent().getFlags());
AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
15000, myActivity );
System.exit(2);

// re-throw critical exception further to the os (important)
defaultUEH.uncaughtException(thread, ex);

}};
Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);

Works well. I put intentional divide by zero deep into my code. App got restarted. Some things are easy.

Now. How to e-mail the log?

Let’s collect data

1. https://code.google.com/p/android-log-collector/

2. http://acra.ch/

3. https://www.bugsense.com/ this claims to include real-time intelligence

4.  integrate Google Analytics into your app? You can configure to send crash reports every time your app freezes with stacktrace and all.

5. Ah, this is promising https://developers.google.com/analytics/devguides/collection/android/