How to align an image-view for all android screens - android

I want to align an image view which is displayed same for all android screens expect for Android tab screen. Different alignment of imageview is noticed for imageview when app runs on emulator or live device. how do i align them properly??
Here is my activity.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#fff" >
<ImageView
android:id="#+id/forts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="150dp"
android:src="#drawable/forts" />
<ImageView
android:id="#+id/portal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/forts"
android:layout_below="#+id/forts"
android:src="#drawable/portal" />
<ImageView
android:id="#+id/emergency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/forts"
android:layout_toRightOf="#+id/portal"
android:src="#drawable/emergency" />
<ImageView
android:id="#+id/aboutus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/portal"
android:layout_toLeftOf="#+id/emergency"
android:src="#drawable/aboutus" />
<ImageView
android:id="#+id/maps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/emergency"
android:layout_alignLeft="#+id/emergency"
android:src="#drawable/maps" />
<ImageView
android:id="#+id/mailus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/emergency"
android:layout_alignTop="#+id/aboutus"
android:src="#drawable/mailus" />
</RelativeLayout>

You can have set variable dimen in different values folder as
1:values
-dimens.xml
<dimen name="image_email_width">45dp</dimen>
<dimen name="image_email_height">35dp</dimen>
2:values-xlarge
-dimens.xml
<dimen name="image_email_width">90dp</dimen>
<dimen name="image_email_height">70dp</dimen>
Then set your Imageview as
<ImageView
android:id="#+id/portal"
android:layout_width="#dimens/image_email_width"
android:layout_height="#dimens/image_email_height"
android:layout_alignLeft="#+id/forts"
android:layout_below="#+id/forts"
android:src="#drawable/portal" />

For different screen size and different densities image will differ.
For fitting the image into imageview usee,
android:scaleType="fitXY"
If you want your imageview similar to all devices, you need to create different layout folders for different screen sizes and different images like ldpi,hdpi,mdpi for different densities.

Use different types of images i.e. ldpi, mdpi, hdpi, xhdpi, xxhdpi.

Related

Android different margin_Top dp in every size of screen

I actually have fixed margin_Top, which is 50dp. But when I tested with different size of screens, the imageview will not be in the same position as each other. Therefore, I searched something about how to adjust the dimen.xml in values. But I have no idea how to edit, create new dimen.xml, and calculate the size in different screens.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_centerInParent="true"
tools:context="com.example.mygames.MainActivity$PlaceholderFragment">
<ProgressBar
android:id="#+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="350dp"
android:layout_height="750dp"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:progressDrawable="#drawable/circular"
android:secondaryProgress="100"
android:layout_marginBottom="110dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/whitecircle"
android:id="#+id/imageView3"
android:layout_alignTop="#+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:id="#+id/tv"
android:layout_width="250dp"
android:layout_height="250dp"
android:gravity="center"
android:text="25%"
android:textColor="#ffffff"
android:textSize="35sp"
android:textStyle="bold"
android:layout_marginBottom="57dp"
android:layout_alignBottom="#+id/circularProgressbar"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textSize="20sp"
android:text="Progress" />
</RelativeLayout>
My dimen.xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="appbar_padding_top">8dp</dimen>
</resources>
This is done in a different way.
Like you can put a layout for landscape mode in a folder layout-land you can put values files (like dimens.xml) in different folders too.
Example is the values-w820p folder which is created in a default project.
the -wxxxx is the screen dimension (for tablets and the like).
So you just need copies of your dimens.xml in different values folders, each containing the values for the screen size.
I am not native english, I hope you understand what I mean.
Android will automatically pick the file which fits the currently running device best.
Read through the resource features here: https://developer.android.com/guide/topics/resources/providing-resources.html
Here is a simple example:
in your values folder is dimens.xml which holds
<dimen name="examplewidth">50dp</dimen>
now create a folder values-mdpi and just copy the dimens.xml file into that folder.
in that copy you change the value from 50dp to... say 20dp.
Then examplewidth will be 50dp on all devices except on low-res screens (mdpi) where it will resolve to 20dp. Automatically you do not need to write a single line of code.

png images getting streached inside imageview

