Guest User

Untitled

a guest
Jul 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package com.example.anthony.helloandroid;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.res.Configuration;
  8. import android.content.res.Resources;
  9. //import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.RadioButton;
  14.  
  15. import java.util.HashMap;
  16. import java.util.Locale;
  17. import java.util.Map;
  18.  
  19. public class MainActivity extends Activity {
  20. public static final String EXTRA_GREETING = "com.example.anthony.Greet";
  21.  
  22. public static final Map<RadioButton, Locale> localeMap = new HashMap<>();
  23.  
  24. private Resources resources;
  25. private Configuration configuration;
  26.  
  27. private RadioButton rbEnglish;
  28. private RadioButton rbFrench;
  29. private RadioButton rbSpanish;
  30.  
  31. private AlertDialog.Builder noNameDialogBuilder;
  32. private EditText txtName;
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38.  
  39. resources = getResources();
  40. configuration = resources.getConfiguration();
  41.  
  42. rbEnglish = findViewById(R.id.rbEnglish);
  43. rbFrench = findViewById(R.id.rbFrench);
  44. rbSpanish = findViewById(R.id.rbSpanish);
  45.  
  46. txtName = findViewById(R.id.txtName);
  47.  
  48. noNameDialogBuilder = new AlertDialog.Builder(this)
  49. .setMessage("Please enter your name.")
  50. .setNeutralButton("OK",
  51. (DialogInterface dialog, int which) -> dialog.dismiss());
  52.  
  53.  
  54. fillLocaleMap();
  55. }
  56.  
  57. public void helloButtonClick(View view){
  58. String name = txtName.getText().toString();
  59. if(name.equals("")){
  60. noNameDialogBuilder.show();
  61. }
  62. else{
  63. greet();
  64. }
  65. }
  66.  
  67. public void greet(){
  68.  
  69. String name = txtName.getText().toString();
  70. String greeting = generateGreeting(name);
  71.  
  72. Intent intent = new Intent(this, GreetActivity.class);
  73. intent.putExtra(EXTRA_GREETING, greeting);
  74.  
  75. startActivity(intent);
  76. }
  77.  
  78. private String generateGreeting(String name){
  79. String greeting = getString(R.string.greeting);
  80. greeting += " " + name + "!";
  81. return greeting;
  82. }
  83.  
  84. private void fillLocaleMap(){
  85. localeMap.put(rbEnglish, Locale.ENGLISH);
  86. localeMap.put(rbFrench, Locale.FRENCH);
  87. localeMap.put(rbSpanish, new Locale("es"));
  88. }
  89.  
  90. public void setLocale(View view){
  91. RadioButton rbView = (RadioButton) view;
  92. Locale locale = localeMap.get(rbView);
  93. configuration.setLocale(locale);
  94. resources.updateConfiguration(configuration, resources.getDisplayMetrics());
  95. recreate();
  96. }
  97. }
  98.  
  99. package com.example.anthony.helloandroid;
  100.  
  101. import android.content.Intent;
  102. import android.support.v7.app.AppCompatActivity;
  103. import android.os.Bundle;
  104. import android.widget.TextView;
  105.  
  106. public class GreetActivity extends AppCompatActivity {
  107.  
  108. @Override
  109. protected void onCreate(Bundle savedInstanceState) {
  110. super.onCreate(savedInstanceState);
  111. setContentView(R.layout.activity_greet);
  112.  
  113. // Get the Intent that started this activity and extract the string
  114. Intent intent = getIntent();
  115. String message = intent.getStringExtra(MainActivity.EXTRA_GREETING);
  116.  
  117. // Capture the layout's TextView and set the string as its text
  118. TextView textView = findViewById(R.id.txtGreeting);
  119. textView.setText(message);
  120. }
  121. }
Add Comment
Please, Sign In to add comment