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.
Related
I want to create same UI as whatsapp chat.Here you can see whatsapp has background image:
Now this is my app in which I have a relative layout that contains swipe layout (androidx.swiperefreshlayout) in which it has recycle view. This is my app with normal color background:
I just want to set an image in the background same as WhatsApp. In order to achieve this what should I do?
Why don't you put it in the XML like this?
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lst_main"
android:background="#drawable/name_of_your_background"
/>
My View hierarchy looks like this:
<RelativeLayout>
<ImageView
android:id="#+id/my_background_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
androd:id="#+id/my_image"
android:clickable="true"
android:foreground="?attr/selectableItemBackground" />
</LinearLayout>
</RelativeLayout>
If I set an image Drawable to my_background_image, the ripple drawable provided by ?attr/selectableItemBackground over my_image disappears. It doesn't matter what image I use, the ripple is completely gone. I've also found that the background (which includes the down arrow) of any "foreground" Spinner also disappears.
This is very strange behaviour. Does anyone know why this is happening?
Edit: I'd like to add that the ripple appears as long as src="..." is not included in the background ImageView or setImageDrawable() is not called on it.
Setting a transparent background to the LinearLayout solves the problem.
"Borderless buttons draw their content on the closest background. Your button might not be having background between itself and the ImageView, so it draws underneath the ImageView" from This Answer
I want to overlay an image above all activities after some operation to have a feel like phone is locked. How can I do this programmatically in android ?
You can set a FrameLayout at the end of you XML layout file with an ImageView and "match_parent" dimension for both length and height. Set it's visibility to "gone" in the XML layout file. and the problematically in your code change it's visibility to visible to show this full screen Image and hide all the other components that are laid below it.
For example in your XML file put this at the end of the XML:
<FrameLayout
android:id="#+id/lockScreenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_iamge"
android:contentDescription="#drawable/your_iamge" />
</FrameLayout>
So it would be the child of your root xml element.
and then in code, do this:
lockScreenLayout = (FrameLayout) findViewById(R.id.lockScreenLayout);
lockScreenLayout.setVisibility(View.VISIBLE);
One simple solution consists in setting your image as the background of the root view of your activities.
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.