<LinearLayout 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:orientation="vertical"
android:padding="#dimen/contactUsPad"
tools:context="com.onqanetapp.mp.onqanet.ContactUs_Fragment">
<!-- TODO: Update blank fragment layout -->
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:text="#string/contactUsAddHead"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/contactUsAdd"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".6"
android:orientation="horizontal">
<ImageView
android:id="#+id/ivFb"
android:layout_width="#dimen/socialImageWidth"
android:layout_height="#dimen/socialImageHeight"
android:layout_marginRight="#dimen/socialImageRightMargin"
android:src="#drawable/f" />
<ImageView
android:id="#+id/ivIn"
android:layout_width="#dimen/socialImageWidth"
android:layout_height="#dimen/socialImageHeight"
android:layout_marginRight="#dimen/socialImageRightMargin"
android:src="#drawable/in" />
<ImageView
android:id="#+id/ivTw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/socialImageRightMargin"
android:src="#drawable/tw" />
</LinearLayout>
</LinearLayout>
dimens
<dimen name="socialImageHeight">50dp</dimen>
<dimen name="socialImageWidth">50dp</dimen>
<dimen name="socialImageRightMargin">10dp</dimen>
Image
this is final layout screenshot and i did't change the last imageview to show you guys the problem
info
all images are png
first drawable start's at 48x48
question
wrap_content should give those images there resolution i.e 48x48 , but it's not.
when i give them width and height of 50dp they looks stretched but
also looks out of bounds.
also tired 72x72 resolution but same
You have to adjust the image bounds according to the screen size.
android:scaleType="centerInside"
It wont strech or crop image. It will simply resize the image
try to add following lines in imageView xml :
android:adjustViewBounds="true"
android:scaleType="centerCrop"
or
android:adjustViewBounds="true"
android:scaleType="fitXY"; //fitCenter, or change according to your requirement
For Scaling the bounds of an image view:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Thanks for the answers guys .
but nothing works for me.whatever i do the image steal not crisp or clear so i use SVG.
===========
downloaded svg icons from http://www.flaticon.com/
then used Vector Asset Studio(https://developer.android.com/studio/write/vector-asset-studio.html)
(Right-click the res folder and select New > Vector Asset.)
to import SVG to vector asset studio which then convert the svg to drawable xml.
now it's work like a charm.

How to calculate image size for different devices dpi

I have a listview layout with images
It looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:adjustViewBounds="true"
android:id="#+id/listview_item_imageView"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FAC308"
android:id="#+id/listview_item_title"
android:text="TITLE"
android:textSize="20sp"
android:paddingBottom="40dp"
android:layout_centerInParent="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/listview_item_subtitle"
android:paddingTop="40dp"
android:text="SUBTITLE"
android:textSize="20sp"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/orangeSeparator"
android:src="#drawable/orangeseparator"
android:layout_centerInParent="true" />
If I run it on device with 720x1280 240dpi it looks like this
If I run it on device with 720x1280 320dpi it looks like this
I get images from the internet, so I cant prepare different versions.
How to make it look similar across all the devices ?
You can use fitXY Android Developer,
android:scaleType="fitXY"
or you can also create different drawable resources from the original one, with Photoshop or any similar program
in your container you have
android:layout_height="match_parent"
and in the image:
android:layout_height="240dp"
you have to have the same height or 'wrap_content' for your container not to have those gaps.
you can also add to the imageview:
android:scaleType="centerCrop"
another thing is that you can use specific library to handle loading of network images.
You can look this article . Make your app supporting different dpi variations and different screen types: Supporting Different Screens

How to Supporting Multiple Screens (different distance button in three different device)

I'm trying to create an app that is suitable for any kind of screen.
I created 4 different layout types ( layout - sw320dp , 480dp , 600dp , 720dp )
I am currently proceeding for "step " and I started to create the layout for devices that fall within sw-720dp then average for devices from 10 "
This is the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/toolbar" />
<FrameLayout
android:layout_below="#+id/toolbar"
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:foreground="#drawable/shadow_toolbar"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/nav_body_main" />
</android.support.v4.widget.DrawerLayout>
Then I designed the layout for a fragment ... and I added a button and the floating action button to insert ourselves as social references ( facebook twitter etc ) .
With the latter I gave the size in dp and increasingly distances dp in the one of the more
This is code.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<Button
android:id="#+id/button_vai_assistenza"
android:layout_width="540dp"
android:layout_height="65dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/custom_button"
android:text="INIZIA SUBITO!"
android:textColor="#drawable/custom_textcolor_button"
android:textSize="20dp" />
<TextView
android:id="#+id/textView_nome_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView_logo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:text="Assistenza Copyworld"
android:textColor="#000000"
android:textSize="35dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="30dp"
android:src="#drawable/logo_nav_grande" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView_copyright_benv"
android:layout_alignStart="#+id/textView_copyright_benv"
android:layout_below="#+id/button_vai_assistenza"
android:layout_marginTop="122dp"
android:text="www.copyworld.it"
android:textSize="25dp" />
<RelativeLayout
android:id="#+id/RelativeLayout_social"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_social_facebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="false"
android:src="#drawable/fb_icon_social"
app:backgroundTint="#drawable/custom_floating_action_button" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_social_twitter"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toEndOf="#+id/float_social_facebook"
android:layout_toRightOf="#+id/float_social_facebook"
android:src="#drawable/tw_icon_social"
app:backgroundTint="#drawable/custom_floating_action_button" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_social_linkedin"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toEndOf="#+id/float_social_twitter"
android:layout_toRightOf="#+id/float_social_twitter"
android:src="#drawable/li_icon_social"
app:backgroundTint="#drawable/custom_floating_action_button" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_social_youtube"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toEndOf="#+id/float_social_linkedin"
android:layout_toRightOf="#+id/float_social_linkedin"
android:src="#drawable/yt_icon_social"
app:backgroundTint="#drawable/custom_floating_action_button" />
</RelativeLayout>
<TextView
android:id="#+id/textView_copyright_benv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="22dp"
android:text="Copyright © 2016 Copyworld srl " />
</RelativeLayout>
I then launched the app on the 3 tablet I have available .. (namely a galaxy tab 2 10.1, a galaxy tab A and galaxy tab E).
As soon as the app is open on the 3 devices I noticed this thing "abnormal" ...
The "social icons" and the TextView were at different distances between them ... (
in particular of the galaxy tab 2 10.1 and Galaxy Tab E,) while the Galaxy tab A icons and TextView are at the right distance as set in 'xml ...
How come the two devices distances not only equal to the right device ?? (The two devices have the text and view the icons at equal distances but different from the one on the Galaxy Tab A)
As for other components such as (image view with the app icon and the TextView with the name of the app) the dimensions and distances have been respected ...
How can I do to make everything in a "perfect adaptable layout" on all devices without each they assume proportions and distances different ??
I read the official guide to this topic, where he speaks of dp, its layout etc.
I leave the pictures attached of 3 devices.
You can use google library PercentRelativeLayout with this library you can set width and height of your views by percentage which is great because in all screen they look the same and of course it is not hard to code it. Here example:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
also add this line in your build.gradle
dependencies {
compile 'com.android.support:percent:23.2.0'
}
and official documentation by Google https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
If you have some questions, feel free to ask.
Create three different Layouts Folder in your res folder for all devices and use the dimensions accordingly.
Generic Layout Folders
res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge
After you are done with making your Normal/Medium Layouts follow these steps:
Convert the Normal Dimensions for other Screen Sizes.
Copy your Normal Layout xml files in to other Folders.
Change the suffix of the dimensions used according to the folder that you are in
Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).
Then your Layouts should work on every device with correct positioning.
For converting Values
0.75 - ldpi (small) //mdpi dimens *0.75
1.0 - mdpi (normal) //First create these dimensions
1.5 - hdpi (large) //mdpi dimens *1.5
2.0 - xhdpi (xLarge) //mdpi dimens *2.0
For Example
android:layout_width="66dip" //in normal
android:layout_width="100dip"//in large 66*1.5=100(approx)
android:layout_width="52dip" //in small 66*0.75=52(approx)
Also new Qualifier has been introduced
- SmallestWidth
- AvailableScreenWidth
- AvailableScreenHeight
read more about it here https://developer.android.com/guide/practices/screens_support.html
I hope this helps.

