Diedis
Diedis

Reputation: 15

XML image switching

I'm new to coding and I'm working on a text adventure and want the imageview to change images based on the location, I was wondering how it'd work and why this solution doesn't. Would it be better to do it based on player position? Where can I look to find information on that as I'm having some trouble. Thanks

<room>
        <east>1</east>
        <description>Room 1(0)</description>
        <ImageView
            android:id="@+id/ImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bedroom"
            />
    </room>
    <room>
        <east>2</east>
        <west>0</west>
        <description>Room 2(1)</description>
        <ImageView
            android:id="@+id/imagePlace1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/living"
            />
    </room>
    <room>
        <west>1</west>
        <description>Room 3(2)</description>
        <ImageView
            android:id="@+id/imagePlace2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/kitchen"
            />
    </room>```

Upvotes: 0

Views: 232

Answers (1)

Prathamesh
Prathamesh

Reputation: 1057

It will be nice if you use only 1 imageview and change the image based on location

to change the image use following java code:

imageview_object.setImageResource(R.drawable.image_name);

make sure that your image is inside res/drawable folder

And to get location in latitude and logitude you can follow this link https://javapapers.com/android/get-current-location-in-android/

Upvotes: 1

Related Questions