Toast toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT); TextView v = (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.RED); toast.show();
Toast toast = Toast.makeText(this, "Centered\nmessage", Toast.LENGTH_SHORT); TextView v = (TextView) toast.getView().findViewById(android.R.id.message); if( v != null) v.setGravity(Gravity.CENTER); toast.show();
public static void showPopupEnablementToast(Context context) { Toast toast = Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG); TextView messageView = toast.getView().findViewById(android.R.id.message); if (messageView != null) messageView.setGravity(Gravity.CENTER); toast.show(); } }
Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG); View view = toast.getView(); view.setBackgroundResource(R.drawable.custom_bkg); TextView text = (TextView) view.findViewById(android.R.id.message); /*here you can do anything with text*/ toast.show();
Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT); //the default toast view group is a relativelayout RelativeLayout toastLayout = (RelativeLayout) toast.getView(); TextView toastTV = (TextView) toastLayout.getChildAt(0); toastTV.setTextSize(30); toast.show();
private void shortToast(String s) { toast.setText(s); if(!toast.getView().isShown()){ toast.show(); } }
private void initTN() { try { //todo android P test Field tnField = toast.getClass().getDeclaredField("mTN"); tnField.setAccessible(true); mTN = tnField.get(toast); show = mTN.getClass().getMethod("show"); hide = mTN.getClass().getMethod("hide"); Field tnParamsField = mTN.getClass().getDeclaredField("mParams"); tnParamsField.setAccessible(true); WindowManager.LayoutParams params = (WindowManager.LayoutParams) tnParamsField.get(mTN); params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.width = mWidth; params.height = mHeight; params.windowAnimations = 0; Field tnNextViewField = mTN.getClass().getDeclaredField("mNextView"); tnNextViewField.setAccessible(true); tnNextViewField.set(mTN, toast.getView()); } catch (Exception e) { e.printStackTrace(); } }
/** * Returns whether or not a particular custom {@code Toast} has been shown. * * @param message the message to search for * @param layoutResourceIdToCheckForMessage * the id of the resource that contains the toast messages * @return whether the {@code Toast} was requested */ public static boolean showedCustomToast(CharSequence message, int layoutResourceIdToCheckForMessage) { ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); for (Toast toast : shadowApplication.getShownToasts()) { String text = ((TextView) toast.getView().findViewById(layoutResourceIdToCheckForMessage)).getText().toString(); if (text.equals(message.toString())) { return true; } } return false; }
private void initTN() { try { Field tnField = toast.getClass().getDeclaredField("mTN"); tnField.setAccessible(true); mTN = tnField.get(toast); show = mTN.getClass().getMethod("show"); hide = mTN.getClass().getMethod("hide"); Field tnParamsField = mTN.getClass().getDeclaredField("mParams"); tnParamsField.setAccessible(true); WindowManager.LayoutParams params = (WindowManager.LayoutParams) tnParamsField.get(mTN); params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.width = mWidth; params.height = mHeight; params.windowAnimations = 0; Field tnNextViewField = mTN.getClass().getDeclaredField("mNextView"); tnNextViewField.setAccessible(true); tnNextViewField.set(mTN, toast.getView()); } catch (Exception e) { e.printStackTrace(); } }
public ToastAssert hasView(View view) { isNotNull(); View actualView = actual.getView(); assertThat(actualView) // .overridingErrorMessage("Expected view <%s> but was <%s>.", view, actualView) // .isSameAs(view); return this; }
@Test public void shouldSetViewCorrectly() throws Exception { Toast toast = new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); final View view = new TextView(context); toast.setView(view); assertThat(toast.getView()).isSameAs(view); }
Toast toast = null; if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) { toast = Toast.makeText(getApplicationContext(), "Text", Toast.LENGTH_SHORT); toast.show(); }
Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG); View view = toast.getView(); view.setBackgroundResource(R.drawable.custom_backgrround); TextView text = (TextView) view.findViewById(android.R.id.message); /*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/ toast.show();
Toast toast = Toast.makeText(this, "I am custom Toast!", Toast.LENGTH_LONG); View toastView = toast.getView(); //This'll return the default View of the Toast. /* And now you can get the TextView of the default View of the Toast. */ TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message); toastMessage.setTextSize(25); toastMessage.setTextColor(Color.RED); toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_fly, 0, 0, 0); toastMessage.setGravity(Gravity.CENTER); toastMessage.setCompoundDrawablePadding(16); toastView.setBackgroundColor(Color.CYAN); toast.show();
public static void positionToast(Toast toast, View view, Window window, int offsetX, int offsetY) { // toasts are positioned relatively to decor view, views relatively to their parents, we have to gather additional data to have a common coordinate system Rect rect = new Rect(); window.getDecorView().getWindowVisibleDisplayFrame(rect); // covert anchor view absolute position to a position which is relative to decor view int[] viewLocation = new int[2]; view.getLocationInWindow(viewLocation); int viewLeft = viewLocation[0] - rect.left; int viewTop = viewLocation[1] - rect.top; // measure toast to center it relatively to the anchor view DisplayMetrics metrics = new DisplayMetrics(); window.getWindowManager().getDefaultDisplay().getMetrics(metrics); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.widthPixels, MeasureSpec.UNSPECIFIED); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.heightPixels, MeasureSpec.UNSPECIFIED); toast.getView().measure(widthMeasureSpec, heightMeasureSpec); int toastWidth = toast.getView().getMeasuredWidth(); // compute toast offsets int toastX = viewLeft + (view.getWidth() - toastWidth) / 2 + offsetX; int toastY = viewTop + view.getHeight() + offsetY; toast.setGravity(Gravity.LEFT | Gravity.TOP, toastX, toastY); }
Toast toast = yourToastCreationCode(); if (null == toast.getView().getWindowToken()) { yeahToastIsInvisible(); }
public static Toast toastShow; public void showToast(Activity actRef, String message) { if (toastShow == null || toastShow.getView().getWindowVisibility() != View.VISIBLE) { toastShow = Toast.makeText(actRef, message, Toast.LENGTH_SHORT); toastShow.setGravity(Gravity.CENTER, 0, 0); toastShow.show(); } }
Toast toast = Toast.makeText(getApplicationContext(),"Welcome",Toast.LENGTH_LONG); View view = toast.getView(); TextView v = (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.WHITE); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0); ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#f43f10")); view.setBackgroundDrawable(colorDrawable); toast.show();
public static void showToast(Context ctx, int duration, String text) { Toast toast = Toast.makeText(ctx, text, duration); View mNextView = toast.getView(); if (mNextView != null) mNextView.setBackgroundResource(R.drawable.toast_frame); toast.show(); } }
static private void toastUp(String s, Application app) { Toast toast = Toast.makeText(app, s, Toast.LENGTH_SHORT); toast.getView().setBackgroundDrawable(null); toast.show(); }