Skip to content

Instantly share code, notes, and snippets.

View android_button_implement_listener.java
// Overriden onClick method. This method will invoke for buttons that has been set with the on click listener
// with this class.
@Override
public void onClick(View v) {
Toast.makeText(this, "Button listener implemented through this class pressed!", Toast.LENGTH_LONG).show();
}
View android_button_xml_listener_xml.xml
<!-- Must have onClick attribute with the method name as the value. -->
<Button
android:id="@+id/button_xml_attribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_xml_attribute_text"
android:onClick="onButtonPressed" />
View android_button_xml_listener_java.java
// This method will invoke with the button that has the xml attribute onClick set with this method name.
public void onButtonPressed(View v) {
Toast.makeText(this, "Button listener implemented xml onClick attribute!", Toast.LENGTH_LONG).show();
}
View android_button_instantiate_listener.java
// Button listener implemented by not implmenting this class with
// OnClickListener. When button is pressed, it will invoke the onClick method
// inside the OnClickListener object created when the listener has been set.
Button implementListenerButton = (Button) findViewById(R.id.button_object_onclicklistener);
implementListenerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(ButtonMainActivity.this, "Button listener created while setting the button listener pressed!", Toast.LENGTH_LONG).show();
}
});
View android_gallery_getpath.java
View android_gallery_chooser_intent.java
View android_gallery_default_app_intent.java
View android_camera_with_filepath_onActivityResult.java
if (resultCode == RESULT_OK) {
...
else if (requestCode == CAMERA_WITH_FILEPATH) {
// Get the image from the filepath you specified when you
// started the camera intent.
String filepath = getOutputLink(TEMP_JPEG_FILENAME);
Bitmap img = BitmapFactory.decodeFile(filepath);
mImage.setImageBitmap(img);
Toast.makeText(this, "Image is saved in: " + filepath, Toast.LENGTH_SHORT).show();
}
View android_camera_with_filepath.java
// Create a new File object with specified filepath, where the
// captured image will be located.
File file = new File(getOutputLink(TEMP_JPEG_FILENAME));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(cameraIntent, CAMERA_WITH_FILEPATH);
View android_camera_filepath.java
/*
* Creates a new file path into the standard Android pictures directory.
*/
private String getOutputLink(String filename) {
String directory = "";
// Check if storage is mounted.
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
getResources().getString(R.string.app_name));