I can't seem to fill my background with the image that I have chosen it is in ConstraintLayout how to fix this?
<ImageView
android:id="#+id/background_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background" />
this is my code of the ImageView
Add a scale type in your Image View.
<ImageView
android:id="#+id/background_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"
android:scaleType="center"/>
Related
I have designed a splash screen in Adobe XD as shown:
enter image description here
Now when I exported the parts of the image to android studio, I was unable to make all the images stick to the bottom as in XD. enter image description here
Can someone tell me how I would go about doing that and would be the best ViewGroup for such task?
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/group_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</LinearLayout>
It can be done with either ViewGroup. But the problem is not the the view group - the problem is that you have a single image for the whole content - it is bad practice.
There should be at least 2 images - one for the background and the other is for the content.
It is due to fragmentation issue regarding device screens - there too many different resolution, aspect ratios and dpis to make the single image work properly on them all.
I will show you an example based on your UI with a background and logo image with small images below logo. You just need to separate it into two files - background could be jpg but logo must be png with transparency. In case you will do it - here is how your layout should look like.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg" />
<ImageView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:padding="24dp"
android:src="#drawable/logo" />
</FrameLayout>
You may want to experiment with android:padding and android:scaleType to achieve the best result.
Edit
Changed android:layout_height="match_parent" to android:layout_height="wrap_content" - it will work better this way
Hope it helps.
I want to add the transparent black color on top of the image and make it darker.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/rest_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</RelativeLayout>
I can set the alpha parameter but the color change is in white.
I want to make the darker image like this.
How can i do it in xml or Java code. I will set it based on condition.?
Thanks.
What you need is called tinting. Apply a tint to your ImageView:
<ImageView
...
app:tint="#6F000000"
/>
Easiest/Fastest solution would be in XML
Add a second layer (can be a View, doesn't have to be an ImageView) on top of your ImageView, with the desired color/alpha. Show/Hide it when needed.
<ImageView
android:id="#+id/rest_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<View
android:id="#+id/overlay_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background=“#color/yourColorWithAlpha"
/>
</RelativeLayout>
You can add foreground tint with tint mode.
Make sure you add alpha channel in your foreground color.
foreground="##AARRGGBB"
AA = alpha channel in HEX
R,G,B = Red, Blue, Green.
<ImageView
android:id="#+id/vidthumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:foreground="#96222f3e"
android:foregroundTintMode="src_atop"/>
Try this second imageview will set the transparent color. Adjust the height according to your need.
500000 - last 4 digits stands for black color and first two stands for alpha you want to set.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/rest_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#500000"
/>
</RelativeLayout>
I know this may strike out as a dumb question but i bet some of you had this problem before.From the screenshot,i am attempting to set an image in an activity.However my image brings up these spaces i dont know how to get rid of them.I mean is there a way,either in xml or to programmatically resize image to fit the height and width?In short,is there a way i can get rid of these spaces above and below?
My xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.snappy.stevekamau.cosmeticsapp.Categories.Lips">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
<ImageView
android:id="#+id/imageTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_rectangle"
android:src="#drawable/img4" />
<TextView
android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
to make the bitmap fill the width and height you can add android:scaleType="fitXY" to your ImageView. This way your bitmap will be scaled in x and y independently, making your bitmap fill entirely the ImageView
Yes, it can be done by a single line of code. Here's the trick :)
android:adjustViewBounds="true"
Add this in ImageView.
<ImageView
android:id="#+id/imageTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/white_rectangle"
android:src="#drawable/img4" />
Well I have to fit one ImageView inside another one. It is smaller and has to be exactly at the center. I have both images scaled for different screen resolutions but I can test only on one phone and I am wondering if I set the second image's height and width with dpi to fit my screen resolution will it be perfectly matched on all screens or is there any property in Android aside from setting the height and width that will automatically center one ImageView inside of other ImageView?
Edit: It's in RelativeLayout and the ImageViews are at the top left so I haven't added anything specific to the because they by default are there.
Edit2: Well changing the first ImageView to be RelativeLayout helps center the second image inside of it but then the RelativeLayout grows too big and enlarges the image that is background. Is there a way to make the RelativeLayout be as big as its background?
try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="250dp"
android:layout_height="250dp" >
<ImageView
android:id="#+id/base"
android:layout_height="250dp"
android:layout_width="250dp"
android:src="#drawable/home"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/center"
android:layout_height="150dp"
android:layout_width="150dp"
android:src="#drawable/home"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
You cannot put an imageView inside another. Hope the following code will be helpful for you.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_image"
android:gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_on_center" />
</LinearLayout>
Use RelativeLayout and then use android:layout_centerInParent="true" remember the order you add the ImageViews will decide what is on top :)
Try this:
android:layout_gravity="center"
I have i weird problem. Or maybe i got it wrong. But my ImageView has somewhat like a padding. It looks like this:
So the "MyImage" is supposed to align to the topmost. But instead when I check the Graphical Layout the image is adjusted lower. So it's like my whole image is the green broken line.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/master_background">
<ImageView
android:id="#+id/topPage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/image" />
</RelativeLayout>
this is my code. I tried to change src to background but my image was adjusted.
How can i adjust the image. This application doesn't have window title.
Use android:adjustViewBounds="true" in your image view.
<ImageView
android:id="#+id/topPage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/image" />