Mobile/Android(Kotlin)

[Android/Kotlin] Set Preference Screen and Send data to another Activity from preference

개발왕 금골드 2020. 8. 7. 12:13
반응형

Hello i m GOLD.

this time i m going to make a preference screen in my app, and send a data to another activity. 

 

1. add implementation

first, we need to add implementation about preference in build.gradle(module: app)

 

in 2020 08 07 latest version 1.1.1

 

then we can use it. cool.

 

 

2. Create Options Menu

in MainActivity(or some activity) i m going to override onCreateOptionsMenu and onOptionsItemSelected.

 

in MainActivity code.

 

in onCreateOptionsMenu, i m going to use inflate for Instantiates a layout XML file into its corresponding view objects. maybe you can see the red line. its okay. i gonna make menu folder and main_menu.xml file. then it will be gone. res -> (mouse right click) -> New resources Directory -> (type and name)menu -> ok and create main_menu(or what you want).xml file in menu folder.

 

main_menu.xml

 

my xml file is so simple just one item "Settings". the important thing is id. you should write it for use it.

(now if you play this app, you can see the options menu in your app right top side.)

 

when our settings menu button click, then move to another activity.

 

in MainActivity.

 

override onOptionsItemSelected. write your item id. i set it "settings". so it is R.id.settings ->. in when item.itemId is corrected, then go to the preSetting function.

 

preSetting() function is just send intent to SettingsActivity. and start it.

 

in MainActivity code.

 

so if you click menu option settings button, then go to the SettingsActivity(or what you made). 

 

 

3. make a preference xml file

res -> Android Resources Directory -> create xml folder and preference.xml file.

 

preference.xml

 

you can divide preferences into PreferenceCategory.

ListPreference is when you click it, create a dialog. so we need to create dialog entries and entryValues xml file for the dialog.

res -> values -> create a array.xml

 

array.xml

 

you can make an array what you want. the font_list is on the dialog. the font_values is clicked value in regular order.

 

4. create a fragment

i gonna make a fragment class. this fragment will show a preference layout what i made. 

 

SettingsFragment whole code.

 

so override onCreatePreferences function and put my xml into addPreferencesFromResource. done.

 

 

5. intended Activity

second, in intended activity(in my case SettingsActivity) xml file has a fragment only.

 

activity_settings.xml file code

 

as you can see, fragment in activity_settings.xml refer my SettingsFragment.  it is important. now play it.

 

list preference dialog

 

 

6. send data to main activity

if you want to send some data(like entry in dialog what you clicked) to mainActivity(or other activity), you can use PreferenceManager. it can set values to key-value. so i create value with PreferenceManager.Editor() for send data to another activity.

 

SettingsActivity whole code.

 

in getString, Key values are taken from the xml file we made. put the corresponding value in the editor. and commit. 

 

now back to the MainActivity. we need to get the editor value. 

 

in MainActivity fun

 

so i use PreferenceManager again in MainActivity and getString. in getString, Key value is editor.putString's key value, not shared.getString's key value in SettingsActivity. it's a bit confuse. and you should use this function in onResume fun(or something in your code) in MainActivity.

 

now play it. 

 

 

i clicked the comic value, and go back to the MainActivity. you can see the Toast message in MainActivity.

 

 

this is GOLD

thank you.

*my english is not good sorry about it.

 

reference : stackoverflow.com/questions/6186123/android-how-do-i-get-sharedpreferences-from-another-activity

 

Android - How do i get sharedpreferences from another activity?

In my app there is a button (activity1). When user clicks it i want no sound in the game. I thought i should do this by using sharedpreferences in activity1 in the onClick method of the button:

stackoverflow.com

 

반응형