Skip to content
Advertisement

How to reference an ImageView from inside Java code in Android?

I have this xml-file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#444548">
  
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image">  
    </ImageView>
  
    <TextView android:id="@+id/TextView01"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:text="@+id/TextView01"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

I’m calling my TextView in my java code like this:

TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Welcome to Schogini!");  

Is there any way to do the same for an ImageView?

Advertisement

Answer

Ya sure.. you can create an object as like TextView you did above,and create a folder named drawable under res folder and place the image u need,and set image view image by calling its id from drawable

For ex:

       ImageView im = (ImageView) findViewById(R.id.my_image);  
       im.setImage(R.drawable.name_of_image);
  • without the extension of image.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement