Run gradle project as java application

18 07 2015

gradle run

is not a default task in gradle. To use it, you need to enable the ‘application’ plugin. To enable the plugin add this line to your build.gradle:

apply plugin: 'application'

This plugin requires one variable to be configured – the main class name:

mainClassName = "com.looksok.Main"

Now you can execute the

gradle clean build run

to run your app

Advertisements




Fix: SharedPreferences not saved after app restart

21 09 2012

It is quite common error that SharedPreference setting is being reset or cleared to default, after Android application is restarted. There are two things you have to remember.

Commit changes made by prefs editor (the obvious one)

prefsEditor.commit();

Clear prefs editor before using it (the tricky one)

prefsEditor.clear();

The clear() method clears your preferences made by editor. Clear() is also always called in commit() method (so after the change in editor was made).

Summary

So to solve this problem You need to write code like this (compare it with snippets in previous post):

public static void setPushEnabledFlag(Context context, boolean newValue) {
	SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	Editor prefsEditor = prefs.edit();
	prefsEditor.clear();
	prefsEditor.putBoolean(
			context.getString(R.string.settingsActivity_ReceivePushNotifications_key),
			newValue);
	prefsEditor.commit();
}

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Android tutorial: app preferences screen with shared preferences

23 06 2012

If there is a need to give some configuration options to the user, you can implement application settings screen, along with saving these settings for application. Settings will be stored in Shared Preferences – place where it will persist and can be read in future. This is a set of key-value pairs, that are stored even after application is closed.

Application settings screen, well known by the Android users looks like this:

1. Create Java class helping you to read and write Shared Preferences

In this tutorial there is just one preference to be set by the user. The class wil contain two methods: one for reading the setting from shared preferences, second one for storing the updated value (shared preference getter and setter):

public class AppSettingsHelper {

	public static boolean getPushEnabledFlag(Context context) {
	        SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	        return prefs.getBoolean(
	                context.getString(R.string.settingsActivity_ReceivePushNotifications_key),
	                true);
	}

	public static void setPushEnabledFlag(Context context, boolean newValue) {
	        SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	        Editor prefsEditor = prefs.edit();
	        prefsEditor.putBoolean(
	                context.getString(R.string.settingsActivity_ReceivePushNotifications_key),
	                newValue);
	        if(newValue){
	        	C2dmRegistrar.registerForPush(context);
	        }
	        prefsEditor.commit();
	}
}

2. Add new activity with preferences

Create new activity, where all preferences will be shown. Make it extending PreferencesActivity, not the Activity as usual. Add new Activity entry to AndroidManifest.xml. Here is how it looks like in my application:

public class SettingsActivity extends PreferenceActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(Constants.SP_MY_APPLICATION_SETTINGS);
        addPreferencesFromResource(R.xml.prefs);
    }
}

Here I am reading shared preference for this application (under the Constants.SP_MY_APPLICATION_SETTINGS key), and adding a new preferences from the resource. this is the whole code of my activity. Now what about resources?

3. Add settings options to the Preferences activity

Preferences that will be displayed on the screen are customized in xml file: res/xml/prefs.xml. Here I add one preference with checkbox:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="@string/settingsActivity_titile">
        <CheckBoxPreference
            android:summary="@string/settingsActivity_ReceivePushNotifications_summary"
            android:title="@string/settingsActivity_ReceivePushNotifications_title"
            android:key="@string/settingsActivity_ReceivePushNotifications_key" />
    </PreferenceCategory>
</PreferenceScreen>

title and summary are self-explanatory,and the android:key attribute is the name of preference stored in Shared Preferences.

4. How to launch Preferences activity?

As you wish. It can be lanched for example by some button in application, but the most intuitive way to do so is application menu called after the ‘menu’ button press. How to build such menu and call the activity from it, I described here: Android menu implementation tutorial.

My tutorial and implementation is inspired by very good article here.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: