Search code examples
androidtoast

How to create a custom toast


I'm making a custom toast, so I've a xml file for custom toast layout named ctoast_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
 
     <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/checkbox_on_background" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />



</LinearLayout>

the main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="133dp"
        android:text="Button" />
 

</RelativeLayout>
 

and the CToast main class

package com.example.customtoast;



import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class CToast extends Activity{


	private Context mContext;
	private Button mButton;
	private LinearLayout cToastView;
	private TextView toastTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);


		setContentView(R.layout.main);

		mContext = this;

		toastTextView = (TextView) findViewById(R.id.textView1);
		mButton = (Button) findViewById(R.id.button1);





		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				showToast();
			}


		});



	}


	private void showToast() {
		// TODO Auto-generated method stub


		Toast mToast = new Toast(getApplicationContext());
		mToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toastTextView.setText("message!!");
		mToast.setView(getLayoutInflater().inflate(R.layout.ctoast_view,null));
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.show();



	}


}

Now I got a NullPointerException on this line

toastTextView.setText("message!!"); 

I think because I can't reference to a View in a different layout file set by setContentView. How can I dinamically set text of my custom toast layout?


Solution

  • You need to do it this way:

    private void showToast() {
        View view = getLayoutInflater().inflate(R.layout.ctoast_view,null);
        toastTextView = (TextView) view.findViewById(R.id.textView1);
        toastTextView.setText("message!!");
    
        Toast mToast = new Toast(getApplicationContext());
        mToast.setView(view);
        mToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.show();
    }
    

    Inflate the toast content view and get the TextView from it when you create the toast.