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; }
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