Tuesday, July 12, 2011

How to show toast?

Simple toast:


Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

Customized toast:


LayoutInflater inflater;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.detail_toast,
(ViewGroup)arg0.findViewById(R.id.toast_layout_root));
TextView taskTextView = (TextView)
layout.findViewById(R.id.taskTextView);
taskTextView.setText(task.getName());
TextView detailTextView = (TextView)
layout.findViewById(R.id.detailTextView);
detailTextView.setText(sb.toString());
Toast toast = new Toast(context.getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

No comments:

Post a Comment