Search code examples
androidandroid-layoutandroid-studiolayouttoast

Dynamically modify layout of a toast Android


I have a trouble creating my app in Android Studio, I explain myself

I created a layout for my toast, the XML looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/elemento_correcto_s" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#afff45"
android:weightSum="1">
<ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="@drawable/base" android:padding="5dip" android:id="@+id/ok"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false">
</ImageView>
<TextView android:layout_height="50dp" android:id="@+id/tv" android:layout_width="match_parent" android:text="¡CORRECTO!" android:textColor="#FFF" android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="center_vertical"
    android:layout_toRightOf="@+id/ok"
    android:layout_marginTop="26dp"
    >
</TextView>

It works fine when I inflate the layout

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(
            R.layout.elemento_correcto_s
            , (ViewGroup) findViewById(R.id.elemento_correcto_s));
    this.elementoCorrecto = new Toast(this);
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
    this.elementoCorrecto.setView(layout);
    this.elementoCorrecto.show();

But the problem is that I want to change dynamically the text for the TextView, I already tried just calling the TextView and changing the text, but it doesn't work, so I hope you can help me

This is my code

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(
            R.layout.elemento_correcto_xl
            , (ViewGroup) findViewById(R.id.elemento_correcto_xl));

    TextView tvCombo = (TextView) findViewById(R.id.tv);
    if(combo > 1) {
        tvCombo.setText("¡" + combo + " VECES SEGUIDAS!");
    }
    else
        tvCombo.setText("¡CORRECTO!");

    this.elementoCorrecto = new Toast(this);
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
    this.elementoCorrecto.setView(layout);
    this.elementoCorrecto.show();

Solution

  • You can use this method to as i passing the string to show..

    public static void makeToast(Context c, String msgToShow){
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v= inflater.inflate(R.layout.view_toast,null); //your layout to show
        TextView text = (TextView) v.findViewById(R.id.textViewToast);
        text.setText(msgToShow);
        Toast toast = new Toast(c);
        toast.setGravity(Gravity.BOTTOM, 10, 25);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(v);
        toast.show();
    }
    

    Call it wherever you want in the Activities.