Make Custom Toast in android using another layout xml
- make layout of that which you want to display
- inflate that layout using "LayoutInflater"
- set that layout as a ToastView using setView method
Button btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.toast,(ViewGroup) findViewById(R.id.toast_layout_root));ImageView image = (ImageView) layout.findViewById(R.id.image);image.setImageResource(R.drawable.ic_launcher);TextView text = (TextView) layout.findViewById(R.id.text);text.setText("custom toast Demo!");Toast toast = new Toast(getApplicationContext());// set postion of toast where you want to displaytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);// Visible timetoast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();}});
Download Source Code
No comments:
Post a Comment