Toast toast = Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_SHORT); TextView textView = new TextView(getApplicationContext()); textView.setText("This is a custom toast message"); toast.setView(textView); toast.show();
Toast toast = new Toast(getApplicationContext()); LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout)); TextView textView = (TextView) layout.findViewById(R.id.custom_toast_text); textView.setText("This is a custom toast message"); toast.setView(layout); toast.show();This example uses a custom layout file (custom_toast.xml) to define the view for the Toast message. The TextView in the layout file is then updated with the custom message "This is a custom toast message" before it is set as the view for the Toast. This allows for even more customization of the Toast message beyond just simple text or a single TextView. The android.widget package library contains classes for creating and managing user interface elements in an Android application.