PRÁTICA 8 – STRINGS XML
Objetivo:
Utilizar o arquivo strings.xml no diretório RES para gerenciar traduções e outras strings
Introdução
Dentro do diretório RES do seu aplicativo há um arquivo xml para todas os textos fixos mostrados, o que simplifica a tradução. Há também um arquivo para cores, estilos e dimensões.
Tarefa:
Usar strings.xml ao invés de hard-codded strings:
Programa 1 – Exemplo de Strings.xml:
1 2 3 4 5 6 7 8 9 10 |
<resources> <string name="app_name">Empty</string> <string name="red">RED</string> <string name="orange">ORANGE</string> <string name="yellow">YELLOW</string> <string name="green">GREEN</string> <string name="blue">BLUE</string> <string name="indigo">INDIGO</string> <string name="violet">VIOLET</string> </resources> |
Programa 1 – Exemplo de layout que pode ser traduzido:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TextView android:text="@string/red" android:id="@+id/TextView01" android:layout_height="wrap_content" android:background="#f00" android:gravity="center" android:textColor="#000" android:layout_width="wrap_content" android:padding="25dp"></TextView> <TextView android:text="@string/orange" android:layout_height="wrap_content" android:background="#ffa500" android:gravity="center" android:textColor="#000" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:padding="25dp"></TextView> <TextView android:text="@string/yellow" android:layout_height="wrap_content" android:background="#ffff00" android:gravity="center" android:textColor="#000" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:padding="25dp"></TextView> <TextView android:text="@string/green" android:layout_height="wrap_content" android:background="#0f0" android:gravity="center" android:textColor="#000" android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/TextView05" android:padding="25dp" android:layout_centerVertical="true"></TextView> <TextView android:text="@string/blue" android:layout_height="wrap_content" android:background="#00f" android:gravity="center" android:textColor="#fff" android:id="@+id/TextView05" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:padding="25dp"></TextView> <TextView android:text="@string/indigo" android:layout_height="wrap_content" android:gravity="center" android:textColor="#fff" android:id="@+id/TextView06" android:layout_width="wrap_content" android:layout_toRightOf="@+id/TextView05" android:background="#4b0082" android:padding="25dp" android:layout_centerVertical="true"></TextView> <TextView android:text="@string/violet" android:layout_height="wrap_content" android:background="#ee82ee" android:gravity="center" android:textColor="#000" android:id="@+id/TextView07" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:padding="25dp"></TextView> </RelativeLayout> |
Programa 1 – Crie um novo locale em “open editor” e depois “add Locale”. Adicione portuguese (pt) e coloque os dados abaixo.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Empty</string> <string name="red">VERMELHO</string> <string name="orange">LARANJA</string> <string name="yellow">AMARELO</string> <string name="green">VERDE</string> <string name="blue">AZUL</string> <string name="indigo">INDIGO</string> <string name="violet">VIOLETA</string> </resources> |
Programa 2 – LayOut pronto para ser traduzido:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="empty.ammsoft.empty.MainActivity" android:id="@+id/layout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/equa_o_de_segundo_grau" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="a" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/a" android:layout_weight="1" android:inputType="number|numberSigned" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="b" android:id="@+id/textView3" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b" android:layout_weight="1" android:inputType="number|numberSigned" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="c" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/c" android:layout_weight="1" android:inputType="number|numberSigned" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X1=" android:id="@+id/x1" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="X2=" android:id="@+id/x2" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/resolver" android:id="@+id/button2" android:layout_weight="1" android:layout_gravity="right" android:clickable="true" android:onClick="calculaSegundoGrau" /> </LinearLayout> |
Programa 2 – Usando strings XML dentro do programa:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void calculaSegundoGrau(View v) { EditText a_edit = (EditText) findViewById(R.id.a); EditText b_edit = (EditText) findViewById(R.id.b); EditText c_edit = (EditText) findViewById(R.id.c); TextView x1_text = (TextView) findViewById(R.id.x1); TextView x2_text = (TextView) findViewById(R.id.x2); double a = Double.parseDouble(a_edit.getText().toString()); double b = Double.parseDouble(b_edit.getText().toString()); double c = Double.parseDouble(c_edit.getText().toString()); double delta = b * b - 4*a*c; double x1 = -b + Math.sqrt(delta); double x2 = -b - Math.sqrt(delta); x1_text.setText(getResources().getString(R.string.raiz_x1) + " " + String.valueOf(x1)); x2_text.setText(getResources().getString(R.string.raiz_x2) + " " + String.valueOf(x2)); } } |
Programa 2 – Strings.xml com o texto
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Equação do Segundo Grau</string> <string name="eq_de_segundo_grau">Equação de Segundo Grau</string> <string name="resolver">Resolver</string> <string name="raiz_x1">Raiz X1 =</string> <string name="raiz_x2">Raiz X2 =</string> </resources> |
Desafio: Adicione mensagem de erro indicando que está faltando algum dado ou quando DELTA for menor que zero. Traduza para o inglês e português:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void calculaSegundoGrau(View v) { EditText a_edit = (EditText) findViewById(R.id.a); EditText b_edit = (EditText) findViewById(R.id.b); EditText c_edit = (EditText) findViewById(R.id.c); TextView x1_text = (TextView) findViewById(R.id.x1); TextView x2_text = (TextView) findViewById(R.id.x2); x1_text.setText(getResources().getString(R.string.raiz_x1)); x2_text.setText(getResources().getString(R.string.raiz_x2)); if (a_edit.getText().length() == 0) { Toast.makeText(this, getResources().getString(R.string.noA),Toast.LENGTH_SHORT).show(); return; } if (b_edit.getText().length() == 0) { Toast.makeText(this, getResources().getString(R.string.noB),Toast.LENGTH_SHORT).show(); return; } if (c_edit.getText().length() == 0) { Toast.makeText(this, getResources().getString(R.string.noC),Toast.LENGTH_SHORT).show(); return; } double a = Double.parseDouble(a_edit.getText().toString()); double b = Double.parseDouble(b_edit.getText().toString()); double c = Double.parseDouble(c_edit.getText().toString()); double delta = b * b - 4*a*c; if (delta < 0) { Toast.makeText(this, getResources().getString(R.string.noReal),Toast.LENGTH_SHORT).show(); return; } double x1 = -b + Math.sqrt(delta); double x2 = -b - Math.sqrt(delta); x1_text.setText(getResources().getString(R.string.raiz_x1) + String.valueOf(x1)); x2_text.setText(getResources().getString(R.string.raiz_x1) + String.valueOf(x2)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Second Degree Equation</string> <string name="eq_de_segundo_grau">Second Degree Equation</string> <string name="resolver">Solve</string> <string name="raiz_x1">Root X1 =</string> <string name="raiz_x2">Root X2 =</string> <string name="noA">No value on a!</string> <string name="noB">No value on b!</string> <string name="noC">No value on c!</string> <string name="noReal">Delta lower than ZERO!</string> </resources> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Equação do Segundo Grau</string> <string name="eq_de_segundo_grau">Equação de Segundo Grau</string> <string name="resolver">Resolver</string> <string name="raiz_x1">Raiz X1 =</string> <string name="raiz_x2">Raiz X2 =</string> <string name="noA">Esqueceu o valor de a!</string> <string name="noB">Esqueceu o valor de b!</string> <string name="noC">Esqueceu o valor de c!</string> <string name="noReal">Delta menor que ZERO!</string> </resources> |