2

I'm trying to create a multilingual app, but my home page is a gridView made of images and textViews. When I created a list in Java (like this):

String textoList[] = {"Visto", "Alojamento", "C.P.F.", "Alimentação", "Carteirinha", "Guia", "Registro", "Informações", "Assistência", "Biblioteca", "Calendário", "Transporte", "INT", "Intérprete", "F.A.Q."};

it worked very well. But I realized that I shouldn't create a list in Java, but use an array that is already create in the strings.xml file so that way my app can be translated. Basically, I want the texts from this array. How can I do this?

Can someone give me a hand, please? If someone would like to test it, the complete code is:

    package br.com.unbparaestrangeiros.unbparaestrangeiros;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivity extends Activity{
    GridView gridView;

    Resources r = getResources();
    String[] textoList = r.getStringArray(R.array.itens);

    //String textoList[] = {"Visto", "Alojamento", "C.P.F.", "Alimentação", "Carteirinha", "Guia", "Registro", "Informações", "Assistência", "Biblioteca", "Calendário", "Transporte", "INT", "Intérprete", "F.A.Q."};

    int textoImagem[] = {R.drawable.visto, R.drawable.alojamento, R.drawable.cpf, R.drawable.alimentacao, R.drawable.carteirinha, R.drawable.guia,  R.drawable.registro, R.drawable.informacoes, R.drawable.assistencia,  R.drawable.biblioteca, R.drawable.calendario, R.drawable.transporte,  R.drawable.informacoes, R.drawable.interprete, R.drawable.perguntas};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inicio);

        gridView = (GridView) findViewById(R.id.gridView);

        GridAdapter adapter = new GridAdapter(MainActivity.this, textoImagem, textoList);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent case0 = new Intent(MainActivity.this, Visto.class);
                        startActivity(case0);
                        break;

                    case 1:
                        Intent case1 = new Intent(MainActivity.this, Alojamento.class);
                        startActivity(case1);
                        break;

                    case 2:
                        Intent case2 = new Intent(MainActivity.this, CPF.class);
                        startActivity(case2);
                        break;

                    case 3:
                        Intent case3 = new Intent(MainActivity.this, Alimentacao.class);
                        startActivity(case3);
                        break;

                    case 4:
                        Intent case4 = new Intent(MainActivity.this, Carteirinha.class);
                        startActivity(case4);
                        break;

                    case 5:
                        Intent case5 = new Intent(MainActivity.this, Guia.class);
                        startActivity(case5);
                        break;

                    case 6:
                        Intent case6 = new Intent(MainActivity.this, Registro.class);
                        startActivity(case6);
                        break;

                    case 7:
                        Intent case7 = new Intent(MainActivity.this, Informacoes.class);
                        startActivity(case7);
                        break;

                    case 8:
                        Intent case8 = new Intent(MainActivity.this, Assistencia.class);
                        startActivity(case8);
                        break;

                    case 9:
                        Intent case9 = new Intent(MainActivity.this, Biblioteca.class);
                        startActivity(case9);
                        break;

                    case 10:
                        Intent case10 = new Intent(MainActivity.this, Calendario.class);
                        startActivity(case10);
                        break;

                    case 11:
                        Intent case11 = new Intent(MainActivity.this, Transporte.class);
                        startActivity(case11);
                        break;

                    case 12:
                        Intent case12 = new Intent(MainActivity.this, Assessoria.class);
                        startActivity(case12);
                        break;

                    case 13:
                        Intent case13 = new Intent(MainActivity.this, Interprete.class);
                        startActivity(case13);
                        break;

                    case 14:
                        Intent case14 = new Intent(MainActivity.this, Perguntas.class);
                        startActivity(case14);
                        break;

                }
            }
        });
    }

}
3
  • 1
    String[] textoList[] = getResources().getStringArray(R.array.itens); from strings.xml in oncreate. Note you have String[] textoList = r.getStringArray(R.array.itens); . getResources() needs a context. Context is available once activity is created. Move your code inside onCreate Commented Apr 23, 2017 at 18:37
  • what is the error Commented Apr 23, 2017 at 18:39
  • "But I realized that I shouldn't create a list in Java, but use an array that is already create in the strings.xml file" - String[] textoList is not a List, you are already using an array of strings obtained directly from the resources. Commented Apr 23, 2017 at 18:43

2 Answers 2

1

Create a new file if not already present called array.xml in app/src/main/res/values. You can add your strings like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="itens">
        <item>Visto</item>
        <item>Alojamento</item>
        <!--Add your other words here-->
    </string-array>
</resources>

And you can access them using getResources().getStringArray(R.array.itens); from your Activity.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

String[] textoList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inicio);

    textoList = getResources().getStringArray(R.array.itens);
}

And your itens array in strings.xml as below:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    ...............
    ......................
    <string-array name="itens">
        <item>Visto</item>
        <item>Alojamento</item>
        <item>C.P.F.</item>
        <item>Alimentação</item>
        <item>Carteirinha</item>
        <item>Guia</item>
        <item>Registro</item>
        ................
        .......................
    </string-array>
</resources>

Hope this will help~

1 Comment

If my answer seems useful then you can mark it as right answer and vote it up. Thanks in advance :)

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.