Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Custom toast without using XML

Posted on November 14, 2022

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.

READ  [ANSWERED] kotlin - Bottom Nav Android Jetpack Compose issue
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress