how can I set src attribute (line 7) dynamically in android studio,I tried to change the backgroundResource but is not the same
XML file
<ImageView
android:onClick="next2"
android:layout_margin="10dp"
android:id="#+id/img3"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/img3"
android:src="#9d070976"
android:layout_toEndOf="#+id/img2" />
Java code
a.setBackgroundResource(R.color.mycolor);
Depending on what form your image is in, use:
setImageBitmap()
setImageDrawable()
setImageIcon()
or setImageResource()
If your replacement image is in the form of a URL or Uri that you need to load, use an image-loading library like Picasso or Glide. They can load the image in the background and fill in the ImageView once the image is ready to be displayed.
Related
I have filepath which is a String holding the absolute path to an image file.
Is there a way to display the image to the user similar to when they use the Gallery app?
When I looked up solutions to this problem, most of them involved setting an ImageView's bitmap to the bitmap decoded from the file, but I don't want to go this route because the orientations may be different, the size of the picture may not be maximized depending on how it fits within the ImageView, and so on. I basically want to have it displayed full-screen. Or if this can be done with an ImageView I'd be curious to know how.
Is this possible to do?
You can declare an ImageView in your layout that fills the parent
<ImageView
android:layout_gravity="fill"
android:id="#+id/your_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then you use the Picasso library to load the image by
1/ Inflating the ImageView
yourImage = (ImageView) view.findViewById(R.id.your_image);
2/ Loading the image to the ImageView like
Picasso.with(context).load(filepath).fit().into(yourImage);
Check http://square.github.io/picasso/
Use the following library: TouchImageView
Usage:
Place TouchImageView.java in your project. It can then be used the same as
ImageView. Example:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
If you are using TouchImageView in xml, then you must provide the full package
name, because it is a custom view. Example:
<com.example.touch.TouchImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have downloaded the code from AndroidHive of customize list view but I don't want my image in list view I want to parse it from xml with a simple imageview
Assuming you already have the png image you can add that image in drawable folder
and then
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image1" />
simply refer that image with android:src of imageview
OR you can use Glide library
Whenever i am using transparent PNG images, on my android device ( nexus ), i get a border on one side of transparent part. What is the way to sort out this problem ?
The image you are trying to add may not be transparent.Check it...
In the xml file, use this code to add an ImageView and set a resource for it:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample" />
To access the ImageView from the java code ,use the following code.
ImageView imgView = (ImageView) findViewById(R.id.image);
You can also set a resource programatically:
imgView.setImageResource(R.drawable.sample);
I am using CircleImageView for populating circle image in my android application.But after using the sample project i found that image which is circled, set from the xml using the following code:-
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/circleImgView"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/image"
app:border="true"
app:border_color="#color/GrayLight"
app:border_width="10"
app:shadow="true" />
After that i tried to set image to this view from java. Image is successfully set but circle effect doesn't working.
imgView = (CircularImageView)findViewById(R.id.circleImgView);
imgView.setBackgroundResource(R.drawable.image);
I need to set image programmatically using java. But it is not working by this way.
I am trying to set bitmap to imagebutton but image size change
My Image xml is
<ImageButton
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/choose_pic"
android:tag="PIC1" />
i want to set image size accoring to the "#drawable/choose_pic" size
after choosing image from gallery i am convertin it to bitmap and it change the imagebutton size please give me some solution ?
//for that use ImageView with src
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
As you are using ImageButton you need to set your image in property android:src="" not as background of your ImageButton.
So set your image in src property and remove background as below:
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
EDITED:
Difference between android:background" and android:src"
android:background exists for all the view. As the name suggests this is what is going to be there in the background.
android:src exists for ImageViews and its subclasses. You can think of this as the foreground. Because ImageView is a subclass of View you even have android:background for that.
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.