Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom spinner view
    text
    copied!<p>I have created my own custom spinner which I can access from "Custom and library views" as it implements <code>View</code>. But, the problem is that when drag my custom spinner from palette to use it in my <code>main.xml</code>, it throws "unhandled event loop exception". I don't know where is my mistake.</p> <p><strong>main.xml</strong> </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:pref="http://schemas.android.com/apk/res/com.myspinner" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;com.myspinner.Myspinner android:id="@+id/myspinner1" android:layout_width="match_parent" android:layout_height="wrap_content" pref:MyEntries="@array/testarray" /&gt; </code></pre> <p></p> <p><strong>MySpinnerattributes.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;declare-styleable name="MySpinner"&gt; &lt;attr name="MyEntries" format="reference"/&gt; &lt;/declare-styleable&gt; &lt;/resources&gt; </code></pre> <p><strong>spinnerlayout.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:text="@string/large_text" android:gravity="center"/&gt; </code></pre> <p><strong>Myspinner.java</strong></p> <pre><code> public class Myspinner extends Spinner{ LayoutInflater inflater; CharSequence cs[]={"MySpinner"}; String s[]={"MySpinner"}; View row; TextView txt; public Myspinner(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setAttributes(attrs); this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s)); } public Myspinner(Context context, AttributeSet attrs) { super(context, attrs); setAttributes(attrs); this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s)); } public class MyAdapter extends ArrayAdapter&lt;String&gt;{ public MyAdapter(Context context, int textViewResourceId,String[] objects){ super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub } public View getDropDownView(int position, View convertView,ViewGroup parent){ return getCustomView(position, convertView, parent); } public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { inflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row=inflater.inflate(R.layout.spinnerlayout, parent, false); txt=(TextView)row.findViewById(R.id.textView1); txt.setText(s[position]); return row; } } public void setAttributes(AttributeSet attrs){ if (attrs != null) { TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MySpinner); final int N = a.getIndexCount(); for (int i = 0; i &lt; N; ++i) { int attr = a.getIndex(i); switch (attr) { case R.styleable.MySpinner_MyEntries: cs=a.getTextArray(attr); s=new String[cs.length]; for (int j = 0; j &lt; cs.length; ++j) s[j]=(String)cs[j]; break; default:break; } a.recycle(); } } } } </code></pre> <p>It won't give the error if you simply copy paste the <code>main.xml</code> code. It gives the error when I drag my custom spinner from the palette.</p>
 

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