1

I have a problem. I want to give a user freedom to select a number of Bluetooth devices in my app. Now I dont know how many Bluetooth devices will there be so I was wondering is it possible to give user an option to click on "Add a new Bluetooth Device" custom preference and open up a new preference screen for him to select Bluetooth setting of new device?

In summary the display would look like:

Add a new Bluetooth Device..

If user adds one, it should then look like:

Bluetooth Device 1

Add a new Blutooth Device..

If user adds another one it should look like:

Bluetooth Device 1

Bluetooth Device 2

Add a new Bluetooth Device..

I know how to use standard preferences and basic coding. The only thing I want to know is how can I keep on adding settings for these devices at runtime.I will appreciate any help. Thanks.

shaffooo
  • 1,478
  • 23
  • 28

2 Answers2

7

Not sure if I'm understanding this completely, but it sounds like you would want to add a preference for each bluetooth device. To do this, you would want to do something like this:

Inside of the function in which you add the bluetooth device:

SharedPreferences prefs = getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("BT" + nameOfDevice, whateverYouWantToStoreAboutTheDevice);
editor.commit();

If you want to retrieve all the preferences for each bluetooth device, you could get the Set of all keys in your SharedPreferences file, figure out which ones have the "BT" prefix, and pull each of those preferences. Something like this:

Set<String> keySet = prefs.getAll().keySet();
for(String key : keySet){
   if(key.startsWith("BT"){
       String theValue = prefs.getString(key, null);
       //Do whatever with that value
   }
}

However, it just dawned on me that it sounds like you're talking about dynamically adding Preference Views. That's something else entirely =).

Edit: Here's how to add a Preference with a View programmatically from your preferences Activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        CheckBoxPreference checkbox = new CheckBoxPreference(this);
        checkbox.setTitle("This is a checkbox preference");
        ((PreferenceScreen)findPreference("PREFSMAIN")).addPreference(checkbox);
    }

In this example, I gave my PreferenceScreen a key of "PREFSMAIN". You could add any kind of Preference you wanted in this manner.

BigFwoosh
  • 1,197
  • 10
  • 17
  • Thanks for the reply. Yah you are right. I can save data for each Bluetooth device in SharedPreferences file like that and pull it back when needed but I want to display that data on the preference screen as well. I dont know how to do it dynamically so that after adding a device user should be able to see the information stored. I want to know whether I can add a preference screen and a few preferences on it through code at run-time and update the preference screen view where user is adding Bluetooth devices? – shaffooo Mar 27 '11 at 14:47
  • I added code for creating Preference objects at run-time. Is this more of what you were looking for? – BigFwoosh Mar 27 '11 at 18:30
  • @Bigfwoosh...Thanks a lot. I think that will serve th purpose. I will try this and let you know. – shaffooo Mar 28 '11 at 13:20
  • 1
    @Bigfwoosh...Hi, I tried your solution. So now when I click on the custom preference I made, a new PreferenceScreen with a preference on it is created but I cant inflate the new screen. It is created but I cant see it, do you know why is that so? I tried setPreferenceScreen(ScreenObject) but it doesnt inflate the newly created screen :( – shaffooo Mar 28 '11 at 14:41
  • Can you simply call addPreference(thePreferenceScreen)? I can't try it out myself right now, but that may work. If not, check out the API reference here: http://developer.android.com/reference/android/preference/PreferenceScreen.html. It says you need to call createPreferenceScreen(Context) to instantiate a new PreferenceScreen. You may also want to refer to this question: http://stackoverflow.com/questions/5060103/start-an-other-preferencescreen-through-a-preferenceactivity-option – BigFwoosh Mar 28 '11 at 14:48
  • very nice answer! two birds with one stone! I was interested in the second part of the answer and because the time that this comment is written the findPreference method is deprecated you do NOT need to do this: ((PreferenceScreen)findPreference("PREFSMAIN")) you can simply do that: getPreferenceScreen() – George Pligoropoulos Nov 16 '12 at 23:00
0

Consider implementing your own preference activity and using http://developer.android.com/reference/android/preference/PreferenceManager.html

  • Thanks for the reply. I hope by implementing you mean extending. If that is the case then I am doing that already. I can get a customized preference as well clicking which I would create a new preference screen but how to add standard preferences on that screen from code? is that possible? – shaffooo Mar 25 '11 at 20:10
  • Just to make sure you understood my problem, If you check the "add a Wifi Network" feature of an android phone in settings, thats exactly what I want to do. – shaffooo Mar 25 '11 at 20:11