Bonjour à tous,
Je suis en train d'essayer de modifier l'affichage de la page de paramètres d'une application (de la boite où je suis en stage). J'ai bien regardé les tutoriels, et ai tenté de les appliquer, mais étant vraiment junior en développement Java/Android je n'y parviens pas.
Cette application était prévue à la base uniquement pour les téléphones et je dois l'adapter pour les tablettes. Du coup, l'écran de paramètres qui est présenté comme une seule vue et qui ouvre d'autres vues en fonction du choix de l'utilisateur. J'aimerais que sur tablette, cette SettingsActivity soit affichée à la manière d'une Activity "master and details". J'ai bien compris que je dois orienter vers les fragments, du coup modifier le onCreate de ma classe Settings pour lui passer le setContentView après avoir sérialisé le nouveau XML de mon fragment, créé une nouvelle classe qui hériterait de Fragment, modifier sa méthode onCreateView.
J'ai tenté d'effectuer tout cela mais ça ne fonctionne pas.
Voici ma classe Settings, elle contenait initialement tout ce qu'il y a après la méthode onCreateView dans mon FragSettings.
Voici le fragment que j'ai créé et dont la méthode onCreateView est censée supplanter la onCreate de la SettingsActivity :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 import android.os.Bundle; import android.preference.PreferenceActivity; /** * Created by mma on 12/2/2014. */ public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.settings); } }
Quand j'ai repris l'application, toute la classe SettingsActivity, laquelle est visible dans ce SettingsFrag, était une classe à part :
Voici le XML de SettingsActivity :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.text.InputType; import android.text.method.NumberKeyListener; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileFilter; import java.util.Collection; import lu.visionitgroup.vsmobile.model.Page; import lu.visionitgroup.vsmobile.services.Settings; import lu.visionitgroup.vsmobile.tasks.VsMobileAppFetcher; import lu.visionitgroup.vsmobile.util.ControlActivityInstance; import lu.visionitgroup.vsmobile.util.SessionManagement; /** * Created by DWE on 11/05/2015. */ public class SettingsFrag extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.settings, container, false); } public static class SettingsActivity extends PreferenceActivity { private static final Logger log = LoggerFactory.getLogger(SettingsActivity.class); public static Context context; SessionManagement session; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); log.info("onCreate({})", savedInstanceState); ControlActivityInstance.getInstance().setSettingsActivity(this); this.addPreferencesFromResource(R.xml.preferences); session = new SessionManagement(getApplicationContext()); context = getApplicationContext(); CheckBoxPreference prf = (CheckBoxPreference) findPreference("pref_ROAMING_ALLOWED"); prf.setChecked(true); Preference connPrefScreen = findPreference("manageSigning"); //FIXME Use String Value From Stringvalue.XML if (session.isLoggedIn()) { connPrefScreen.setSummary("Vous êtes connecté"); } else { connPrefScreen.setSummary("Veuillez Vous Connecter !"); } // Set keyboard and accept only chars for sync interval ((EditTextPreference) this.findPreference(Settings.SYNCHRONIZATION_INTERVAL.getPrefKey())).getEditText().setKeyListener(new NumberKeyListener() { @Override public int getInputType() { // The following shows the standard keyboard but switches to the view with numbers on available on the top line of chars return InputType.TYPE_CLASS_NUMBER; // Return the following to show a dialpad as the one shown when entering phone numbers. // return InputType.TYPE_CLASS_PHONE } @Override protected char[] getAcceptedChars() { return "1234567890".toCharArray(); } }); final PreferenceScreen targetCategory = this.getPreferencesPages((PreferenceScreen) this.findPreference("manageServices")); targetCategory.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { return SettingsActivity.this.getPreferencesPages((PreferenceScreen) SettingsActivity.this.findPreference("manageServices")).getPreferenceCount() > 0; } }); if (VsMobile.CURRENT_APP.getAuthenticationAvailable().equalsIgnoreCase("true")) { final PreferenceScreen pref = (PreferenceScreen) findPreference("manageSigning"); pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { if (session.isLoggedIn()) { Intent i = new Intent(getApplicationContext(), LoginViewActivity.class); startActivity(i); finish(); } else { session.checkLogin(); finish(); } return false; } }); } else { PreferenceScreen screen = getPreferenceScreen(); Preference catPref = getPreferenceManager().findPreference("mySigning"); screen.removePreference(catPref); } // onClick Load all data Preference prefRefresh = this.findPreference("pref_load_all_data"); prefRefresh.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { log.info("Pref#{} has been clicked", preference.getKey()); if ("pref_load_all_data".equals(preference.getKey())) { VsMobile.refreshAppInNewActivity(SettingsActivity.this); } return true; } }); // onClick Empty cache Preference prefClear = findPreference("pref_empty_cache"); prefClear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { log.info("Pref#{} has been clicked", preference.getKey()); if ("pref_empty_cache".equals(preference.getKey())) { // FIXME do not instanciate a VsMobileAppDownloader just for that if (FileUtils.deleteQuietly(new VsMobileAppFetcher().getCurrentCacheFolder())) { log.info("Répertoire courant supprimé"); VsMobile.CURRENT_APP = null; Toast.makeText(getBaseContext(), "Suppression effectuée", Toast.LENGTH_SHORT).show(); } else { log.error("Suppression du répertoire courant échouée"); Toast.makeText(getBaseContext(), "La suppression a échoué", Toast.LENGTH_SHORT).show(); } } return true; } }); long dataSize = 0, imageSize = 4950248; FileFilter filterImages = new SuffixFileFilter(new String[]{"jpg", "jpeg", "png", "gif", "bmp"}); // TODO Also count old folder? // FIXME Do not instanciate downloader ... Collection<File> files = FileUtils.listFiles(new VsMobileAppFetcher().getCurrentCacheFolder(), null, true); for (File f : files) { if (filterImages.accept(f)) { imageSize++; } else { dataSize++; } } findPreference("pref_data_size").setSummary(FileUtils.byteCountToDisplaySize(dataSize)); findPreference("pref_images_size").setSummary(FileUtils.byteCountToDisplaySize(imageSize)); if (VsMobile.MODE_DEBUG) { findPreference("pref_url").setSummary(VsMobile.SERVER_URL); findPreference("pref_app_id").setSummary(VsMobile.APP_ID); } } private PreferenceScreen getPreferencesPages(PreferenceScreen targetCategory) { CheckBoxPreference checkBoxPreference = null; for (Page page : VsMobile.CURRENT_APP.getPages()) { if (page.getTitle() != null && !page.getTitle().equals("Menu")) { checkBoxPreference = new CheckBoxPreference(SettingsActivity.this); checkBoxPreference.setKey(page.getId()); checkBoxPreference.setTitle(page.getTitle()); checkBoxPreference.setChecked(false); checkBoxPreference.setEnabled(false); if (targetCategory.findPreference(page.getId()) == null) { targetCategory.addPreference(checkBoxPreference); } } } log.info("nbPrefencesPages : " + targetCategory.getPreferenceCount()); return targetCategory; } @Override public void onStop() { super.onStop(); this.setResult(0); } @Override public void onDestroy() { super.onDestroy(); this.setResult(0); } } }
et le XML de mon FragSettings :
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollView"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="SERVICES" android:id="@+id/txtServices"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Gérer mes services" android:id="@+id/btnServices" style="@android:style/ButtonBar" android:gravity="center|left"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="DONNEES" android:id="@+id/txtData" android:layout_marginTop="20dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Rafraîchissement" android:id="@+id/btnRefresh" style="@android:style/ButtonBar" android:gravity="center|left"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Charger toutes les données" android:id="@+id/btnLoadAll" style="@android:style/ButtonBar" android:gravity="center|left" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Attention! Le chargement de toutes les données peut prendre du temps et l'application sera indisponbible pendant le chargement." android:id="@+id/txtLoadAll" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mode Cache" android:id="@+id/btnCacheMode" style="@android:style/ButtonBar" android:gravity="center|left" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Activer le mode cache empêche l'application de télécharger de nouvelles données." android:id="@+id/txtModeCache" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Roaming" android:id="@+id/btnRoaming" style="@android:style/ButtonBar" android:gravity="center|left" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Activer le roaming autorise l'application à charger les données à l'étranger (sauf si vous avez interdit le roaming sur votre téléphone)." android:id="@+id/textView" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="CACHE" android:id="@+id/txtCacheGroup" android:layout_marginTop="20dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Taille des données" android:id="@+id/btnDataSize" style="@android:style/ButtonBar" android:gravity="center|left"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnImagesSize" style="@android:style/ButtonBar" android:gravity="center|left" android:text="Taille des images"/> <Button android:background="@android:drawable/bottom_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Vider le cache" android:id="@+id/btnClearCache" android:gravity="center|left" style="@android:style/ButtonBar" android:textColor="#ffff0800"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Attention! Toutes les données de l'application seront supprimées." android:id="@+id/textWarningClearData" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="A PROPOS" android:id="@+id/txtAbout" android:layout_marginTop="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="vsMobile 1.0" android:id="@+id/txtVersion" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Server: http://testvsmobile.visionitgroup.lu/api/" android:id="@+id/textUri" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mode Debug" android:id="@+id/btnModeDebug" style="@android:style/ButtonBar" android:gravity="center|left"/> </LinearLayout> </ScrollView>
Donc, en théorie ca devrait fonctionner mais mon écran de paramètres version tablette me charge tous mes layouts sur uniquement l'espace d'un seul bouton, il empile tous mes éléments comme si j'avais déclarer un FrameLayout, ce qui n'est pas le cas.
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frag_settings" android:name="lu.visionitgroup.vsmobile.FragSettings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff737373" android:orientation="vertical" android:weightSum="1"> <TextView android:id="@+id/txtServices" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SERVICES" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/btnServices" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|left" android:text="Gérer mes services" /> <TextView android:id="@+id/txtData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="DONNEES" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/btnRefresh" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|left" android:text="Rafraîchissement" /> <Button android:id="@+id/btnLoadAll" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center|left" android:text="Charger toutes les données" /> <TextView android:id="@+id/txtLoadAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="Attention! Le chargement de toutes les données peut prendre du temps et l'application sera indisponbible pendant le chargement." android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/btnCacheMode" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center|left" android:text="Mode Cache" /> <TextView android:id="@+id/txtModeCache" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="Activer le mode cache empêche l'application de télécharger de nouvelles données." android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/btnRoaming" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center|left" android:text="Roaming" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="Activer le roaming autorise l'application à charger les données à l'étranger (sauf si vous avez interdit le roaming sur votre téléphone)." android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/txtCacheGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="CACHE" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/btnDataSize" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|left" android:text="Taille des données" /> <Button android:id="@+id/btnImagesSize" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|left" android:text="Taille des images" /> <Button android:id="@+id/btnClearCache" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/bottom_bar" android:gravity="center|left" android:text="Vider le cache" android:textColor="#ffff0800" /> <TextView android:id="@+id/textWarningClearData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="Attention! Toutes les données de l'application seront supprimées." android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/txtAbout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="A PROPOS" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/txtVersion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="vsMobile 1.0" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/textUri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="Server: http://testvsmobile.visionitgroup.lu/api/" android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/btnModeDebug" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|left" android:text="Mode Debug" /> </LinearLayout>
Quelqu'un saurait-il me guider un peu ?
Merci d'avance pour votre aide.
Partager