Friday, September 25, 2015

Using keytools

During early development i have trouble finding keytools or how to access them. Most developer in my understanding are using linux so it's like they can just type the keytools to run.

I tried to configure windows command once for them to be able to read inside JDK folder but now forgot already after upgrading my OS to Win10. This is like shortcut stuff so here goes

Go to your JDK install folder. Mine is here and usually everyone is:
C:\Program Files\Java\jdk1.7.0_79

Shift+RightClick on 'bin' folder and select 'open command windows here'

Now you are inside jdk bin folder and keytools command can be used.

type your keytools command here, for example

keytool -exportcert -alias your-key-name -keystore /path/to/your/keystore/file -list -v

your-key-name is your app-key you create
path should be location of your key ie c:\myapp\hello_world\key.jks

Sunday, September 20, 2015

Delay using Handler

private Handler mHandler = new Handler();

private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            // do what you need to do here after the delay
            
        }
    };
insert this anywhere you want to run
mHandler.postDelayed(mUpdateTimeTask, 2000);

SetTextColor


text.setTextColor(Color.parseColor("#FFFFFF"));

Saturday, September 19, 2015

start new activity

// Define intent to play again
final Intent playagainIntent = new Intent(this, PlayActivity.class);

ActivityOptions options = ActivityOptions.makeCustomAnimation(GameOverActivity.this, R.anim.slide_up, R.anim.slide_down);
startActivity(playagainIntent, options.toBundle());

Required XML attribute “adSize” was missing

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="YOUR_AD_UNIT_ID"
    ads:adSize="BANNER"/>
basically, add this line in the ads xml
xmlns:ads="http://schemas.android.com/apk/res-auto"

Device Test ID for Admob

If you are running admob ads on an emulator then there is no ID. just use the AdManager method and set it to TEST_EMULATOR like the logcat says. If you run on an actual device with usb debugging and watch the logcat, the ID will appear in there. You can filter the logs by using "device" or "adRequest" (Case Sensitive) as the filter. After filtering using the above mentioned terms "adRequest", you'll find a line -
12-19 17:48:25.615: I/Ads(2132): To get test ads on this device, call adRequest.addTestDevice("D9XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

Thread/ Sleep for few seconds

anywhere in oncreate
Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 3 seconds

                    sleep(6 * 1000);

                    // After 3 seconds redirect to another intent
                    Intent mainmenu = new Intent(getBaseContext(), MainActivity.class);
                    // Use fade in fade out transition animation in startActivity
                    ActivityOptions options = ActivityOptions.makeCustomAnimation(intro.this, R.anim.fade_in, R.anim.fade_out);
                    startActivity(mainmenu,options.toBundle());

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

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