Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lets say you have a custom view named InputView, which is not a TextView (lets say its a RelativeLayout).</p> <p>In your attrs.xml:</p> <pre><code>&lt;declare-styleable name="InputView"&gt; &lt;!-- any custom attributes --&gt; &lt;attr name="title" format="string" /&gt; &lt;!-- standart attributes, note android: prefix and no format attribute --&gt; &lt;attr name="android:imeOptions"/&gt; &lt;attr name="android:inputType"/&gt; &lt;/declare-styleable&gt; </code></pre> <p>In an xml layout where you want to include InputView:</p> <pre><code>&lt;!-- note xmlns:custom and com.mycompany.myapp --&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;!-- note that you will be using android: prefix for standart attributes, and not custom: prefix --&gt; &lt;!-- also note that you can use standart values: actionNext or textEmailAddress --&gt; &lt;com.mycompany.myapp.InputView android:id="@+id/emailField" android:layout_width="fill_parent" android:layout_height="wrap_content" custom:title="@string/my_title" android:imeOptions="actionNext|flagNoExtractUi" android:inputType="textEmailAddress" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>Inside your custom class you can extract attributes as usual:</p> <pre><code> ... private String title; private int inputType; private int imeOptions; ... TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.InputView); int n = a.getIndexCount(); for (int i = 0; i &lt; n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.InputView_title: title = a.getString(attr); break; //note that you are accessing standart attributes using your attrs identifier case R.styleable.InputView_android_inputType: inputType = a.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL); break; case R.styleable.InputView_android_imeOptions: imeOptions = a.getInt(attr, 0); break; default: Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr); break; } } a.recycle(); ... </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