Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make findViewById get the id attibute on the XML dynamically (Android)?
    text
    copied!<p>Tipically you would have this:</p> <pre><code> Activity parent = (Activity) context; dragImage = (ImageView) parent.findViewById(R.id.what_ever_you_defined_on_xml); </code></pre> <p>However, in my case I want "findViewById" to get the id attribute dinamically because I have more than one attribute depending on what kind of OverlayItem the user want on the Map (Open Street Map). I have tried this but my application crash:</p> <pre><code> Activity parent = (Activity) context; View v = parent.getCurrentFocus(); dragImage = (ImageView) parent.findViewById(v.getId()); </code></pre> <p>Any suggestion? Thanks. </p> <hr> <p>More details to add context:</p> <pre><code>public Itemizedoverlay(Drawable defaultMarker, Context context, MapView map) { super(defaultMarker, new DefaultResourceProxyImpl(context)); this.defaultMarker = defaultMarker; this.map = map; // Setup for dragging: // Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle): Activity parent = (Activity) context; View v = parent.getCurrentFocus(); dragImage = (ImageView) parent.findViewById(v.getId()); xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2; yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight(); } </code></pre> <p>In the XML:</p> <pre><code>&lt;ImageView android:id="@+id/drag1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/symbol1" android:visibility="gone" /&gt; &lt;ImageView android:id="@+id/drag2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/symbol2" android:visibility="gone" /&gt; &lt;ImageView android:id="@+id/drag3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/symbol3" android:visibility="gone" /&gt; &lt;ImageView android:id="@+id/drag4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/symbol4" android:visibility="gone" /&gt; </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