Android: Is there a way to show DialogPreference from code?
Asked Answered
R

1

10

I want to open some preferences (which are made by extending DialogPreference) on first app startup. Also, these preferences are used as usual preferences.

Is there a way of accomplishing this?

EDIT:

I have my custom preference, made like this:

public class CitySelectPreference extends DialogPreference  {
// Some code here
}

And as the solution I want it to be shown from the code, without the need of user getting to preference screen.

Remnant answered 21/6, 2012 at 16:24 Comment(6)
what version are you compiling against because depending on that there are a few waysNeuter
I'm targeting android 2.2... Also, found some answer here: https://mcmap.net/q/358778/-how-to-open-or-simulate-a-click-on-an-android-preference-created-with-xml-programmaticallyRemnant
i may be misunderstanding your question, but what is wrong with displaying the dialog preference in the oncreate method of the activity?Boulder
how can I do this? I have no access to the CitySelectPreference object from arbitrary code...Remnant
did you find the solution?Jugulate
@Jugulate Check Pdroid's solution, it seems to be working. I didn't try it as this problem is not actual for me anymore. Also, take a notice here if it works, please.Remnant
V
-1

Just do this :

CitySelectPreference preference = (CitySelectPreference) findPreference("city_pref_key")

//You have to set a key to yout PreferenceScreen
PreferenceScreen screen = (PreferenceScreen) findPreference("screen_pref_key");

//Retrieve the index of the preference in preferenceScreen
int index = preference.getOrder();

//Perform a click
screen.onItemClick(null, null, index, 0); 
Valdemar answered 31/3, 2015 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.