Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that the only <a href="http://developer.android.com/reference/android/view/Window.html" rel="noreferrer"><code>Window</code></a> implementation (<a href="http://android.git.kernel.org/?p=platform/frameworks/policies/base.git;a=blob_plain;f=phone/com/android/internal/policy/impl/PhoneWindow.java;hb=HEAD" rel="noreferrer"><code>PhoneWindow</code></a>) uses a <a href="http://developer.android.com/reference/android/view/LayoutInflater.html" rel="noreferrer"><code>LayoutInflater</code></a> in its <code>setFeatureInt</code> method and instantiates the new layout with <a href="http://developer.android.com/reference/android/view/LayoutInflater.html" rel="noreferrer"><code>inflate</code></a> and <code>attachToRoot=true</code>. Consequently, when you call <code>setFeatureInt</code>, the new layouts are not <em>replaced</em> but <em>attached</em> to the internal title container and thus drawn on top of each other.</p> <p>You can workaround this by using the following helper method instead of <code>setFeatureInt</code>. The helper simply removes all views from the internal title container before the new custom title feature is set:</p> <pre><code> private void setCustomTitleFeatureInt(int value) { try { // retrieve value for com.android.internal.R.id.title_container(=0x1020149) int titleContainerId = (Integer) Class.forName( "com.android.internal.R$id").getField("title_container").get(null); // remove all views from titleContainer ((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews(); // add new custom title view getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value); } catch(Exception ex) { // whatever you want to do here.. } } </code></pre> <p>I'm not sure whether the current <code>setFeatureInt</code> behaviour is intended, but it is certainly not documented one way or the other which is why I'll take this to the android devs ;)</p> <p><strong>EDIT</strong></p> <p>As pointed out in the comments, the aforementioned workaround is not ideal. Instead of relying on the <code>com.android.internal.R.id.title_container</code> constant you could simply <em>hide</em> the old custom title whenever you set a new one.</p> <p>Let's assume you have two custom title layouts:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout android:id="@+id/custom_title_1" ... </code></pre> <p>and</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout android:id="@+id/custom_title_2" ... </code></pre> <p>and you want to replace <code>custom_title_1</code> with <code>custom_title_2</code>, you could hide former and use <code>setFeatureInt</code> to add the latter:</p> <pre><code>findViewById(R.id.custom_title_1).setVisibility(View.GONE); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload