Question

I have a problem concerning the width of a DialogPreference in Android. According to the width of the DialogPreference, I set the margin of a text field so that its position is aligned with the thumb position of a seek bar. This works fine when dragging the thumb.

But when I show the DialogPreference, the width has not yet been set and is always 0. Therefore, the initial position of my TextView is always 0 until I drag the seek bar for the first time.

I cannot find a suitable callback within the class that is called when the width is set (something similar to onSizeChanged() would be great).

Is there a callback or a trick that I can use?

Was it helpful?

Solution

The solution is pretty complex. You have to create your own widget derived from a standard widget (a widget that must be used in your dialog layout). You use this widget instead of the standard widget in the layout used by your dialog preference.

Then, you add a callback handler interface and a callback handler to this new widget. Afterwards, you can override onDraw() and call your callback handler before calling super.onDraw().

Now, when you implement the callback listener in your dialog preference class and assign it to this widget, you get an onDraw() callback. When this callback is called, all the width and height values have been set and you can work with them.

I know that this is a dirty hack, but I didn't find anything better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top