Android Turorial.pdf
Dirt Cross App (Cloud)


In this tutorial we’ll go with getSharedPreferences(). The method is defined as follows:

getSharedPreferences (String PREFS_NAME, int mode)

PREFS_NAME is the name of the file.

mode is the operating mode.

Following are the operating modes applicable:

  • MODE_PRIVATE: the default mode, where the created file can only be accessed by the calling application
  • MODE_WORLD_READABLE: Creating world-readable files is very dangerous, and likely to cause security holes in applications
  • MODE_WORLD_WRITEABLE: Creating world-writable files is very dangerous, and likely to cause security holes in applications
  • MODE_MULTI_PROCESS: This method will check for modification of preferences even if the Shared Preference instance has already been loaded
  • MODE_APPEND: This will append the new preferences with the already existing preferences
  • MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is set, it would enable write ahead logging by default


We need an editor to edit and save the changes in shared preferences. The following code can be used to get the shared preferences.

            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            Editor editor = pref.edit();

Storing Data

editor.commit() is used in order to save changes to shared preferences.

          editor.putBoolean("key_name", true); // Storing boolean - true/false
          editor.putString("key_name", "string value"); // Storing string
          editor.putInt("key_name", "int value"); // Storing integer
          editor.putFloat("key_name", "float value"); // Storing float
          editor.putLong("key_name", "long value"); // Storing long
          editor.commit(); // commit changes

Retrieving Data

Data can be retrieved from saved preferences by calling getString() as follows:

          String str = pref.getString("key_name", null); // getting String
          int x = pref.getInt("key_name", -1); // getting Integer
          float f = pref.getFloat("key_name", null); // getting Float
          long l = pref.getLong("key_name", null); // getting Long
          boolean b = pref.getBoolean("key_name", null); // getting boolean

Clearing or Deleting Data

remove(“key_name”) is used to delete that particular value.

clear() is used to remove all data

            editor.remove("name"); // will delete key name
            editor.remove("email"); // will delete key email
            editor.commit(); // commit changes

              editor.commit(); // commit changes

Logic for Love Calculator App

            If 2 names are Kete and Rade
            ASCII value of K   =     75
            ASCII value of E    =     69
            ASCII value of T    =     84
            ASCII value of E    =     69
            ASCII value of R    =     72
            ASCII value of A    =     65
            ASCII value of D    =     68
            ASCII value of E    =     69

            Sum of  ASCII values of K+E+T+E + R + A + D + E =     581
            now 581 % 100 = 81

          So, the love % between Kete and  Rade= 81%
Download Code

QR Code Library

Add the following line to app/build.gradle

          implementation 'me.dm7.barcodescanner:zxing:1.9.13'
          implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3'

Scanning Java Code

            public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
              private ZXingScannerView mScannerView;
              private String TAG = "QR SCAN";
              protected void onCreate(Bundle savedInstanceState) {
                  mScannerView = new ZXingScannerView(this);
              protected void onResume() {
              public void onPause() {
              public void handleResult(Result rawResult) {
                  //Handle result here
                  Toast.makeText(this, rawResult.getText(), Toast.LENGTH_SHORT).show();

Adding Camera Permision

Add the following line to

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.CAMERA)
                    == PackageManager.PERMISSION_DENIED
            ) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);

Add the following line to AndroidManifest.xml

            uses-permission android:name="android.permission.CAMERA"