-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity_Setting.java
152 lines (130 loc) · 6.24 KB
/
Activity_Setting.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.ArashTorDev.tablo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.skydoves.elasticviews.ElasticButton;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import static android.media.CamcorderProfile.get;
import static com.ArashTorDev.tablo.R.string.listOfLanguages_EN;
public class Activity_Setting extends AppCompatActivity {
List<String> languageList, countryList;
ListView language_listView, country_listView;
ArrayAdapter<String> countryListAdapter, languagesListAdapter;
ElasticButton btn_selectLanguage, btn_selectCountry;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
// initialized variables
// preferences = PreferenceManager.getDefaultSharedPreferences(this);
//
// setLanguage();
languageList = new ArrayList<>();
countryList = new ArrayList<>();
btn_selectLanguage = findViewById(R.id.btn_selectLanguage_settingActivity);
btn_selectCountry = findViewById(R.id.btn_selectCountry_settingActivity);
// initialize list
initializeLists();
String defaultLang = Locale.getDefault().getDisplayLanguage();
Toast.makeText(this, defaultLang, Toast.LENGTH_SHORT).show();
// initialize and setting countryListAdapter for list view
countryListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, countryList);
languagesListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, languageList);
}
private void setLanguage() {
String selectedLanguage = preferences.getString("current_language","en");
setLocale(selectedLanguage);
}
public void initializeLists() {
// initialize list of countries
countryList.add(getResources().getString(R.string.listOfCountries_Iran));
countryList.add(getResources().getString(R.string.listOfCountries_Germany));
countryList.add(getResources().getString(R.string.listOfCountries_UnitedArabEmirates));
countryList.add(getResources().getString(R.string.listOfCountries_Netherlands));
countryList.add(getResources().getString(R.string.listOfCountries_France));
countryList.add(getResources().getString(R.string.listOfCountries_Turkey));
//initialize list of languages
languageList.add(getResources().getString(listOfLanguages_EN));
languageList.add(getResources().getString(R.string.listOfLanguages_PER));
}
public void selectLanguage(View view) {
// initialize listView for language list
language_listView = new ListView(this);
language_listView.setAdapter(languagesListAdapter);
language_listView.setBackground(getResources().getDrawable(R.drawable.list_view_background_transparent));
// create Alert dialog_chooseFromCameraOrGallery
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setView(language_listView, 70, 500, 70, 500);
dialog.show();
// set on item click listener for language listView
language_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selectedLang = languageList.get(i);
Toast.makeText(Activity_Setting.this, selectedLang, Toast.LENGTH_LONG).show();
btn_selectLanguage.setText(selectedLang);
switch (i) {
case 0:
setLocale("en");
break;
case 1:
setLocale("fa");
break;
}
dialog.dismiss();
}
});
}
public void selectCountry(View view) {
// initialize listView for language list
country_listView = new ListView(this);
country_listView.setAdapter(countryListAdapter);
country_listView.setBackground(getResources().getDrawable(R.drawable.list_view_background_transparent));
// create Alert dialog_chooseFromCameraOrGallery
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setView(country_listView, 70, 500, 70, 500);
dialog.show();
// set on item click listener for language listView
country_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
btn_selectCountry.setText(countryList.get(i));
dialog.dismiss();
}
});
}
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, Activity_Setting.class);
finish();
startActivity(refresh);
}
public void okButton(View view) {
onBackPressed();
}
}