Hello! I use DialogFragment with my markup to display a dialog in which there are 4 items (changing the size of the text, changing the color of the text, etc.), I implemented the listener on the list, and when I click on the item, the alert dialog opens, with values ​​in it. I am interested in the following: How can I change the values ​​of TextView in the main activit from DialogFragment? I did so

((TextView)getActivity().findViewById(R.id.head)).setTextSize(50); 

The text size changes, but I cannot save the selected values, and when I reopen the program, the font size becomes standard again. I give the code DialogFragment'a.

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View myView = inflater.inflate(R.layout.settings_dialog, container, false); getDialog().setTitle("Настройки"); final List<SettingsVariable> settingsVar = new ArrayList<>(); settingsVar.add(new SettingsVariable(22, R.color.silver, R.color.white, R.raw.times_new_roman)); settingsVar.add(new SettingsVariable(32, R.color.white, R.color.white, R.raw.kabaret_simp)); settingsVar.add(new SettingsVariable(42, R.color.black, R.color.black, R.raw.asylbek_mereke)); mHead = ((TextView)getActivity().findViewById(R.id.head)); List<SettingDialogItems> dialogItems = new ArrayList<>(); dialogItems.add(new SettingDialogItems("Размер текста", "Выберите размер текста", R.drawable.ic_action_text_size)); dialogItems.add(new SettingDialogItems("Цвет текста", "Выберите цвет текста", R.drawable.ic_action_text_color)); dialogItems.add(new SettingDialogItems("Цвет фона", "Выберите цвет фона", R.drawable.ic_action_background_color)); dialogItems.add(new SettingDialogItems("ِШрифт", "Выберите шрифт", R.drawable.ic_action_style_font)); ArrayList<Map<String, Object>> data = new ArrayList<>( dialogItems.size()); Map<String, Object> m; for (int i = 0; i < dialogItems.size(); i++) { m = new HashMap<>(); m.put(mItem, dialogItems.get(i).getmItemName()); m.put(mItem2, dialogItems.get(i).getmItemDescriptions()); m.put(mImg, dialogItems.get(i).getmItemImage()); data.add(m); } String[] from = {mItem, mItem2, mImg}; int[] to = {R.id.txt_item_settings, R.id.txt_item_description, R.id.img_item_settings}; SimpleAdapter sAdapter = new SimpleAdapter(myView.getContext(), data, R.layout.item_settings, from, to); ListView list_settings = (ListView) myView.findViewById(R.id.list_settings); list_settings.setAdapter(sAdapter); list_settings.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { if (position == 0) { AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity()); alt_bld.setTitle("Выберите размер шрифта") .setItems(R.array.text_size, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog_one, int id) { mHead.setTextSize(settingsVar.get(id).getText_size()); } }) .setNegativeButton("Отмена", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = alt_bld.create(); alert.setIcon(R.drawable.ic_action_text_size); alert.show(); } } }); return myView; } 
  • In order for values ​​to be saved between application launches, they must be written to persistent storage, for example, SharedPreferences . In the activation, at the start, read these values ​​and apply to widgets. In the dialogue, write in the preferences new values ​​when changing - pavlofff

1 answer 1

In the dialog box, when you change the textView values ​​in the Activity , pass the parameters to the save() method, thereby saving your data to the SharedPreferences .

  public static void save(Context context, int textSize, int textColor, int bgColor, int font) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("textSize", textSize); editor.putInt("textColor", textColor); editor.putInt("bgColor", bgColor); editor.putInt("font", font); editor.apply(); } 

And when you start your Activity check the load() method for NULL , and if not, get the data in order and set the values ​​to your TextView .

  public static ArrayList<Integer> load(Context context) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(sharedPreferences.getInt("textSize", 0)); arrayList.add(sharedPreferences.getInt("textColor", 0)); arrayList.add(sharedPreferences.getInt("bgColor", 0)); arrayList.add(sharedPreferences.getInt("font", 0)); return arrayList; } 
  • Could you clarify more details? I updated the code, this is how I want to pass the values ​​to the textview , but I cannot figure out how to transfer these values ​​to save . After the values ​​are transferred to save , save must be registered in onStop and load in onCreate ? - Int404
  • In general, how much does such an implementation correspond to "how it should be"? Did I understand the correct working methods of dialog boxes, or did I, by not knowing, complicate the task? - Int404
  • Yes, you can also in onStop() or when you click on the OK button in the dialog box, send your data to the save() method parameters. Yes, correctly, in the MainActivity in onCreate() call the load() method and insert it into the TextView from ArrayList in this order - DevOma
  • Can you describe an example of how to send data to the `save ? Я просто уже несколько дней все никак не могу сохранить эти значения, попробовал по разному через ? Я просто уже несколько дней все никак не могу сохранить эти значения, попробовал по разному через sharedpreferens`, but I could not. - Int404
  • save(R.drawable.ic_action_text_size, R.drawable.ic_action_text_color, R.drawable.ic_action_background_color, R.drawable.ic_action_style_font); - DevOma