Skip to content

Instantly share code, notes, and snippets.

View choiboi's full-sized avatar

James.C choiboi

View GitHub Profile
// 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();
}
});
// 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();
}
<!-- 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" />
// 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();
}
/*
* This method gets the image inside the template area and returns that bitmap.
* Process of how this method works:
* 1) Combine img and templateImage together with templateImage being on the top.
* 2) Create a new blank bitmap using the given width and height, which is the
* size of the template on the screen.
* 3) Starting in the center go through the image quadrant by quadrant copying
* the pixel value onto the new blank bitmap.
* 4) Once it hits the pixel colour values of the template lines, then set the pixel
* values from that point on transparent.
// Get the layout you want and set the values inside that layout.
View toastRoot = getLayoutInflater().inflate(R.layout.activity_toast_custom_toast, null);
TextView tvToast = (TextView) toastRoot.findViewById(R.id.toast_textview);
tvToast.setText(TOAST_CUSTOM_BG_MSG);
// Create new Toast object and set the view to the one you want.
Toast toast = new Toast(getApplicationContext());
toast.setView(toastRoot);
toast.show();
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_bubble"
android:orientation="vertical" >
<TextView
android:id="@+id/toast_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_NO_FILEPATH) {
// Get the image from intent data.
Bundle bundle = data.getExtras();
// This Bundle object will contain the Bitmap image,
// so no Bitmap decoding will be required.
Bitmap img = (Bitmap) bundle.get("data");
mImage.setImageBitmap(img);
}
}
/*
* 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));
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();
}