Difference between src and background for image view
But both are looking differnt
First, the drawables are different.
Second, the background always scales to the size of the view.
when i press the imageview with background set it is not showing the selection. can anyone explain why this happens
The selection effect is because by default, the background is a StateListDrawable
with a different appearance in state_pressed
. Your custom background looks the same in all states. You can create StateListDrawables
in xml with a selector
.
What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?
setBackgroundResource
is for setting the background of an ImageView.setImageResource
is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
Difference between app:srcCompat and android:src in Android's layout XML
app:srcCompat
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
Note
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat
.
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle
file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
android:src
Sets a drawable as the content of this ImageView.It will display in
its original size. No automatic scaling .
Difference(s): android:src and tools:src?
If you are using android:src
in xml during runtime this will show up on the app, whereas if you use tools:src
in xml it will show only on the preview of Android studio and not on the runtime of the app.
Related Topics
Android Studio Create Project Template for New Projects
Android Calling Asynctask Right After an Another Finished
Applying Effects on Video Being Played
Modifying the Android Seekbar Widget to Operate Vertically
How to Get File Path from Sd Card in Android
Quality Selector for Exoplayer 2
Android: Backspace in Webview/Baseinputconnection
Add Google Play Services to Eclipse Project
Android What Permissions Required to Call Powermanager.Gotosleep(N) Put Device in Sleep Mode
Android Screen Size Hdpi, Ldpi, Mdpi
Android Xml Rounded Clipped Corners
How to Change the Color of the Tabs Indicator Text in Android
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
Receive Mms Messages in Android Kitkat
How to Put an Image in an Alertdialog? Android
Trying to Uninstall_Shortcut But Shortcut Won't Go Away