android - How to create a custom toast -


I am creating a custom toast, so I have an XML file for a custom toast layout called ctoast_view.xml

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; LinearLayout xmlns: android = "Http://schemaskandroidkcom/apk/res/android" Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" android: orientation = "horizontal" & gt; & Lt; imageView android: id = "@ + id / imageView1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: src = "@ android: drawable / checkbox_on_background" / & gt; & Lt; TextView Android: id = "@ + id / textView1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: text = "" /> & Lt; / LinearLayout & gt;  

Main. Xml file

& Lt; / RelativeLayout & gt;

and CToast main square

  package com.example.customtoast; Import android App Import android.content.Context; Importroid.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 activity (personal reference mContext; private Button Mbtn; private Lainrleaut Sitostwu; private Tekstwu Tosttekstwu: @ Override protected void was rescued from the Create (bundled Instensstet) {// been Todo Auto-generated method stub super. Conet (saved the Instensstet); setContentView (R.layout.main); MContext = this; ToastTextView = (TextView) Find ViewById (R.id.textView1); MButton = (button) searching for ViewById (R.id.button1); MButton.setOnClickListener (New OnClickListener) {@Override Public Shun Click on (see V) {// TODO Automated method showing stub oopost ();}}); } Private defeats show () {// Tudo auto-generated method stub toast amitost = new toast (getApplicationContext ()); MToast.setGravity (Gravity CRNTNO, RTL, 0, 0); ToastTextView.setText ("Message !!"); MToast.setView (getLayoutInflater () increased (R.layout.ctoast_view, tap).); MToast.setDuration (Toast.LENGTH_LONG); MToast.show (); }}  

Now I found a NullPointerException on this line

  toastTextView.setText ( "message! ! ");  

I can not because I can not set a different layout file set by SetContentView in the context of a view. How do I set text for my custom toast layout?

You have to do this like this:

  () {View = getLayoutInflater (). Flowers (R.Let.Contost_view, empty); ToastTextView = (TextView) view.findViewById (R.id.textView1); ToastTextView.setText ("Message !!"); Toast mToast = new toast (getApplicationContext ()); MToast.setView (see); MToast.setGravity (Gravity CRNTNO, RTL, 0, 0); MToast.setDuration (Toast.LENGTH_LONG); MToast.show (); }  

Extend the toast content scene faster and get textView when you make toast.


Comments