Skip to content

Commit

Permalink
Use system toast colors and ToastUtils class in all cases (getodk#2117)
Browse files Browse the repository at this point in the history
Starting in Android 8.1, Toasts have black text on a white background by default. Using system defaults ensures a consistent experience across applications and fixes an issue where toasts in 8.1 were showing up as white on white.
  • Loading branch information
grzesiek2010 authored and lognaturel committed Apr 17, 2018
1 parent 43871c7 commit 6bdba4d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
}
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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();
}
}
31 changes: 0 additions & 31 deletions collect_app/src/main/res/layout/toast_view.xml

This file was deleted.

1 change: 0 additions & 1 deletion collect_app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ the License.
<color name="tintColor">#2196F3</color>
<color name="black">#000000</color>
<color name="grey">#757575</color>
<color name="white">#ffffff</color>

</resources>

0 comments on commit 6bdba4d

Please sign in to comment.