0
I’m studying Java, and Android development, and I want to make this application to understand how it works.
I’m using the Toast class so that when I click on the image, a sentence appears, I can make it work with numbers, but instead I want to put some sentences and when I click on the image, the phrase appears for a few seconds on the screen.
How do I do this without many changes in this code I already have?
package android.tutorial.android;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// public void mostrarMensagem(View view) {
//Toast toast = Toast.makeText(this.sorteia(), Toast.LENGTH_SHORT);
//toast.show();
//}
public void mostrarMensagem(View view) {
int[] lista = new int[]
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
StringBuilder builder = new StringBuilder();
for(int i : lista)
{
builder.append("" + i + " ");
}
Toast.makeText(this, builder, Toast.LENGTH_LONG).show();
}
}
I don’t understand why array of phrases, because if you want to display only one sentence just change the variable
builder
within theToast
by a string. That’s it?– Paulo Rodrigues
@Paulorodrigues What I want is for each click to appear a different message!
– Paulo Roberto
@Paulorodrigues with this code below, if I register 5 messages in the array and give a single tap on the screen will appear the 5 mesnagens, one after the other and that’s not what I want. 1 tap, 1 message. String [] arr = new String []{"sentence one", "sentence two"}; for(String str : arr) Toast.makeText(this, str, Toast.LENGTH_LONG). show(); }
– Paulo Roberto