android findviewbyid code example

Example 1: findview by id in android

This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.

Example 2: viewwillappear android

Activity.onResume

Example 3: onbackpressed android

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("Really Exit?")
        .setMessage("Are you sure you want to exit?")
        .setNegativeButton(android.R.string.no, null)
        .setPositiveButton(android.R.string.yes, new OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
                WelcomeActivity.super.onBackPressed();
            }
        }).create().show();
}

Tags:

Misc Example