Make buttonsize bigger for tablet? Android, Xml

I have a android view with 2 buttons centered. I want to change the button sizes to be bigger when I run the app on a tablet because they look ridiculous small there since it is the same size as for the phones.
How can I do this?
This is my 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:background="#e9e9e9"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ImageView android:id="#+id/imageView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android_layout_gravity= "center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="#drawable/icon_bakgrund_android">
</ImageView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
>
<TableRow android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="260dp"
android:layout_height="50dp"
android:background="#drawable/nybutton"
android:layout_below="#+id/button2"
android:text="#string/las_sollefteabladet"
android:textColor="#ffffff"
android:layout_centerHorizontal="true"/>
</TableRow>
<TableRow android:id="#+id/TableRow02" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop">
<Button
android:id="#+id/button2"
android:layout_width="260dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/nybutton"
android:layout_centerInParent="true"
android:text="#string/annonsorer"
android:textColor="#ffffff"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
</RelativeLayout>
Here you go
insert in the button code
android:layout_weight="some integer value according to your need"
Simply make a separate layout file and place it in the folder
'Layout-xlarge'
or
'Layout-large'
You can do either of the following
make separate layout for large screen
use layout:weight for the buttons. Hence the button size will be set depending on the screensize.
You can create different dimen.xml for different screen densities, so you can have only one layout to adjust to multiple screens.
EDIT:
You have to create several folders for diferent densities (values-mdpi, values-hdpi, etc) and in each you create a dimens.xml . Then you add dimension items like this:
<dimen name="title_small_font_size">14sp</dimen>
<dimen name="header_title_font_size">32sp</dimen>
and so, in each file, adjusting the number as you need. And then, in your button you set the button attributes for example:
android:textSize="#dimen/grid_title_text_size"
Hope it helps.
As far as my knowledge goes, giving static width and height for the button will not be good option here. All you have to do is to create a background image for the button for all type of mobile and tablet size and keep these images in appropriate drawable folders, and try to give the textAppearance for the text of the button like this:
android:textAppearance="?android:attr/textAppearanceLarge"
I copy and past my layout-folder, renamed it to layout-large (with a small l) and took in all my xml views and changed them and it works fine.
Thanks!

Categories

Resources