Monday, August 5, 2013

Display the attribution text for Google Maps Android V2 by overriding the onPreferenceTreeClick of the PreferenceFragment activity

If you are using Google Maps Android API Version 2.0, it is necessary to include the Google Play Services attribution text as part of your Android application. I decided to implement this requirement by including a clickable "Legal Notices" preference on a settings fragment activity. The following shows how this is done.

In the Android preference XML file under the project\res\xml\ folder, create a preference with a title for Legal Notices as shown below. This preference is given the key string prefLegalNotices.


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 
<PreferenceCategory
android:title="About the dataset"
>
<Preference
android:key="prefLegalNotices"
android:title="Legal notices"
/>    
</PreferenceCategory>
 
</PreferenceScreen>

Then in the PreferenceFragment source file, override the onPreferenceTreeClick method.


public class SettingsFragment extends PreferenceFragment implements OnPreferenceChangeListener {
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
String key = preference.getKey();

//if the user click on the Legal Notices preference, display the license
if (key.equalsIgnoreCase("prefLegalNotices")){
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getActivity());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(getActivity());
LicenseDialog.setTitle("Legal Notices");
if (LicenseInfo!=null){
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();                
}            
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}

The screenshots below show how it works.


4 comments:

Anonymous said...

I cannot extend PreferenceFragment, because I am supporting lower than API 11. However, with your example, I get, "The method getActivity() is undefined for the type." How do I correct this for extending PreferenceActivity?


public class PreferencesActivity extends PreferenceActivity {

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);

}

@SuppressWarnings("deprecation")
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
{
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
return false;

String key = preference.getKey();
//if the user click on the Legal Notices preference, display the license
if (key.equalsIgnoreCase("prefGoogleLegal")){
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getActivity());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(getActivity());
LicenseDialog.setTitle("Legal Notices");
if (LicenseInfo!=null){
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();

}

}

return super.onPreferenceTreeClick(preferenceScreen, preference);

}
}

Anonymous said...

I cannot utilize PreferenceFragment because I am supporting API 10. However, because of that I get, "The method getActivity() is undefined for the type..." Is there a workaround for this in your provided code? Thanks!

dominoc925 said...

Hi thetuneupguy,

just do something like this{
dialog = new AlertDialog.Builder(this);
because PreferenceActivity is an activity

HTH

Anonymous said...

Roger that!

This did the trick...

String key = preference.getKey();
//if the user click on the Legal Notices preference, display the license
if (key.equalsIgnoreCase("prefGoogleLegal")){
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this);
Builder LicenseDialog = new AlertDialog.Builder(this);
LicenseDialog.setTitle("Legal Notice");
if (LicenseInfo!=null){
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();

}

}

return super.onPreferenceTreeClick(preferenceScreen, preference);

}