z.k z.k - 3 months ago 15
Android Question

Display string variables in a Toast

I wanna display the information of 7

EditText
in a
Toast
so I put the edit texts information in 7 string variables but I don't know how.
Here is what I written:

public class MainActivity extends Activity {
EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
EditText ed5;
EditText ed6;
EditText ed7;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
showinfo();
}

private void showinfo() {
ed1= (EditText) findViewById(R.id.editText1);
ed2= (EditText) findViewById(R.id.editText2);
ed3= (EditText) findViewById(R.id.editText3);
ed4= (EditText) findViewById(R.id.editText4);
ed5= (EditText) findViewById(R.id.editText5);
ed6= (EditText) findViewById(R.id.editText6);
ed7= (EditText) findViewById(R.id.editText7);
btn= (Button) findViewById(R.id.button1);
///////
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String str1 = ed1.getText().toString();
String str2 = ed2.getText().toString();
String str3 = ed3.getText().toString();
String str4 = ed4.getText().toString();
String str5 = ed5.getText().toString();
String str6 = ed6.getText().toString();
String str7 = ed7.getText().toString();
////////////
Toast.makeText(getApplicationContext(), ,Toast.LENGTH_LONG).show();
}
});
}


How can I display all the string variables in
Toast
?

Answer

If you want to show all content of EditText in one Toast, then just use concatenation

String concatenatedText = str1 + str2 + str3 + ... 

or StringBuilder class:

StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
sb.append(str3);
String concatenatedText = sb.toString();

and simply pass the result as second argument, as below:

Toast toast = Toast.makeText(context, concatenatedText, duration);
Comments