I am just learning how to develop android apps but i came across an error in which i cannot load a decoded bitmap to my image view
My image view is declared in linear layout (vertical) as
<ImageView>
android:id="#+id/image"
android:layout_height="match_parent"
android:width="match_parent"
/>
And i am adding bitmap by this process in onCreate() method
ImageView iv=(ImageView)findViewById(R.id.image);
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
iv.setImageBitmap(bv);
When i run this code it shows me a dialog box showin your project contains errors ...
Please help me
Thanks in advance
Firstly you need to rightly define you ImageView, it should be, without right bracket >:
<ImageView
android:id="#+id/image"
android:layout_height="match_parent"
android:width="match_parent"
/>
Secondly, if you want to set image from resources, you can simply use setImageResource():
ImageView iv=(ImageView)findViewById(R.id.image);
iv.setImageResource(R.drawable.ic_launcher);
Maybe try loading the drawable instead of the bitmap.
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher);
See here for more details
Related
I have a custom notification with RelativeLayout in root of my layout. I need to set background image to this RelativeLayout. I have an image url and then I download Bitmap using Picasso.
Is there a way to set Bitmap to custom notification?
All answers are about settings color or image which are located in resources remoteView.setInt(R.id.viewid, "setBackgroundResource", R.color.your_color).
Unfortunately, I couldn't add background to RelativeLayout, but I found a workaround. I made layout like this
<FrameLayout>
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
ImageView now works as background because it goes first in FrameLayout.
I am having a recyclerview inside a relative layout and the app is working fine, the navigation drawer is smooth. But I added an Imageview over the recycler view so that I can hide and show based on the availability of the data. But soon as I have added the Imageview the navigation drawer becomes very slow. And when I remove the Imageview it again works smooth. The image size is just 38kb. Can someone tell me how to show empty state in an efficient way
This is my Layout with the ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test.sample.itemHistory"
tools:showIn="#layout/app_bar_item_history"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="#+id/materialList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageViewNoItems"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/noItems" />
</RelativeLayout>
Thanks in Advance
In my case, I try to load Image (PNG File within the Project Drawable Resource Folder) to my RecycleView's ImageView as well using Picasso and I facing the same problem where the Navigation Drawer sliding animation is being slow down.
I found that the main issue is because my Image File is quite Large so the Picasso load the Full Size image into the RecycleView's ImageView which would cause the Lag.
My Solution are: (I am using KOTLIN Language)
Re-size the image using Picasso built in resize function
Without Resize (Which Cause Lag):
Picasso.get().load(R.drawable.icon).into(holder.view.ImageView)
Resized (Solve Lag Problem):
Picasso.get().load(R.drawable.icon).resize(400,400).centerCrop().into(holder.view.ImageView)
Use setImageDrawable instead of Picasso
This Method does not required to Resize the image:
holder.view.ImageView.setImageDrawable(holder.view.ImageView.context.resources.getDrawable(R.drawable.icon))
used picasso or glide library for image loading.. and also add one line in your manifest and your project become smooth as before.
<application
android:largeHeap="true">
</application>
largeHeap is used to avoid outofmemory exception while loading images.
I have my imageview declared like this:
<ImageView
android:id="#+id/category_image_top"
android:layout_width="match_parent"
android:layout_height="170dp"
android:maxHeight="170dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/image_placeholder"
/>
And this is how I set my category_image_top in asynctask (on onPostExecute)
imageView.setImageBitmap(image);
When image is set, imageview suddenly gets a margin of 4px. But when I remove android:background="#drawable/image_placeholder" from XML everything is fine?!
BTW: image_placeholder is a 9-patch image, if this makes any difference.
Any ideas why this happens?
UPDATE: I've tried placing background as a solid color and then no margins appear when image is loaded. I've also tried placing another 9-patch image and when I do so margins appear again. So it must be something with background as an image
UPDATE2: Maybe it's an android bug like this guy points out? https://stackoverflow.com/a/8340745/581531
I can guess that image_placeholder has 4px transparent margin
OK. Solution was found, maybe it's not the prettiest one but hey, it work! :)
In my view I've just added an arbitrary View that is a holder for my image. On top of it i have my ImageView so when the image is loaded placeholder (View) get covered. This is my view (or at least part that matters):
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="#drawable/image_placeholder"
/>
<ImageView
android:id="#+id/article_image"
android:layout_width="match_parent"
android:layout_height="210dp"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
...
The element that contains the image view may have a margin, have you checked that?
Change android:background="#drawable/image_placeholder" to android:src="#drawable/image_placeholder"
Also add the following: android:scaleType="centerCrop"
If centerCrop doesn't work, try the other ones listed here.
I have a LinearLayout with width set to fill_parent and height to wrap_content. Now I want to give it a background from a png file, but doing it in a traditional way causes the LinearLayout to resize in order to show the whole background instead of cropping the additional part.
How can I set the background of LinearLayout so it won't resize?
Thanks
It appears that such thing is achievable only using the ImageView and setting the scaleType parameter accordingly. A quick workaround is to use FrameLayout and put the ImageView under another layout with transparent background.
Code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background" />
</FrameLayout>
The default behaviour of background bitmaps is to fill the container completely and grow the horizontal and vertical size of the container if needed. The solution is to create a drawable bitmap resource and changing the gravity.
Check the following link and look for the possible gravity values. Your view should then just reference the drawable resource and you should be fine.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Bitmap
I solved this one by subclassing LinearLayout and overriding the onMeasure(int,int) callback, then updating the layout XML to use my new layout type. I'm not at liberty to post that exact code, but what I did was invoke the super.onMeasure(int,int) implementation and check whether the measured height came back as the exact height of my background image. If so, I grab the measured height of all child views, then calculate a new height value and pass it to setMeasuredDimension(int,int).
put this code in the xml activity, for example activity_main.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/image" />
Is it possible to have overlapping views in Android? I would like to have an ImageView with a transparent png in the front and another view in the background.
edit:
This is what I have at the moment, the problem is that the image in the imageView is not transparent, the parts that should be transparent are just black.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gallerylayout"
>
<Gallery android:id="#+id/overview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView android:id="#+id/navigmaske"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/navigmask"
/>
</RelativeLayout>
edit:
I got it to work, it was a theme file from another programmer on the team.
Just changed this
<item name="android:background">#FF000000</item>
to this
<item name="android:background">#00000000</item>
Android handles transparency across views and drawables (including PNG images) natively, so the scenario you describe (a partially transparent ImageView in front of a Gallery) is certainly possible.
If you're having problems it may be related to either the layout or your image. I've replicated the layout you describe and successfully achieved the effect you're after. Here's the exact layout I used.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallerylayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery
android:id="#+id/overview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/navigmaske"
android:background="#0000"
android:src="#drawable/navigmask"
android:scaleType="fitXY"
android:layout_alignTop="#id/overview"
android:layout_alignBottom="#id/overview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Note that I've changed the parent RelativeLayout to a height and width of fill_parent as is generally what you want for a main Activity. Then I've aligned the top and bottom of the ImageView to the top and bottom of the Gallery to ensure it's centered in front of it.
I've also explicitly set the background of the ImageView to be transparent.
As for the image drawable itself, if you put the PNG file somewhere for me to look at I can use it in my project and see if it's responsible.
Also, take a look at FrameLayout, that's how the Camera's Gallery application implements the Zoom buttons overlay.
If you want to add your custom Overlay screen on Layout, you can create a Custom Linear Layout and get control of drawing and key events. You can my tutorial- Overlay on Android Layout-
http://prasanta-paul.blogspot.com/2010/08/overlay-on-android-layout.html
The simples way arround is to put -40dp margin at the buttom of the top imageview
A visible gallery changes visibility which is how you get the gallery over other view overlaps. the Home sample app has some good examples of this technique.
Now with Jetpack Compose in android, you should use Box for overlapping views.
Example.
Box(modifier = Modifier.fillMaxWidth().fillMaxHeight()){
RecipesList(viewModel.recipes.value)
Snackbar()
}
Here RecipesList and Snackbar are composabes positioned one on top of the other in the composition order
Check out this for Jetpack Compose samples - https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/
Yes, that is possible. The challenge, however, is to do their layout properly. The easiest way to do it would be to have an AbsoluteLayout and then put the two images where you want them to be. You don't need to do anything special for the transparent png except having it added later to the layout.