Android: How to apply a variable to a toast?

I am going to use a toast in my application for testing. I am new to Android and I am not very familiar with toasts. I know the standard toast like this: Toast.makeText(context, text, duration).show(); . However, instead of applying a line of text to the "text" section, I want to apply a variable.

Here is what I wrote:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_next); Button send = (Button) findViewById(R.id.bSend);//Import button1 (Send) send.setOnClickListener(new OnClickListener() {//Set an onClickListener for the button to work public void onClick(View v) { Toast.makeText(getApplicationContext(), cText, Toast.LENGTH_LONG).show(); }//end method });//End Send }//End onCreate 

cText is a variable used in another method present in the class. Any suggestions on how I can get a toast to contain cText content? Thanks in advance.

+4
source share
4 answers

Maybe you will try this

 public class MainActivity extends Activity { String Text="MainActivity Message"; //Global variable @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_next); Button send = (Button) findViewById(R.id.bSend);//Import button1 (Send) send.setOnClickListener(new OnClickListener() {//Set an onClickListener for the button to work public void onClick(View v) { //Declaring two variables. //You can also declare it as global. //but global variable must be initialized before creating toast otherwise you will get NPE and lead to you application crash String cText="Toast Message"; int val=1; Toast.makeText(getApplicationContext(), cText, Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "vlaue is "+val, Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), getMessage(), Toast.LENGTH_LONG).show(); } }); } public String getMessage(){ return "Text from Function"; } } 
+2
source

It seems cText is out of scope. Either define it at the top level, or as the final variable before setting onClickListener.

You should learn the basics of java about variables before moving your head to android, which will help you a lot. I can recommend the Head First Java book for this.

0
source

cText sounds like a sequence of characters or at least some text. Assuming its character sequence or string: yes, you can use it. In addition, you can customize your toast notification.

0
source

Declare cText with the scope of the class. setTextvalue () sets the string value. On Button click displayValue () to display a toast message with the value set to cText.

 public class MainActivity extends Activity { String cText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTextvalue(); Button b= (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub displayValue(); } }); } public void setTextvalue() { cText="hello"; } public void displayValue() { Toast.makeText(MainActivity.this, cText.toString(), Toast.LENGTH_LONG).show(); } } 
0
source

All Articles