I am trying to add an image into my app with java so I googled how to do this and seen I had to add
Context mContext;
Drawable myImage = mContext.getDrawable(R.drawable.my_image);
(That was all of the code they said I had to add) But when I run it I don't see my image I set to it. So how I would be able to show the image I set to it?
For a image to be displayed, you need to give a view for that . ie - ImageView
Add a ImageView in your main-layout.
Initialize that ImageView in the on create of your activity like
ImageView appImage = findViewById(R.id.your_image_View_id);
3.Then set an image to the image view like
appImage.setImageResource(R.drawable.your_image);
Hope this helps.
You just initialize the imageview from layout like below,
ImageView appImage = findViewById(R.id.your_image_View_id);
appImage.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_downarrow));
With new android API 22 getResources().getDrawable() is now deprecated. So now the best approach is to use only getDrawable() is using ContextCompat
Please follow the steps:
Step1: In your main_layout.xml, add an <ImageView ... tag give an id, let's say id is "imageView"
Step2: In your MainActivity.class onCreate() method, Initialize that ImageView as:
ImageView image = findViewById(R.id.imageView)
then,
Step3: assign your drawable as:
im.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.your_image_drawable));
If you are using a fragment, use:
im.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.your_image_drawable));
Hope it helps. Please update if it does.
ImageView appImage = findViewById(R.id.your_image_View_id);
In activity simply use setImageResource
appImage.setImageResource(R.drawable.ic_avatar);
Related
I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.
You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);
Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)
I'm struggling to get my android app to show and hide imageView objects programmatically. Actually I'm struggling to get expected behavior from imageView objects full stop.
Following the answers to this question,
Here's what I'm testing with:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView warn = (ImageView)findViewById(R.id.gpsStatusWarning);
warn.setImageResource(R.drawable.gps_error);
warn.getLayoutParams().height = 64;
warn.getLayoutParams().width = 64;
}
}
The above code is called in the parent activity's OnCreate method, and it does exactly what I expect: It changes the image of the object to be the one designated, and it sets the height and width of said object. However, what I can't seem to do is to set the object as INVISIBLE or GONE. I just can't make it vanish at all, in fact. I've tried both:
warn.setVisibility(View.INVISIBLE);
warn.setVisibility(View.GONE);
But the image is still visible. I've even tried changing it in the XML to
android:visibility="gone"
But even that hasn't helped. The image is still visible.
What am I doing wrong? Am I missing a call to some update method? Does setting the image resource force the image to be drawn?
Try :
warn.setImageResource(0);
Or : warn.setImageResource(android.R.color.transparent);
This is how you set the visibility of ImageView objects on Android programatically:
yourImage.setVisibility(View.VISIBLE);
yourImage.setVisibility(View.INVISIBLE);
yourImage.setVisibility(View.GONE);
You can also set the initial states of ImageView objects in XML layout files like this:
visibility="visible"
visibility="gone"
visibility="invisible"
You can follow the official documentation about ImageView controls to try it on yourself on this link below. Learn how to set the visibility state of a view.
imageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
I am currently using the #android:drawable/presence_online as the srcCompat for a ImageView.
I would like to change it programmatically to #android:drawable/presence_offline.
I tried the code below but it didn't work.
final ImageView imgPresence = (ImageView) findViewById(R.id.imgPresence);
imgPresence.setImageResource(R.drawable.presence_offline);
Any ideas?
You missed android in the resource id.
Instead of
imgPresence.setImageResource(R.drawable.presence_offline);
use this:
imgPresence.setImageResource(android.R.drawable.presence_offline);
Though commented by #Srijith, I am putting as an answer here:
Change the below line
setImageResource(R.drawable.presence_offline);
to
setImageResource(android.R.drawable.presence_offline);
try this:
imgPresence.setImageResource(getResources().getDrawable(R.drawable.presence_offline));
I am building my application using Android Studio, this app can upload an image from raspberry to my emulator. It works fine. What I want to do now is uploading this image and showing it directly to the user without searching it in the gallery. I thought about creating another class and setting this image as a background image in my xml file, but this is too much like I have to create another class every time I want to upload an image from my raspberry.
Can someone help me please. Thank you
If I'm understanding your question correctly, you'd like to load an image from the Android filesystem into your app and display it to the user.
Drawable, Android's generalized image class, allows you to load from file via Drawable#createFromPath.
This SO question suggests Drawable#createFromPath doesn't work on paths beginning with file://, so depending on your use case you may want to precede that with Uri#parse/Uri#getPath.
Once you have a Drawable, you can display it in one of two ways: put an ImageView in your app and call its setImageDrawable method, or set the Drawable as your background image via View#setBackground (note that setBackground was only added in API 16 - in prior versions, you should call View#setBackgroundDrawable).
Putting all of this together, we end up with the following (untested):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
You should be able to modify this to work with any View just by replacing imageDisplay's declared type with the appropriate View type and changing the cast on findViewById. Just make sure you're calling setBackground, not setImageDrawable, for a non-ImageView View.
I am trying to use this library for card view
https://github.com/DenisMondon/material-design-library
<com.blunderer.materialdesignlibrary.views.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mdl_title="CardView"
app:mdl_description="A Left Image CardView"
app:mdl_normalButton="Normal"
app:mdl_highlightButton="Highlight"
app:mdl_imagePosition="left"
app:mdl_image="#drawable/image" />
I can not figure out how to add a onClickListener for the labels normalButton and highlightButto.
Can someone please give me a hand. thank you
Also how to set an Image programmatically into this cardView?
It has following two listeners
private OnClickListener mOnNormalButtonClickListener;
private OnClickListener mOnHighlightButtonClickListener;
You need to implement CardView.mOnNormalButtonClickListener, CardView.mOnHighlightButtonClickListener in your activity and override their methods to implement the click listeners for these buttons and set them using these functions.
setOnNormalButtonClickListener(OnClickListener onNormalButtonClickListener)
setOnHighlightButtonClickListener(OnClickListener onHighlightButtonClickListener)
#Joolah
With the latest version of the library, you can have a cardview url image.
Just write this:
myCardView.setImageUrl("http://your_image_url");