I'm just starting android programming and so I'm really noob!
I'm trying to set a plaid image as a background and I want to prevent from shuffling the squares in background and becoming rectangle when phone rotates!
can you come up with a solution to my problem!? preferably handling in XML file! tnx
This is what you are asking for:
<?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">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background_image"
android:contentDescription="#string/text" />
</FrameLayout>
<!-- rest of your layout !-->
</RelativeLayout>
just make sure that your image for background is big enough to fill big screen sizes.
Related
I am creating an App Gallery using CardView as my widget, but I am facing a problem that the images in my gallery are ignoring the size that I want for them, so they are all overlapping instead of creating a margin for it.
I am adapting the following Android Image Gallery:
https://medium.com/#moforemmanuel/android-simple-image-gallery-30c0f00abe64
I want something like that:
But running my code I get this result when I have more than one image:
And the code that I have based myself on you can see below:
<?xml version="1.0" encoding="utf-8"?>
<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="190dp"
android:layout_height="190dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_marginLeft=".5dp"
android:layout_marginRight=".5dp"
android:layout_marginBottom=".5dp"
app:cardCornerRadius="1dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
tools:ignore="ContentDescription" />
</FrameLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
What should I change in my code so that I can modify the images' size and still maintain a margin so that I will always have an adaptive layout?
Many thanks!
P.S.:
I've tried what Dharmaraj suggested but then I got the following layout:
Changing android:layout_width and android:layout_height to "200sp", it stills overlaps as you can see below:
To load the images I am using this two functions:
Try out this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.cardview.widget.CardView
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_marginTop="1sp"
android:layout_marginLeft="1sp"
android:layout_marginRight="1sp"
android:layout_marginBottom="1Sp"
app:cardCornerRadius="1sp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_center="true" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
I've basically replaced FrameLayout with RelativeLayout for ease and please assign a constant height and width to the card view. Don't assign a constant height and width to the main layout. This fixed this problem for me. Please let me know if you need more clarification in the comments.
By the way the attribute android:src in image is missing. Or are you assigning image dynamically?
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 added a fragment with an ImageView, which is a PNG image, and a TextView just below it.
The fragment is showing fine but when I rotate the screen the image will take most of the screen and the TextView is partially cut.
Here's the code:
<?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">
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/shopping_cart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#id/shopping_cart"
android:text="#string/no_data"
android:gravity="center"/>
</RelativeLayout>
I'm trying to find a way to resize the image in order to show both image and text in the screen
try using a scroll-view as the root element if u want to retain the same image size rather than resizing the image. Here's a small snippet -
<ScrollView
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"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(all your view elements)
</RelativeLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/text_image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(Enter view here)
</LinearLayout>
Alright, so firstly ensure that you have the image for the ImageView in all densities. I would suggest that you use this attribute in your ImageView code:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
The scale type defines how your image should scale according to the view. I faced a similar issue once, and i was able to solve it using this. Having said that, i have not seen the image you are using, so i cannot be very sure of it. But, i would say that you use:
android:scaleType="centerCrop"
you can find the definition of the attribute on the link provided above. Also, i do not know what devices you are targeting for your app, but its always good to keep views inside a scroll view so that they are visible even on a smaller screen.
Give it a try, if not by centerCrop, then maybe by some other attribute.
Cheers!!
Here is the code:
<?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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/shopping_text"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/no_data"/>
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/shopping_text"
android:src="#drawable/shopping_cart"/>
</RelativeLayout>
I have developed an android application. I used android:background property to make an image as background. But when it is displayed on square displays, the image keeps stretching.
How to make Android Background fit the screen without any stretching ?
I have seen many apps with images or videos that are non stretched , but fills the entire screen of the android phone.
My xml code is given below
<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=".MainActivity"
android:background="#drawable/Splash" >
</RelativeLayout>
You can try and use an ImageView rather than setting it to the background of the RelativeLayout
I have modified your code to look like this
<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=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/Splash"/>
</RelativeLayout>
Only problem with this solution is that the image might be cut off at parts on some devices. To solve this issue you can create a few images with different aspect ratios for relevant devices and put them into the relevant drawable-xxxx folders.
Also have a look at this thread for other solutions that might suit your requirements better.
you need severalimage for different displays
Supporting Multiple Screens
In Android to support multiple screen you must have to add different images according to screen resolution and screen size.
For more details just visit these links:
Drawable folders in res folder?
http://developer.android.com/guide/practices/screens_support.html
You could use a FrameLayout and put ImageView as background
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgPlaylistItemBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:maxHeight="0dp"
android:scaleType="fitXY"
android:src="#drawable/img_dsh" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
I am pretty new to android programming and i was hoping you could help me out with something. I have set a background in an xml file and i am trying to align a image button to a specific area of my background. I have tried using density pixels however it is not precise enough and the button now covers an area of the background i would like to be visible. Any ideas on how i could fix this?
Many thanks,
Alex
My code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/home_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="93dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="341dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/workbench" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
You can always try using px instead of dp, however the whole point of dp is that they will scale automatically for any screen. If you use px I suspect that it will only work well on the current screen resolution that you're testing on. You would have to define px values for numerous screen resolutions/densities to ensure that your app displayed correctly across different devices.