I am using Android Studio version 2.2. I am facing strange thing first time. In one of my layouts I am using image ‘abc.png’ and Android Studio preview showing the right preview with right image, but on real device the image is different. The real device getting image wrong constantly. I don’ know what am I doing wrong? Below is the button code.
<Button
android:id="#+id/btn_pay"
android:textColor="#525252"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="30dp"
android:background="#drawable/btn_normal"
android:text="Pay" />
Android studio is showing this preview as expected because 'Button image' (text 'Pay' is placed) I am using is what it should be.
but on real device it is showing different image as shown below
The button is getting wrong image on real device. One more thing I have checked some this button with other colored images as background it is properly reflected on android studio preview but on real device it always shows the same image no matter what image I use on background. Any help?
After struggling a lot, I have found the problem. The problem was not with Button png or xml drawable. The problem was in java coding. The image was placed programmatically. I have posted the answer in case it may help full for others.
What I want:
I want to make a Camera Activity (i.e. an Activity which shows the Camera Preview) in which a square shape (it's actually a bit complicated shape but I am saying square for simplicity) is overlay-ed on the camera preview, so that the user knows where to position the camera such that a particular square shaped real object is positioned inside that square shape overlayed on the camera preview.
Just like there is a face shape in the camera preview in the following image, so that the user can put their face there.
My Solution:
I thought of having a FrameLayout and then having SurfaceView for the camera preview and a VectorDrawable for the shape to be overlayed, as FrameLayout would Z-order its children such that VectorDrawable will end up being overlayed on SurfaceView.
Problems with it:
I need my app to be compatible with API 11 and above. So I'd have to use VectorDrawableCompat. To use VectorDrawableCompat, we need to set an AAPT flag. - source
The problem is that everywhere on the web, the way to set this flag is for Android Studio. (like the method described here) A few lines are to be added to the project's build.gradle file.
I couldn't find out how to set this flag in Eclipse. I kept searching, and ended up installing the Eclipse Buildship plugin to use Gradle in Eclipse. I followed this guide. When I added the Gradle nature to my Android project, I did get a build.gradle file. I pasted the code to enable that AAPT flag in it but it did not help my problem at all.
SO NOW I AM LOOKING FOR AN ALTERNATE METHOD TO ACHIEVE MY TASK. PLEASE TELL ME HOW CAN I DO IT, WITHOUT USING VectorDrawableCompat?
I agree that VectorDrawable is better than png file.
But I think png is acceptable, because ImageView with dp size will automatically scale the image.
This is example:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="#drawable/face_shape"/>
</FrameLayout>
Today I received a complaint for an app that I made. Basically, the problem is that I have an ImageView which loads an image from the resources directory, but sometimes, the image is not loaded and a white space is displayed instead. The only complaint comes from Sony Xperia Z3 with the Android version 5.1.1. The drawable is added only for xhdpi, but I know that if needed Android scales it to the corresponding resolution. Do you have any ideas why this could take place since I wasn't able to reproduce the bug? Also, here's my ImageView code but I'm sure that there is no problem.
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="#dimen/padding_medium"
android:background="#drawable/green_logo"
android:scaleType="fitCenter"
android:contentDescription="#null" />
Why do you use background for ImageView? Use android:src="#drawable/image".
I added some background pictures to different linear layouts in my application. as you can see on Eclipse they all look fine.The problem is that on an emulator or a phone the one for the main linear layout gets messed up, does anyone know why?
That's the code I used:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/back"
>
The background is a real picture, not code:
The problem may be that your image is being read into a 16-bit (RGB565) bitmap. See http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/ -- you may want to read the image from /res/raw and use a BitmapFactory to read it in at the proper depth. Also see http://android.nakatome.net/2010/04/bitmap-basics.html for more on that. It also might be enough to make sure to save your .png with an alpha channel, which would force the system to interpret it as 32-bit.
Try this in Activity in onCreate() getWindow().setFormat(PixelFormat.RGBA_8888);
I have a ListView whose items have a tiled background. To accomplish this, I use the following drawable xml:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/tile"
android:tileMode="repeat" />
Usually, this works. Sometimes, however, the src drawable isn't tiled, but stretched to fill the entire list item. (I've got several different tiles like this, and I use them mixed in one ListView. If there is stretching instead of tiling, it's never been in all of them at once, for what that's worth.)
I also tried to add android:dither="true" to that xml, since I read somewhere that without it there might be bugs. That didn't change anything.
Has anyone had the same problem? How did you fix it?
I also got bitten by this problem. Very hard to diagnose, even harder to find similar reports and usable solutions.
"Tapas" on the freenode #android-dev irc channel came with the following utility method:
public static void fixBackgroundRepeat(View view) {
Drawable bg = view.getBackground();
if (bg != null) {
if (bg instanceof BitmapDrawable) {
BitmapDrawable bmp = (BitmapDrawable) bg;
bmp.mutate(); // make sure that we aren't sharing state anymore
bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
}
}
}
Apply it to all Views that have a tiled background set (e.g. findViewById them).
Also, I have the impression this bug started acting up after setting "anyDensity=true" in AndroidManifest.xml
I've just had the exact same issue except with CLAMP TileMode. I have a bitmap that I want to then just stretch away at the bottom and have it set up as an XML defined BitmapDrawable and in the Graphical Preview window all looks fine, no matter what size I make the ViewImage it draws my bitmap at the top and then repeats the last pixels to fill to the end.
Launching the app on various SDK builds on the emulator and on my own phone all then produced a straight 'fill' type distortion which is completely useless.
The solution turned out to simply be to re-apply the TileMode every time I changed the size of the ImageView within my code:
((BitmapDrawable)ascender.getDrawable()).setTileModeY(TileMode.CLAMP);
Now it's all drawing fine. So yes, this looks like a bug to me.
As I didn't see the link here, this was confirmed to be a bug in Android. It was fixed in ICS. See XML drawable Bitmap tileMode bug? for more details.
There is a lot of noise about this topic online, with various (and numerous) suggested solutions.
If you're still at a loss, my suggestion is to keep all tiled bitmap
resources to square, base-2 dimensions.
ie: 16px by 16px for an xhdpi tile asset.
I hoped that the Android platform would "over-tile" to fill a space if the bitmap did not tessellate perfectly - and then trim the waste. However trialling a 10px*10px tiled bitmap across mdpi, hdpi and xhdpi (and v2.3 to v4.0)'inconsistently' showed this stretching.
The base-2 dimension allows for whole and even division as you progress through the various resolutions and as each device tries to paint the tiles each time the view is created.
In Android development, we contest with the ranging hardware and the vendors dipping their fingers into the platform - sometimes this sort of trivial black magic just works.
This appears to have resolved the issue for me at least. Worth a shot.
This sounds like a bug, although I've never seen it myself. If you have a simple APK that reproduces the issue, please send it to me (romainguy /at/ android.com) or file a bug here.
This blog entry discusses the issue
combined with this solution from Tapas listed by Ivo van der Wijk, it works for me.
The key was to remove the tiled setting from the XML, then set it to tiled at runtime. It does not work for me if they are both set to tiled.
Edit: actually, I lied. Even with this it seems to sometimes fail to tile.
Would be very nice to have a reliable work-around.
Edit 2: setting it to something else (eg. CLAMPED) then setting it back so far seems to be working.
I moved my image from drawable-xhdpi to drawable folder and everything was fine.
I was also having the same issue. What I was missing was that we need to add scaletype to fitXY in the imageview for the xml bitmap to work properly.
tile_bitmap.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/tile"
android:tileMode="repeat" />
layout.xml
<ImageView
android:layout_width="match_parent"
android:src="#drawable/tile_bitmap"
android:layout_height="match_parent"
android:scaleType="fitXY"/>