Tiny Acts

Tiny Acts

Home Quiz Apps Random Apps Engineering Apps Coding Tips

Coding Tips

Saving Highscore and Retrieving HighScore

First Application

SharedPreferences prefs = this.getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("name", variable);

Second Application

Initiate values

public static final String GAME_PREFS = "ArithmeticFile";
public int variable;
private SharedPreferences gamePrefs;

SharedPreferences prefs = this.getSharedPreferences("Name", Context.MODE_PRIVATE);
bestscore = prefs.getInt("name", 0);

0 is default.

Intent in another class thats not activity class from button

First Application

Context context = view.getContext();
Intent intent = new Intent(context , MainActivity.class);
intent.putExtra("name", Variable);

Second Application

Intent intent = getIntent();
Variable = intent.getIntExtra("name", 0);

0 is default value

Check if a number is divisible by another number

if (n % x == 0)

Calling Main Activity Method from another class

private static MainActivity instance;

instance = this;

public static MainActivity getInstance() { return instance; }

In other class


Random Number Generator

final int random = new Random().nextInt()

Inside nextInt() could be a array.length or a number

Simple timer

Chronometer is a simple way to set a timer for example a stop watch.

Add a chronometer to XML file for example below

Then the following code can be added to start, stop and reset (Please note: Variable needs to be created as type Chronometer and found by ID first)







Adding colour

Add the colours to the color resource file


Then add the colour to the main activity by .setColor(getResources().getColor(R.color.orange)). An example is shown below.


Opening new activity

Create an intent to open / change activities

Intent Varname = new Intent (this, Main2Activity.class);