Saturday, September 19, 2015

How to toast

Toast.makeText(getApplicationContext(), "You are offline. Login first.", Toast.LENGTH_LONG).show();

Save value for later use using shared preferences

//1. setting up sharedPreferences for signoutstate
            SharedPreferences explicitSignOut = getSharedPreferences("madtrain", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = explicitSignOut.edit();
            editor.putInt("explicitsignout", signout);
            editor.commit();

Thursday, September 17, 2015

Compare string

== tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically "equal"). Consequently, if you want to test whether two strings have the same value you should use .equals().
// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 
You almost always want to use.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.

Wednesday, September 16, 2015

Passing value to other activity

First activity
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
in new activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}
Send value Intent i = new Intent(getBaseContext(), PlayActivity.class); ActivityOptions options = ActivityOptions.makeCustomAnimation(Story1Activity.this, R.anim.fade_in, R.anim.fade_out); i.putExtra(carryLoginstatus,loginStatus); startActivity(i, options.toBundle()); Retrieve value Intent gameoverIntent = getIntent(); String scoreCount = gameoverIntent.getStringExtra(PlayActivity.finalScore);

Tuesday, September 15, 2015

SharedPrefences Problem

SharedPreferences cannot be called from outside onCreate()
public class MainActivity extends Activity implements
        SoundPool.OnLoadCompleteListener,
        View.OnClickListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);


        SharedPreferences loginState =getSharedPreferences("loginState", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = loginState.edit();

..
..
} //END ONCREATE


    @Override
    public void onConnected(Bundle connectionHint) {
        // The player is signed in. Hide the sign-in button and allow the
        // player to proceed.

HERE WE WANT TO COMMIT

    } 
} // END MAIN ACTIVITY