tags:

views:

51

answers:

1

Hi,

Can you please tell me how can I create my own Preference class in andorid? with my own layout and own click listener?

Thank you.

+1  A: 

Well, that's about six or seven pages in a book, so it's a bit involved for a StackOverflow answer. :-)

Here is a sample project with a custom View, which is also wrapped into a custom Preference. It is only ~80 lines of code, though much of the UI smarts are bound up in the custom View class.

In a nutshell, to make a custom DialogPreference, you need to define:

  • The constructors
  • onCreateDialogView() and onBindDialogView() to create the dialog contents and populate the contents with the preference data
  • onDialogClosed() to handle when the user changes the preference
  • onGetDefaultValue() to handle your overall default if the preference is not yet set
  • onSetInitialValue() to either load in the preference value or use your default
CommonsWare
I create a class which sub-class DialogPreference, and overload onCreateDialogView and onBindDialogView(), but for some reason, when I run in debugger, these 2 methods never get called.And I see blank as the preference when i debug in emulator.
michael
@michael: it is difficult for me to advise you in the abstract. One option is for you to clone my code, get it working, then convert it into what you need step-by-step.
CommonsWare