android


How can I make findViewById get the id attibute on the XML dynamically (Android)?


Tipically you would have this:
Activity parent = (Activity) context;
dragImage = (ImageView) parent.findViewById(R.id.what_ever_you_defined_on_xml);
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:
Activity parent = (Activity) context;
View v = parent.getCurrentFocus();
dragImage = (ImageView) parent.findViewById(v.getId());
Any suggestion? Thanks.
More details to add context:
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();
}
In the XML:
<ImageView
android:id="#+id/drag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/symbol1"
android:visibility="gone"
/>
<ImageView
android:id="#+id/drag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/symbol2"
android:visibility="gone" />
<ImageView
android:id="#+id/drag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/symbol3"
android:visibility="gone"
/>
<ImageView
android:id="#+id/drag4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/symbol4"
android:visibility="gone"
/>
Wouldn't your end result simply yield the same thing as:
dragImage = (ImageView)v;

Related Links

alert dialog in fragments
#gmail SQLite Error Android Email ID Key
Send data from 1 activity to other activity via intent in android.. i want send SongName to next activity
Connecting a Raspberry-Pi to Android phone wirelessly
Android Retrofit return status 500 internal server error
Cannot send binary data using multipart form (string params + video param + image param)
ways and usefull libraries for sending image from client (android) to server (Spring)
How long onhandleintent wait after stop service
Unable to start 'org.qtproject.example.testapp'
Licence Key Android
View.postDelayed why is it an instance method?
Matching Location.getTime() milliseconds to NMEA sentences
Markers aren't displaying
Soft keyboard not retain while orientation change
Why am i getting black background when i save an image in android
how to sent message in handler to specific handler only

Categories

HOME
cygwin
kentico
jmeter
solaris
ms-access-2016
yandex
limit
jetty
enthought
siddhi
radgridview
shared-libraries
qsqlquery
heat
capistrano3
atlassian-stash
video-editing
digital-signature
cfml
swagger-2.0
selinux
poedit
angular-routing
pimcore
event-store
short-url
draft-js-plugins
corpus
fragment-backstack
firebase-analytics
user-defined-functions
twiml
userdefaults
complex-numbers
utorrent
autolisp
reactjs.net
magento-1.9.2.4
apic
docfx
nohup
resolution
magento-1.9.3
trigonometry
apriori
chronicle-map
contract
strftime
ovf
spring-integration-sftp
google-cse
aquamacs
reindex
edit
quicktime
rsa-archer-grc
rmongo
meteor-packages
custom-lists
winusb
redbeard-ios
hclust
android-dateutils
observablecollection
refinerycms
android-wake-lock
pushbots
concept-insights
dynamics-nav-2016
sciruby
rpart
dronekit-android
git-ftp
mongohq
kaleidoscope
joomla-template
idn
dopostback
promotion-code
timthumb
cuteeditor
dataform
unattended-processing
zend-rest
feasibility
wordprocessingml
sun
asynchronous-wcf-call
software-lifecycle
technical-debt

Resources

Encrypt Message