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.
Related
how to make single view with multiple imageviews?
I know this is just a simple question but its puzzle for me ...
problem
there are 4 images one for whole background image ,2nd is transparent heart and 3 image is to put in heart and 4 image is a shadow image of heart but how to manage these four images in a single view(any layout)....
Any help would be appreciated see link to see my desired output
link to see photo
update
<RelativeLayout
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/hv_effect"
android:layout_below="#+id/effect_hedaer_toolbar"
android:background="#drawable/home_wall"
android:weightSum="1">
<FrameLayout
android:id="#+id/relative_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center|center_vertical"
android:padding="50dp">
<ImageView
android:id="#+id/iv_effect_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:id="#+id/iv_effect_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|center_vertical"
android:adjustViewBounds="false"
android:scaleType="fitXY"
android:src="#drawable/pip" />
</FrameLayout>
</RelativeLayout>
i have used relative for background image and framelayout to show two imageview to show one is for to show lamp image and second two move as background (user selected image)....
problem:
problem is user selected image must be move only inside lamp.
update read comments to get answer
I'm pretty sure you can do it simply by using a RelativeLayout. The transparent image should be the last one in the layout, example:
<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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background_image"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/transparent_image"/>
</RelativeLayout>
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>
Screenshot of first image
Screenshot of other phoneI have a LinearLayout ,RelativeLayout,LinearLayout and a ImageView in it.This is basically a landing page of my app,inspite of using the dp my output is not same on the all device. There is the screenshot of two different phones. Thanks in advance
<LinearLayout
android:id="#+id/lb_LinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/lb_Relative_HomeScreen"
android:layout_width="match_parent"
android:layout_height="450dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="350dp">
`` <ImageView
android:id="#+id/lb_Background_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</LinearLayout>
<TextView
android:id="#+id/lb_Welcome"
android:text="#string/welcome"
android:gravity="start"
style="#style/Heading2.yellow"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="start|center_vertical"
android:layout_above="#+id/lb_Descrption"
android:layout_marginLeft="20dp"
/>
...
</RelativeLayout>
</LinearLayout>
You need to create different layout files for the major screen density buckets and screen sizes. This way you will be able to fine tune your layout to look consistent across multiple displays.
You can easily create a new layout for a specific bucket from the preview window:
Click on this icon in the left corner.
Then select Create Other....
A popup window will appear then select from the Available Qualifiers the size item for the different screen sizes of Density for the different density buckets.
After you selected the desired item press the >> key, and you can select the appropriate bucket for witch you wish to create a new layout file.
After this you press ok and the android studio creates for you a copy of the current layout and places it in the appropriate folder for you, all that you need to do is to edit it so that it look the way you want it to look on the specific layout.
You should use ScrollView like this :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/lb_LinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/lb_Relative_HomeScreen"
android:layout_width="match_parent"
android:layout_height="450dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="350dp">
`` <ImageView
android:id="#+id/lb_Background_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</LinearLayout>
<TextView
android:id="#+id/lb_Welcome"
android:text="#string/welcome"
android:gravity="start"
style="#style/Heading2.yellow"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="start|center_vertical"
android:layout_above="#+id/lb_Descrption"
android:layout_marginLeft="20dp"
/>
...
</RelativeLayout>
</LinearLayout>
And to be more compatible with all the devices, try to use "wrap_content" or "match_parent" for height and width. If images are making you to declare dp in your code, then please go through this link :
https://developer.android.com/guide/practices/screens_support.html
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.
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.