To create Custom toasted message in sketchware follow these steps
1. In your Sketchware android project, in VIEW area, create a new CustomView with name custom1. In this CustomView add a LinearH linear1 and a TextView textview1.
Customize the textview1 and linear1 as per your requirement.
2. In LOGIC area, add a new More Block with name customToast(String _text) as shown in image below.
3. Next define this customToast Block. In event customToast MoreBlock use add source directlyblock, and in the block, write codes as described below.
First define a new LayoutInflater and use it to create a View from the CustomView.
LayoutInflater inflater = getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.custom1, null);
To Initialize textview1 and linear1 used in CustomView.
TextView textview1 = (TextView) toastLayout.findViewById(R.id.textview1);
LinearLayout linear1 = (LinearLayout) toastLayout.findViewById(R.id.linear1);
Set the text of textview1 to String _text.
textview1.setText(_text);
Create a new Toast.
Toast toast = new Toast(getApplicationContext());
Set duration of Toast.
Short:
toast.setDuration(Toast.LENGTH_SHORT);
Long :
toast.setDuration(Toast.LENGTH_LONG);
Code to improve the look of toasted message using gradient drawable
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setCornerRadius(60);
gd.setStroke(2, Color.parseColor("#ff0000"));
linear1.setBackground(gd);
Set the View created from CustomView, as view of Toast.
toast.setView(toastLayout);
Show the Toast.
toast.show();
The Whole code Given above should look like this:
LayoutInflater inflater = getLayoutInflater(); View toastLayout = inflater.inflate(R.layout.custom1, null);
TextView textview1 = (TextView) toastLayout.findViewById(R.id.textview1);
textview1.setText(_text);
LinearLayout linear1 = (LinearLayout) toastLayout.findViewById(R.id.linear1);
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setCornerRadius(60);
gd.setStroke(2, Color.parseColor("#ff0000"));
linear1.setBackground(gd);
Toast toast = new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
4. Use the more Block whenever you want to display a custom Toast, as shown in image below.
5. Save and run the project.
Tags:
Sketchware