{"id":3486,"date":"2016-05-06T01:02:16","date_gmt":"2016-05-06T01:02:16","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3486"},"modified":"2019-04-25T13:07:49","modified_gmt":"2019-04-25T13:07:49","slug":"android-how-to-set-a-custom-title-view-on-a-dialogpreference-extending-class","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/android-how-to-set-a-custom-title-view-on-a-dialogpreference-extending-class\/","title":{"rendered":"[ANDROID] How to set a custom title View on a DialogPreference extending class."},"content":{"rendered":"<p>So you have a <strong>FooDialogPreference<\/strong> class which extends <strong>DialogPreference<\/strong>, and one of the things you&#8217;d like to do to it, is to change the Dialog&#8217;s title.<\/p>\n<p><strong>DialogPreference<\/strong> happens to be a bit of a bitch, you can&#8217;t just call <strong>getDialog()<\/strong> and start playing with its features before it is shown because the <strong>Dialog<\/strong> object is only created when\u00a0<strong>DialogPreference.showDialog()<\/strong>is called.<\/p>\n<p>However, there&#8217;s ONE entry point where you can make your magic happen.<\/p>\n<p>If we look at the code of<br \/>\n<strong>DialogPreference.showDialog()<\/strong><br \/>\nfrom the geniuses at the Android project, some gentle soul added a call to<br \/>\n<strong>onPrepareDialogBuilder(mBuilder);<\/strong> which is a method you&#8217;re meant to override.<\/p>\n<p>Override this method, and then with the <strong>builder<\/strong> object you&#8217;ve received you will be able to call:<\/p>\n<p><strong>builder.setCustomTitle(myCustomTitleView);<\/strong><\/p>\n<p>However, there&#8217;s a couple of things you need to do:<\/p>\n<ol>\n<li>Instantiate your <strong>myCustomTitleView<\/strong> object on the implementation of your <strong>onBindDialogView<\/strong>, and keep the reference as a private property of your\u00a0<strong>FooDialogPreference\u00a0<\/strong>class.<\/p>\n<\/li>\n<li>\n<p>Make sure to detach it from it&#8217;s parent <strong>ViewGroup<\/strong>, otherwise you will get an <strong>IllegalStateException<\/strong> from <strong>android.view.ViewGroup.addViewInner()<\/strong> letting you know this view you&#8217;re trying to add, cannot have a parent.<\/p>\n<\/li>\n<\/ol>\n<p>This is what I had to do:<\/p>\n<p>[pastacode lang=&#8221;java&#8221; manual=&#8221;final%20ViewGroup%20parent%20%3D%20(ViewGroup)%20myCustomTitleView.getParent()%3B%0Aparent.removeView(myCustomTitleView)%3B%0Abuilder.setCustomTitle(myCustomTitleView)%3B&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<p>That&#8217;s all folks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you have a FooDialogPreference class which extends DialogPreference, and one of the things you&#8217;d like to do to it, is to change the Dialog&#8217;s title. DialogPreference happens to be a bit of a bitch, you can&#8217;t just call getDialog() and start playing with its features before it is shown because the Dialog object is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,15],"tags":[1434,1437,1481,1479,1225,1480],"class_list":["post-3486","post","type-post","status-publish","format-standard","hentry","category-android","category-code","tag-android","tag-code","tag-custom-title-view","tag-dialogpreference","tag-hack","tag-onpreparedialogbuilder"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Ue","jetpack-related-posts":[{"id":3572,"url":"https:\/\/www.gubatron.com\/blog\/android-how-to-create-a-background-shape-with-a-double-border-rounded-border-with-a-drop-shadow\/","url_meta":{"origin":3486,"position":0},"title":"[ANDROID] How to create a background shape with a double border \/ rounded border with a drop shadow","author":"gubatron","date":"July 10, 2016","format":false,"excerpt":"Usually we use <shape> objects in our .xml drawables meant for the backgrounds of buttons or other View objects. I came across a situation where such object should appear as if it had been previously selected, and the graphic design choice was to put a thick rounded stroke around the\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2063,"url":"https:\/\/www.gubatron.com\/blog\/android-programming-how-to-obtain-the-wifis-corresponding-networkinterface\/","url_meta":{"origin":3486,"position":1},"title":"Android: How to obtain the WiFi&#8217;s corresponding NetworkInterface","author":"gubatron","date":"September 19, 2010","format":false,"excerpt":"Let's say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":541,"url":"https:\/\/www.gubatron.com\/blog\/note-about-signals-and-pyqt4\/","url_meta":{"origin":3486,"position":2},"title":"Note about Signals and PyQt4.","author":"gubatron","date":"July 9, 2007","format":false,"excerpt":"I keep making the mistake of sending PyQt_PyObjects instead of sending actual Qt4 objects on signals that are defined by Qt like that. Bottom line: If a signal has been defined by Qt, to send Qt objects, just copy and paste it, do not try to override it by exchanging\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2887,"url":"https:\/\/www.gubatron.com\/blog\/android-changing-textview-alpha-transparency-across-different-target-sdks\/","url_meta":{"origin":3486,"position":3},"title":"Android: Changing TextView alpha transparency across different target SDKs","author":"gubatron","date":"November 30, 2012","format":false,"excerpt":"Sometimes you may need to make a TextView (label) look a little transparent to make emphasis on other parts of your UI. The .setAlpha() function on TextView is not supported after later in the SDK. Here's a static workaround you can place on some sort of UIUtils class you may\u2026","rel":"","context":"In \"Android\"","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/tag\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":3486,"position":4},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2660,"url":"https:\/\/www.gubatron.com\/blog\/5-object-oriented-programming-principles-learned-during-the-last-15-years\/","url_meta":{"origin":3486,"position":5},"title":"5 Object Oriented Programming Principles learned during the last 15 years","author":"gubatron","date":"February 1, 2012","format":false,"excerpt":"It was about 15 years ago when I first created my first class. I went from thinking of Object Oriented programming as this awesome paradigm I should know about, I remember having all these mental models of what objects where and learning all this new vocabulary that I really didn't\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3486"}],"version-history":[{"count":3,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3486\/revisions"}],"predecessor-version":[{"id":3782,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3486\/revisions\/3782"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}