Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.anthony.helloandroid;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- //import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import java.util.HashMap;
- import java.util.Locale;
- import java.util.Map;
- public class MainActivity extends Activity {
- public static final String EXTRA_GREETING = "com.example.anthony.Greet";
- public static final Map<RadioButton, Locale> localeMap = new HashMap<>();
- private Resources resources;
- private Configuration configuration;
- private RadioButton rbEnglish;
- private RadioButton rbFrench;
- private RadioButton rbSpanish;
- private AlertDialog.Builder noNameDialogBuilder;
- private EditText txtName;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- resources = getResources();
- configuration = resources.getConfiguration();
- rbEnglish = findViewById(R.id.rbEnglish);
- rbFrench = findViewById(R.id.rbFrench);
- rbSpanish = findViewById(R.id.rbSpanish);
- txtName = findViewById(R.id.txtName);
- noNameDialogBuilder = new AlertDialog.Builder(this)
- .setMessage("Please enter your name.")
- .setNeutralButton("OK",
- (DialogInterface dialog, int which) -> dialog.dismiss());
- fillLocaleMap();
- }
- public void helloButtonClick(View view){
- String name = txtName.getText().toString();
- if(name.equals("")){
- noNameDialogBuilder.show();
- }
- else{
- greet();
- }
- }
- public void greet(){
- String name = txtName.getText().toString();
- String greeting = generateGreeting(name);
- Intent intent = new Intent(this, GreetActivity.class);
- intent.putExtra(EXTRA_GREETING, greeting);
- startActivity(intent);
- }
- private String generateGreeting(String name){
- String greeting = getString(R.string.greeting);
- greeting += " " + name + "!";
- return greeting;
- }
- private void fillLocaleMap(){
- localeMap.put(rbEnglish, Locale.ENGLISH);
- localeMap.put(rbFrench, Locale.FRENCH);
- localeMap.put(rbSpanish, new Locale("es"));
- }
- public void setLocale(View view){
- RadioButton rbView = (RadioButton) view;
- Locale locale = localeMap.get(rbView);
- configuration.setLocale(locale);
- resources.updateConfiguration(configuration, resources.getDisplayMetrics());
- recreate();
- }
- }
- package com.example.anthony.helloandroid;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class GreetActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_greet);
- // Get the Intent that started this activity and extract the string
- Intent intent = getIntent();
- String message = intent.getStringExtra(MainActivity.EXTRA_GREETING);
- // Capture the layout's TextView and set the string as its text
- TextView textView = findViewById(R.id.txtGreeting);
- textView.setText(message);
- }
- }
Add Comment
Please, Sign In to add comment