diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java index 980acc628a4..ae1c1dd219f 100644 --- a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java @@ -39,9 +39,7 @@ import android.view.ContextMenu.ContextMenuInfo; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; -import android.view.Gravity; import android.view.KeyEvent; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; @@ -61,7 +59,6 @@ import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; -import android.widget.Toast; import com.google.common.collect.ImmutableList; import com.google.zxing.integration.android.IntentIntegrator; @@ -808,14 +805,14 @@ private void saveChosenImage(Uri selectedImage) { runOnUiThread(() -> { dismissDialog(SAVING_IMAGE_DIALOG); Timber.e("Could not receive chosen image"); - showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT); + ToastUtils.showShortToastInMiddle(R.string.error_occured); }); } } catch (GDriveConnectionException e) { runOnUiThread(() -> { dismissDialog(SAVING_IMAGE_DIALOG); Timber.e("Could not receive chosen image due to connection problem"); - showCustomToast(getString(R.string.gdrive_connection_exception), Toast.LENGTH_LONG); + ToastUtils.showLongToastInMiddle(R.string.gdrive_connection_exception); }); } } @@ -1627,27 +1624,7 @@ private void createConstraintToast(FormIndex index, int saveStatus) { return; } - showCustomToast(constraintText, Toast.LENGTH_SHORT); - } - - /** - * Creates a toast with the specified message. - */ - private void showCustomToast(String message, int duration) { - LayoutInflater inflater = (LayoutInflater) getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - - View view = inflater.inflate(R.layout.toast_view, null); - - // set the text in the view - TextView tv = view.findViewById(R.id.message); - tv.setText(message); - - Toast t = new Toast(this); - t.setView(view); - t.setDuration(duration); - t.setGravity(Gravity.CENTER, 0, 0); - t.show(); + ToastUtils.showShortToastInMiddle(constraintText); } /** diff --git a/collect_app/src/main/java/org/odk/collect/android/utilities/ToastUtils.java b/collect_app/src/main/java/org/odk/collect/android/utilities/ToastUtils.java index 0fdb81b35cc..5d70c78f5a8 100644 --- a/collect_app/src/main/java/org/odk/collect/android/utilities/ToastUtils.java +++ b/collect_app/src/main/java/org/odk/collect/android/utilities/ToastUtils.java @@ -1,10 +1,12 @@ package org.odk.collect.android.utilities; +import android.view.Gravity; +import android.view.ViewGroup; +import android.widget.TextView; import android.widget.Toast; import org.odk.collect.android.application.Collect; - public class ToastUtils { private ToastUtils() { @@ -34,4 +36,27 @@ private static void showToast(String message, int duration) { private static void showToast(int messageResource, int duration) { Toast.makeText(Collect.getInstance(), Collect.getInstance().getString(messageResource), duration).show(); } + + public static void showShortToastInMiddle(int messageResource) { + showToastInMiddle(Collect.getInstance().getString(messageResource), Toast.LENGTH_SHORT); + } + + public static void showShortToastInMiddle(String message) { + showToastInMiddle(message, Toast.LENGTH_SHORT); + } + + public static void showLongToastInMiddle(int messageResource) { + showToastInMiddle(Collect.getInstance().getString(messageResource), Toast.LENGTH_LONG); + } + + private static void showToastInMiddle(String message, int duration) { + Toast toast = Toast.makeText(Collect.getInstance(), message, duration); + ViewGroup group = (ViewGroup) toast.getView(); + TextView messageTextView = (TextView) group.getChildAt(0); + messageTextView.setTextSize(21); + messageTextView.setGravity(Gravity.CENTER); + + toast.setGravity(Gravity.CENTER, 0, 0); + toast.show(); + } } diff --git a/collect_app/src/main/res/layout/toast_view.xml b/collect_app/src/main/res/layout/toast_view.xml deleted file mode 100644 index d4173b5b854..00000000000 --- a/collect_app/src/main/res/layout/toast_view.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - diff --git a/collect_app/src/main/res/values/colors.xml b/collect_app/src/main/res/values/colors.xml index 183292afaf2..2260437985d 100644 --- a/collect_app/src/main/res/values/colors.xml +++ b/collect_app/src/main/res/values/colors.xml @@ -19,6 +19,5 @@ the License. #2196F3 #000000 #757575 - #ffffff \ No newline at end of file