简体   繁体   中英

How do I make an Image appear in Android

I set the image on the graphical layout, but now I am actually trying to make it appear, so far I have this

ImageView redPlayer = (ImageView) findViewById(R.id.redCounter);

but it is saying redPlayer is unused so How do I make it appear?

you must than assign a bitmap to this ImageView, use :

ImageView redPlayer = (ImageView) findViewById(R.id.redCounter);
redPlayer.setImageBitmap(bitmap);

or you can set a drawable from resources , use :

ImageView redPlayer = (ImageView) findViewById(R.id.redCounter);
redPlayer.setImageResource(bitmap);

but it is saying redPlayer is unused

This is just a warning like with any variable. It is letting you know you haven't used it such as calling a method on it. You don't need to declare or initialize it in your Java code for it to appear. If you have given it an image to show in the xml then it should show when you run the program.

You need to give it an image. You can do this in xml with something like android:src="@drawable/someDrawable" or android:background="@drawable/someDrawable" . You can also do it in your Java code with different methods depending on what you need.

See the docs for the different methods.

This is a great tutorial example of using ImageView in Android http://examples.javacodegeeks.com/android/core/ui/imageview/android-imageview-example/

The unused comment from the IDE is just saying that you have not done anything with the image view you assigned (to just display it you do not need to use it in the activity).

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM