PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

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

 July 10, 2022     android, imageview, java, reference     No comments   

Issue

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?


Solution

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.


Answered By - Senthil Mg
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing