LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
LayoutInflater myInflater=LayoutInflater.from(this); View view=myInflater.inflate(R.layout.your_custom_layout,null); Toast mytoast=new Toast(this); mytoast.setView(view); mytoast.setDuration(Toast.LENGTH_LONG); mytoast.show();
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
/** * 显示有image的toast * * @param tvStr * @param imageResource * @return */ public static Toast showToastWithImg(final String tvStr, final int imageResource) { if (toast2 == null) { toast2 = new Toast(BaseApplication.getAppContext()); } View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null); TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv); tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr); ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv); if (imageResource > 0) { iv.setVisibility(View.VISIBLE); iv.setImageResource(imageResource); } else { iv.setVisibility(View.GONE); } toast2.setView(view); toast2.setGravity(Gravity.CENTER, 0, 0); toast2.show(); return toast2; } }
public View getView(int position, View convertView, @NonNull ViewGroup parent) { ItemFolderListBinding binding = (convertView == null) ? DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_folder_list, parent, false) : DataBindingUtil.bind(convertView); binding.label.setText(TextUtils.isEmpty(folder.label) ? folder.id : folder.label); binding.directory.setText(folder.path); binding.override.setOnClickListener(v -> { Intent intent = new Intent(mContext, SyncthingService.class) .putExtra(SyncthingService.EXTRA_FOLDER_ID, folder.id); intent.setAction(SyncthingService.ACTION_OVERRIDE_CHANGES); mContext.startService(intent); }); mContext.startActivity(chooserIntent); } else { Toast.makeText(mContext, R.string.toast_no_file_manager, Toast.LENGTH_SHORT).show();
new LoadRecommend().execute(tryCount); } else { Toast.makeText(mContext, "网络连接失败", Toast.LENGTH_SHORT).show(); View tryAgain = LayoutInflater.from(mContext).inflate(R.layout.try_again, mViewContent, false); tryAgain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v1 = mLayoutInflater.inflate(R.layout.recommend_playlist, mViewContent, false); mRecyclerView1 = (RecyclerView) v1.findViewById(R.id.recommend_playlist_recyclerview); mGridLayoutManager = new GridLayoutManager(mContext, 3); mRecyclerView1.setLayoutManager(mGridLayoutManager); mRecyclerView1.setAdapter(mRecomendAdapter); TextView more = (TextView) v1.findViewById(R.id.more); more.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
@ColorInt int tintColor, @ColorInt int textColor, int duration, boolean withIcon, boolean shouldTint) { final Toast currentToast = Toast.makeText(context, "", duration); final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(R.layout.toast_layout, null); final ImageView toastIcon = toastLayout.findViewById(R.id.toast_icon); final TextView toastTextView = toastLayout.findViewById(R.id.toast_text); Drawable drawableFrame; ToastyUtils.setBackground(toastIcon, tintIcon ? ToastyUtils.tintIcon(icon, textColor) : icon); } else { toastIcon.setVisibility(View.GONE); toastTextView.setText(message); toastTextView.setTextColor(textColor); toastTextView.setTypeface(currentTypeface); toastTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); currentToast.setView(toastLayout); lastToast.cancel(); lastToast = currentToast;
@Override public void onClick(View v) { super.onClick(v); int i = v.getId(); if (i == R.id.thumb) { if (jzDataSource == null || jzDataSource.urlsMap.isEmpty() || jzDataSource.getCurrentUrl() == null) { Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); return; LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.jz_layout_clarity, null); TextView clarityItem = (TextView) View.inflate(getContext(), R.layout.jz_layout_clarity_item, null); clarityItem.setText(key); clarityItem.setTag(j); layout.addView(clarityItem, j); clarityItem.setOnClickListener(mQualityListener); if (j == jzDataSource.currentUrlIndex) { clarityItem.setTextColor(Color.parseColor("#fff85959")); } else if (i == R.id.retry_btn) { if (jzDataSource.urlsMap.isEmpty() || jzDataSource.getCurrentUrl() == null) { Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); return;
@Override public void showError(boolean firstPage, Throwable error) { String errorMessage = error.getMessage(); if (firstPage) { View tipsView = TipsUtils.showTips(mRecyclerView, TipsType.LOADING_FAILED); tipsView.findViewById(R.id.retry_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mFragment.refresh(); } }); if (!TextUtils.isEmpty(errorMessage)) { ((TextView) tipsView.findViewById(R.id.description)).setText(errorMessage); } return; } Toast.makeText(mLoadingView.getContext(), errorMessage, Toast.LENGTH_LONG).show(); }
private void transition(View view) { if (Build.VERSION.SDK_INT < 21) { Toast.makeText(ActivityTransitionActivity.this, "21+ only, keep out", Toast.LENGTH_SHORT).show(); } else { Intent intent = new Intent(ActivityTransitionActivity.this, ActivityTransitionToActivity.class); ActivityOptionsCompat options = ActivityOptionsCompat. makeSceneTransitionAnimation(ActivityTransitionActivity.this, view, getString(R.string.transition_test)); startActivity(intent, options.toBundle()); } } }
@Override public void onClick(View v) { int badgeCount = 0; try { badgeCount = Integer.parseInt(numInput.getText().toString()); } catch (NumberFormatException e) { Toast.makeText(getApplicationContext(), "Error input", Toast.LENGTH_SHORT).show(); } finish(); startService( new Intent(MainActivity.this, BadgeIntentService.class).putExtra("badgeCount", badgeCount) ); } });
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home_page, container, false); unbinder = ButterKnife.bind(this, rootView); lifeIndices = new ArrayList<>(); lifeIndexAdapter = new LifeIndexAdapter(getActivity(), lifeIndices); lifeIndexAdapter.setOnItemClickListener((adapterView, view, i, l) -> Toast.makeText(HomePageFragment.this.getContext(), lifeIndices.get(i).getDetails(), Toast.LENGTH_LONG).show()); lifeIndexRecyclerView.setItemAnimator(new DefaultItemAnimator()); lifeIndexRecyclerView.setAdapter(lifeIndexAdapter); aqiTextView.setText(String.valueOf(currentIndicatorValue)); if (TextUtils.isEmpty(weather.getAirQualityLive().getQuality())) { qualityTextView.setText(stateDescription); } else { qualityTextView.setText(weather.getAirQualityLive().getQuality()); aqiTextView.setTextColor(indicatorTextColor);
Toast toast = Toast.makeText(this, "OCR failed. Please try again.", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 0); toast.show(); return false; statusViewBottom.setVisibility(View.GONE); statusViewTop.setVisibility(View.GONE); cameraButtonView.setVisibility(View.GONE); viewfinderView.setVisibility(View.GONE); resultView.setVisibility(View.VISIBLE); bitmapImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); } else { bitmapImageView.setImageBitmap(lastBitmap); sourceLanguageTextView.setText(sourceLanguageReadable); TextView ocrResultTextView = (TextView) findViewById(R.id.ocr_result_text_view); ocrResultTextView.setText(ocrResult.getText()); translationTextView.setVisibility(View.GONE); progressView.setVisibility(View.VISIBLE); setProgressBarVisibility(true);
@Override public void onClick(View v) { switch (v.getId()) { case R.id.favour: Toast.makeText(context, "赞+1", Toast.LENGTH_SHORT).show(); if (window != null) window.dismiss(); break; case R.id.comment: View editView = mInflater.inflate(R.layout.replay_input, null); editWindow = new PopupWindow(editView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); editWindow.setOutsideTouchable(true); editWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); EditText replyEdit = (EditText) editView.findViewById(R.id.reply); replyEdit.setFocusable(true); replyEdit.requestFocus();
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tab, container, false); marqueeView = view.findViewById(R.id.marqueeView); marqueeView1 = view.findViewById(R.id.marqueeView1); marqueeView2 = view.findViewById(R.id.marqueeView2); marqueeView3 = view.findViewById(R.id.marqueeView3); marqueeView4 = view.findViewById(R.id.marqueeView4); List<CharSequence> list = new ArrayList<>(); SpannableString ss1 = new SpannableString("1、MarqueeView开源项目"); ss1.setSpan(new ForegroundColorSpan(Color.RED), 2, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); list.add(ss1); SpannableString ss2 = new SpannableString("2、GitHub:sfsheng0322"); ss2.setSpan(new ForegroundColorSpan(Color.GREEN), 9, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); list.add(ss2); SpannableString ss3 = new SpannableString("3、个人博客:sunfusheng.com"); ss3.setSpan(new URLSpan("http://sunfusheng.com/"), 7, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); list.add(ss3); list.add("4、新浪微博:@孙福生微博"); marqueeView.startWithList(list); marqueeView.setOnItemClickListener((position, textView) -> Toast.makeText(getContext(), textView.getText() + "", Toast.LENGTH_SHORT).show()); marqueeView1.startWithText(getString(R.string.marquee_texts), R.anim.anim_top_in, R.anim.anim_bottom_out); marqueeView1.setOnItemClickListener((position, textView) -> Toast.makeText(getContext(), String.valueOf(marqueeView1.getPosition()) + ". " + textView.getText(), Toast.LENGTH_SHORT).show()); marqueeView2.startWithText(getString(R.string.marquee_text)); marqueeView3.startWithText(getString(R.string.marquee_texts)); marqueeView4.startWithText(getString(R.string.marquee_texts)); return view; }
/** * 显示自定Toast * @param context * @param msg */ public static void showToastMsgShort(Context context, String msg) { Toast toast = new Toast(context); LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.toast_view, null); TextView tv = (TextView) view.findViewById(R.id.toast_msg); tv.setText(msg); TextPaint tp = tv.getPaint(); tp.setFakeBoldText(true); toast.setView(view); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } /**
public void run() { if (cancel && toast != null) { toast.cancel(); toastLayout = ((LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_toast, null); toastTextView = (TextView) toastLayout.findViewById(R.id.toasttext); if (toast == null) { toast = new Toast(context); toastDefaultYOffset = toast.getYOffset(); toast.setView(toastLayout); toast.setDuration(Toast.LENGTH_LONG); break; case 7000: toast.setDuration(Toast.LENGTH_LONG); break; default: toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, toastYOffset); break; case "bottom": toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, toastYOffset); break; case "center": toast.setGravity(Gravity.CENTER, 0, 0); break; default: