2

I have as a small project to create an array of Name, Surname and Email which I have created using xml in the value

This is my xml named Name with a String named names

<resources>
    <string name="names">
        <item>Ivan</item>
        <item>Santiago</item>                    
        .....
    </string>
</resources>

This is my XML named Surnames with a String called surnames

<resources>
    <string name="surnames">
        <item>Rodríguez</item>
        <item>Gómez</item>
        <item>López</item>
        .....
    </string>
</resources>

enter image description here

public class Registro extends AppCompatActivity implements View.OnClickListener {

    private TextView Nombre,Apellido,Correo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);
        //Seteamos las Cajas de Texto
        Nombre = (TextView) findViewById(R.id.txtNombre);
        Apellido = (TextView) findViewById(R.id.txtApellido);
        Correo = (TextView) findViewById(R.id.txtCorreo);
        //Ponemos en Modo de Escucha al Boton
        findViewById(R.id.btnGenerar).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }
}

I want to randomly select names from the XML arrays and display them in a text view.

3
  • 7
    Sounds great. Do you have a problem or question? Commented Jul 14, 2017 at 15:26
  • <string name="nomes"> Commented Jul 14, 2017 at 15:27
  • @CzarMatt E created 2 XML one named 'Name' and another 'Surname' I want that by means of a RANDOM, the GENERAR button I autocomplete the text boxes, which are shown in the image Commented Jul 14, 2017 at 15:28

2 Answers 2

7

Put your names in a string array:

<resources>
    <string-array name="nomes">
        <item>Ivan</item>
        ...
    </string-array>
    ...
</resources>

Then use Random to get a random name from the array:

private String[] names;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    names = getResources().getStringArray(R.array.nomes);
}

@Override
public void onClick(View v) {
    int randomIndex = new Random().nextInt(names.length);
    String randomName = names[randomIndex];
    yourTextView.setText(randomName);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks, but I'm new to ANDROID and I do not know where to put the code. You can put it next to my code, please
Zorry and how do I put it in my text box?
My god, I get error: c Unfortunately, youApp has stopped
What error did you get? To display text in a TextView use setText(). E.g. Nombre.setText(randomName)
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
|
0

You can try something like this:

String[] array = context.getResources().getStringArray(R.array.your_array);
String randomString = array[new Random().nextInt(array.length)];

Hope this helps

1 Comment

Hi, thanks, but I'm new to ANDROID and I do not know where to put the code. You can put it next to my code, please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.