Change text and icon in a Custom Toast?

左心房为你撑大大i 提交于 2019-12-05 06:41:53

问题


I'm having some issues when I trying to show a custom toast (with icon and message). This is the code.

private void makeToast(String text, int icon) {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message));
        ((TextView) layout.findViewById(R.id.message)).setText(text);

        layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon));

        // 0 - Exclamation, 1 - Noow icon
        if(icon == 0){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.exclamation);
        }else if(icon == 1){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.nicon);
        }


        Toast toast = new Toast(getBaseContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

I want to change the icon and message when the second parameter is 0 or 1. I know the problem is in the call to the second layout, but I don't know how to solve it.

Thanks you.

Edit:

I call this method from a onPostExecute in a AsyncTask. I forgot to tell this.

Thread [<11> AsyncTask #1] (Suspended (exception RuntimeException)) 
    ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: not available 
    ThreadPoolExecutor$Worker.run() line: not available 
    Thread.run() line: not available    

Edit 2:

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    makeToast("loading nightclubs...", 1);
    }
}

Edit 3:

@Override
        protected void onPostExecute(List<Category> result) {
            Log.i("result", result.toString());

            // Cargamos el listado a pasarle a categoryId en la
            // consulta
            for (int k = 0; k < result.size(); k++) {
                ALLOFTHEM += result.get(k).getId();
                if (k < result.size() - 1) {
                    ALLOFTHEM += ",";
                }
            }

            Log.i("POST", ALLOFTHEM);
        }

SOLVED!!!

I have used...

runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

...everytime I want a toast in a UI and it works good.

Thanks to all of you.


回答1:


My solution for custom Toast :

public class MyToast {
    public static void show(Context context, String text, boolean isLong) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.toast_layout, null);

        ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
        image.setImageResource(R.drawable.ic_launcher);

        TextView textV = (TextView) layout.findViewById(R.id.toast_text);
        textV.setText(text);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration((isLong) ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

And toast_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/toast_border"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/toast_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/app_name"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textColor="#FFF" />

</LinearLayout>

And toast_border.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="2dp"
        android:color="#ee777777" />

    <solid android:color="#ee444444" />

    <padding
        android:bottom="2dp"
        android:left="20dp"
        android:right="20dp"
        android:top="2dp" />

    <corners android:radius="5dp" />
</shape>

Result in image :

I hope I have helped you!




回答2:


You inflate your layout two times, and set text in the first one, which is discarded:

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message));
((TextView) layout.findViewById(R.id.message)).setText(text);

// you discard the previously inflated layout, for which you have set text
layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon));

What you want probably is something like this:

View layout = inflater.inflate(R.layout.custom_toast, null);
((TextView) layout.findViewById(R.id.message)).setText(text);

and then your code to set the icon.




回答3:


use this way:

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast_layout, null);

        TextView text = (TextView) layout.findViewById(R.id.toast_message);
        ImageView img = (ImageView) layout.findViewById(R.id.imageView1);

        if (icon == 0) {
            text.setText("icon 0");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        }

        if (icon == 1) {
            text.setText("icon 1");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_one));
        }

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();



回答4:


change your onPostExecute() method to

protected void onPostExecute(String file_url) {
            // anything else; you want to do here

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

        }


来源:https://stackoverflow.com/questions/16788827/change-text-and-icon-in-a-custom-toast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!