I want to develop an app for smartphones and tablets. Now my problem is that my layout isn't scalled in the right dimensens. On a 10' tablet it looks like this:
But this here are screenshoots from a 7' tablet and from a smartphone:
I would like to set a min width and height for the ImageView and scale it up and down depending on the screen size so in each layout the ListView is visible. Which unit do I have to use for the ImageView width and height? I tried with dp, in and some more but that does not help...
Use
<ImageView
android:layout_width="#dimen/imageViewWidth"
android:layout_height="#dimen/imageVewHeight"
android:scaleType="fitXY"
/>
scaleType="fitXY" will scale your image and fit with ImageView width and height. This will solve your problem.
Edit
In your values directory, open strings.xml file, then add following lines
<dimen name="imageViewWidth">30dp</dimen>
<dimen name="imageViewHeight">30dp</dimen>
Now, open values-sw600dp directory (of not, create one) and open strings.xml file (if not, copy from other values directory).
<dimen name="imageViewWidth">100dp</dimen>
<dimen name="imageViewHeight">100dp</dimen>
Now, open values-sw720dp directory (of not, create one) and open strings.xml file (if not, copy from other values directory).
<dimen name="imageViewWidth">120dp</dimen>
<dimen name="imageViewHeight">120dp</dimen>
This will solve your problem of different sizes of ImageView for different layouts.
Still can't understand, read my answer at
Android Universal App Approach
Layouts on different models of mobiles
Related
Currently, I'm developing Android app (phone only) and using only one size for different screen sizes, ie:
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
so in different screen sizes, we have only one size for components. And we go to this result: in small device, a textview can contain 10 chars but in larger device, a textview can contain 20 chars
And some developers use a gradle script to generate multiple dimens files in different folders based on the main dimens file like this:
values-sw320dp
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
values-sw480dp
dimens.xml:
<dimen name="button_size">52dp</dimen>
<dimen name="text_size">20sp</dimen>
...
so the system will use the dimens based on device size. And we go to this result: in small device and larger device, a textview can contain the same char, ie: 12 chars.
My question is: which one is better for UI, UX? (using Google Material Design)
You can use this library to support multiple screen dimen here
You can try this below, this will set automatically based on device.
?android:attr/textAppearanceMedium - For Medium font
?android:attr/textAppearanceSmall - For Small font
?android:attr/textAppearanceLarge - For Large font
Please check Material guidelines, To ensure usability for people with disabilities, give buttons a height of 36dp and give touchable targets a minimum height of 48dp.
Best practice is to use different dimens file for different devices. This will help you application view to be same across devices. If you keep same dimens for different devices then layout problem can also come. In some devices your layout will look perfectly fine but in another it will look very bad.
Android developer site also recommend to use different layout for supporting different devices.
I am setting my mobile app to work in tablets, one of the many things that need to be tweaked is the fon t size of menus which in an 10" screen are too small. So I'm looking for a global variable that change that font size like you can do with textViews and similar things with
<dimen name="size_font_view_tex">21sp</dimen>
inside dimens-wXXXdp/dimens.xml
Anybody know the name (if it is) of the dimen resource that changes the size of menus?
Bonus: List of all dimen resources
You have to use the different layouts for different size of screen.As you stated the 10" screen you have to make the folder layout-large in your res folder and make the layout according to need. Hope this will help you.
form How to set text size of textview dynamically for different screens
You should use the resource folders such as
values-ldpi
values-mdpi
values-hdpi
And write the text size in 'dimensions.xml' file for each range.
And in the java code you can set the text size with
textView.setTextSize(getResources().getDimension(R.dimen.textsize));
Sample dimensions.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>
When you create a new layout with right click, you can manage this creation with a "available qualifers".
Here for size, keep the qualifer size and set the screen size that you want.
For example you can set x-large to create a tablet layout for the same screen that you already created before.
more explain : https://developer.android.com/guide/practices/screens_support.html
I have an imageview that is within a cardview to create a circle image. On a smart phone, I want the size of the image to be 250 and 250dp, let's say that is about 80% of my phone width.
On a tablet, I want the image to be about 80% of the width of my tablet, and the height must also match the width. What's the best way to achieve it. Thanks?
Following is the code where I manually put the width and height of 250dp, which looks fine on smartphone, but way to small for tablet.
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="250dp"
android:layout_height="250dp"
android:elevation="12dp"
app:cardCornerRadius="125dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/image_album_art"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
</android.support.v7.widget.CardView>
Android is designed to make this easy. If you want a resource to change depending on the device configuration (i.e. screen size), you define that resource in a specific directory:
res/layout-sw720dp - large (9"-10") tablets
res/layout-sw600dp - small (7"-8") tablets
res/layout - all other devices
When you append -sw600dp to the directory name, this means "smallest width 600 dp". This means that this directory is meant for devices that have a screen width of 600dp or greater. "Smallest width" means whichever side of the screen is smaller, so whether it's in portrait or landscape, a 1067dp x 600dp screen counts as having a smallest width of 600dp.
You can make a copy of your entire layout file for each configuration and put one in each of the above directories. However, if all of the files are the same except for the View sizes, you will have a lot of duplicate code. If you need to make a change, you will have to make that change in all three files, which can lead to mistakes.
Instead of having multiple layouts, you can define a dimension that varies according to configuration. Layouts go in res/layout/, and dimensions go in res/values/
Create a new XML file dimensions.xml and put it in the res/values directory. Add a new dimen to it like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="album_art_size">250dp</dimen>
</resources>
This will be the file that phones will see.
Then, create two more values directories and append the smallest-width qualifier to them, so you have res/values, res/values-sw600dp and res/values-sw720dp. Copy dimensions.xml into both of those new direcories. You can then modify the album_art_size dimen in each of those files, and when your app runs on different screen sizes, it will see the appropriate dimensions.xml file.
The last thing to do is to point your layout at the album_art_size dimension you have just created. Replace 250dp with #dimen/album_art_size:
android:layout_width="#dimen/album_art_size"
android:layout_height="#dimen/album_art_size"
This will allow you to pick specific dimensions for each device category, which should get you close enough to your 80% screen size target. If you need exactly 80% of the screen width and a square View, you will need to fiddle with ImageView's scaleType and adjustViewBounds attributes or write some Java code.
What you can do is create a folder of layout of type large ie layout-large and then copy the layout from layout folder and paste it in layout-large folder and then just set height and width according to the screen size of tablet ie may be 450dp or so do check that!
I specify all font sizes in sp. The text looks good on several devices including Nexus 7, Galaxy S4 mini etc. However, on Galaxy S 3, the font is too large. Is this because the DPI of the S3 is so much more? If so, how do I adjust for this so that fonts display at relatively similar sizes?
DP/SP seems to be working properly. The larger font sizes I got was because of setting text sizes programmatically, using resource values which were already returned scaled - so essentially fonts were being double scaled. This could have been prevented by proper documentation by Android.
On my project i use different "dimens.xml" file for every type of density folder, for example:
values-mdpi
values-hdpi
values-xhdpi
dimens.xml has:
<!-- Text dimension for activity album preview -->
<dimen name="text_album_title">20sp</dimen>
<dimen name="text_album_author">18sp</dimen>
<dimen name="text_album_info">13sp</dimen>
<!-- Text dimension for row -->
<dimen name="text_track_title">16sp</dimen>
<dimen name="text_track_length">14sp</dimen>
And this is different for every file in folder. Is up to you to decide the size, you will see the difference between each dimension by using a layout with a TextView. Give it a try.
EDIT
I was totally wrong on this subject, thanks to Runloop and 323go.
I want to use different font size for different screen sizes.
I read about this many articles, but I'm not sure about usage. Is correct to use different dimens resource file for different screen dimensions like code below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">24sp</dimen>
</resources>
...
I also read that correct way is to use sp for font size, but this doesn't fit font in different screen dimensions as I expect.
If there, what are the disadvantages of using different dimens for every size?
Thanks
The best way is to create different layout resources for each of the screens you wish to support. Place each of the layouts in a separate folder that designates the width of the screen. For example, normal sized layouts go in your res/layout folder, and a layout resource for a 7 inch tablet (600 pixel width) would go in the res/layout-sw600dp folder. Make the resource names identical, but adjust your font sizes accordingly.
#up Not, it isn't good way.
#topic You can gets width & height of screen (and w&h of View). Next, you can set font, for example 2% of width screen. If you have content 1260x720, 0.02*1260=24,6 px (you can use also (int)24.6 to convert double to int)
I do something very similar and it's worked fine for me. Some people put the values in the various layouts such as Fietser suggested, but if all of your layouts end up being the exact same except for the font size, your approach is better. That way you can have a single layout and only modify the font sizes. But sometimes you might have changes in the actual layout xml, so then it's probably a wash between the two approaches.