0

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?

adev
  • 367
  • 1
  • 3
  • 20

2 Answers2

3

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.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

Use this android dependency :

1. Add this in your root build.gradle file (not your module build.gradle file):

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

2. Add this to your module's build.gradle file (make sure the version matches the JitPack badge above):

dependencies {
            implementation 'com.github.dev-aniketj:RoastyToasty:1.0.2'
    }

There is many method to write a custom toast :

Toasty.normal(context, "Normal Toast");
Toasty.success(context, "Success Toast");
Toasty.error(context, "Error Toast");
Toasty.warning(context, "Warning Toast");
Toasty.custom(Context context, String message, int drawable, String backgroundColor);
Aniket Jain
  • 260
  • 2
  • 20