Note that there are some explanatory texts on larger screens.

plurals
  1. POCan there be conditional statements on custom android attributes?
    primarykey
    data
    text
    <p>most of you will know that there is a possibility to give custom Attributes to custom Views in Android. This is explained quite brilliantly for example in <a href="https://stackoverflow.com/q/3441396/2135787">this thread here on Stackoverflow</a>. My question however is:</p> <p><strong>Is it possible to present such attributes only upon fulfillment of another condition?</strong></p> <p>What I mean by that is something like this (Pseudo-code):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;declare-styleable name="MyCustomView"&gt; &lt;attr name="isClock" format="boolean" /&gt; &lt;/declare-styleable&gt; &lt;if name="isClock" value="true"&gt; &lt;attr name="timezone" format="string"&gt; &lt;/if&gt; &lt;else&gt; &lt;attr name="somethingElse" format="string&gt; &lt;/else&gt; &lt;/resources&gt; </code></pre> <p>Now one possibility to not have to work with "wrong" attributes is to do this in the Java-Code, obviously:</p> <pre class="lang-java prettyprint-override"><code>public class MyCustomView { public MyCustomView(Context context) { TypedArray styleables = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); boolean choice = styleables.getBoolean(R.styleable.MyCustomView_isClock, false); if(choice) { // It's a clock, react to the other attrs } else { // Don't react } styleables.recycle(); } } </code></pre> <p>Another way would be to do what ilomambo suggested in his answer: create various custom views with different names and let them only have the attributes that belong to them.</p> <p>But I'm very much asking myself if it's possible to not confuse the programmer of the .xml-File in the first place and offer him only the stuff he really needs combined in one place. After all this <strong>is</strong> in a way already done by Android (well... the IDE/Lint/the Parser...) when hinting for example that the width or height of a view should be set to <code>0dp</code> when using <code>layout_weight</code>.</p> <p>But if I had to guess I'd say it's probably only possible if I rewrite the Android XML-Parser... Can someone please prove me wrong?</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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