Solution 1 :
I don’t use app inventor and it’s been a long time, but here is a solution using only java, in case someone still needs it.
public void createToast(AndroidViewComponent component,int gravity1,int gravity2){
View content = component.getView();
try{
//declare content
Toast toast = new Toast(context);
LinearLayout toast_layout = new LinearLayout(context);
TextView toast_msg = new TextView(context);
//add rounded border to toast
android.graphics.drawable.GradientDrawable toast_borders = new android.graphics.drawable.GradientDrawable();
//background color of toast
toast_borders.setColor(Color.parseColor("#434343"));
//round size
toast_borders.setCornerRadius(20);
//add shadow
toast_layout.setElevation(15);
toast_layout.setBackground(toast_borders);
//set layout type
toast_layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
toast_layout.setOrientation(LinearLayout.VERTICAL);
//layout padding
toast_layout.setPadding(15, 15, 16, 15);
//text size
toast_msg.setTextSize(15);
//text padding
toast_msg.setPadding(15, 15, 16, 15);
//text color
toast_msg.setTextColor(Color.parseColor("#ff0000"));
//set text
toast_msg.setText("Your Message!");
//add your text to the toast layout
toast_layout.addView(toast_msg);
//set the layout to your toast
toast.setView(toast_layout);
//set toast duration
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(gravity1 | gravity2, 32, 32);
toast.show();
if(autoInvisible == true){
content.setVisibility(View.GONE);}
}catch (Exception e){e.printStackTrace();}
}
Problem :
I am creating a extension for app inventor for custom toast in which I am only allowed to use java but in the android it can be done using Inflater. Is there any way to create custom toast with only Java.
Here is what I have did
public void createToast(AndroidViewComponent component,int gravity1,int gravity2){
toast = new Toast(context);
View content = component.getView();
try{
toast.setView(content);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(gravity1 | gravity2, 32, 32);
toast.show();
if(autoInvisible == true){
content.setVisibility(View.GONE);}
}catch (Exception e){e.printStackTrace();}
}
Comments
Comment posted by github.com/GrenderG/Toasty
I use Toasty for beautiful toasts.Here’s the link:
Comment posted by Devil TM
Thanks for providing me information but I have to create without using any library
Comment posted by developer.android.com/preview/features/toasts?hl=es-419
If you want to avoid problems, Android blocks the custom Toast if use the setView method.