Android Layouts have changed a bit since I've been away from the topic and I never had to get this deep into it before, so forgive what I hope is a relatively simple question.
Say you are developing a card game with a human player and 3 computer or remote opponents (not quite accurate, but close enough for my purposes). My plan is that there will be four children Layouts representing each of the player's "hands". The ones on the left and right will display their images in top down order. The ones on the top and bottom will display their images in left-to right order.
It's reasonable to assume Left and Right Layouts will have identical (gravity, right?) behavior (if different start and end pixels). Same goes for Top and Bottom. If it matters, usage will be restricted to Landscape mode.
What is the correct method of getting said childLayouts into their correct locations? What's the correct top level layout(s) to use for this? What should the player's children layouts be? (I have assumed GridLayouts, but it seemsa host of others might work too -- they will do nothing but hold a number of dynamically generated ImageViews)
Once I've got layouts in the right location, I'm good...I think.
Tx in advance
The answer apparently is to use the layout_width and layout_height params for a gridLayout specifying where they need to be inside of a relativeLayout (in this case I know I am using a 1200 x 600 device, but I'm sure there is a better way to go about this.
<GridLayout
android:layout_width="500px"
android:layout_height="100px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/NORTH">
</GridLayout>
<GridLayout
android:layout_width="500px"
android:layout_height="100px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/SOUTH">
</GridLayout>
<GridLayout
android:layout_width="100px"
android:layout_height="500px"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:id="#+id/WEST">
</GridLayout>
<GridLayout
android:layout_width="100px"
android:layout_height="500px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:id="#+id/EAST">
Related
the upper part of recyclerview item is erased in some phones and is normal in other phones as the images below.
the first image is normal which from samsung phone (android 12 )
the second image has the problem which from huawi phone (android 8)
this is the code
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="70dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_margin="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/itemImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_oval="true"
/>
<View
android:layout_width="1dp"
android:layout_height="1dp"
android:id="#+id/viewSupporter2"
app:layout_constraintBottom_toBottomOf="#id/itemImage"
app:layout_constraintEnd_toEndOf="#id/itemImage"
app:layout_constraintStart_toStartOf="#id/itemImage"
app:layout_constraintTop_toTopOf="#id/itemImage"
/>
<TextView
android:id="#+id/diseasename"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="disease name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
app:layout_constraintBottom_toTopOf="#id/viewSupporter2"
app:layout_constraintStart_toEndOf="#id/itemImage"
/>
<TextView
android:id="#+id/diseasenickname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text= "disease nickname"
android:textColor="#color/teal_700"
app:layout_constraintTop_toBottomOf="#id/viewSupporter2"
app:layout_constraintStart_toEndOf="#id/itemImage"
tools:ignore="NotSibling" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
You're doing a basic mistake of having your cards at fixed height of 70dp. Change them to be wrap_content and tweak margins/paddings of your layout instead to achieve the size you need.
While it looks ok on device you designed it for you can see it cannot fit TextViews on the other one so they ends up being clipped (on top). Actually you can even break it on your Samsung if you go into device settings and increase font size.
You have to let your card view "grow" in those cases so unrestraining height it is the way to go. Although you might consider adding maxlines to your textviews so they don't get too out of hand with maximum font size.
The problem here is your second device is set to a larger system font size than the first one. The dp system is designed so that things look consistent on different devices, and you can see that the ImageView takes up pretty much the same amount of space on both - the space around it is the same, the layout looks the same.
But you have to account for the user changing their overall text size, which is what's happening on your second image. See how the text is just bigger? Not just in your list items - the diseases text is taller than the hamburger icon on this one, the tab headers are much closer together because the words are wider...
You have three options in this case
use dp instead of sp for the text sizes (so they're consistent and don't scale to the user's preference)
make sure your fixed layout has enough space for those larger text sizes
make sure it can expand as necessary to fit them
The first option is ok sometimes, when you have text that's already large enough for everyone, and you want to maintain some kind of visual consistency (like a fixed design where things are a certain size etc). Don't do this as a way to get around the limitations of having to provide accessible content, that scales so the user can read it.
The flexible layout approach is what Pawel's suggesting - basically design your layout so it can grow if necessary. You can use the minWidth and minHeight layout attributes for this (and layout_constraintHeight_min in ConstraintLayout) to design how you want it to look, but use things like wrap_content and constraints to allow it to grow beyond that if necessary. That way you get it looking how you like, but if the user needs bigger text, it compromises.
And don't worry if you don't like how it looks with large text - that's not important! What's important is that the user can read it, and everything's visible and accessible. Nothing cut off, nothing overlapping anything, everything clickable etc. The user is the one making the compromise between style and practicality, and they probably don't have a choice, so just making it usable in that situation is the main thing.
The middle option is the fixed layout that has enough space for the larger text settings. This is actually what's recommended in the Material Design spec - I can't link directly to the Two line version, but go here and scroll down - there are some similar to what you're doing:
Those measurements are all in dp - notice that the top line's position is defined by where its baseline is, the line the text "rests" on, relative to the top of the layout. You can do that with:
android:layout_height="wrap_content"
app:firstBaselineToTopHeight="32dp"
app:layout_constraintTop_toTopOf="parent"
And then the text below has its baseline relative to the bottom of the first
app:layout_constraintBaseline_toBaselineOf="#id/firstTextView"
app:layout_marginBaseline="20dp"
or you could constrain it to the top of the layout like the first one, with a value of 32 + 20dp.
Notice that this design leaves space for the text to grow vertically (up from the baseline, which is in a fixed position). If you use the Material Design type scale (which is a bunch of standard text styles and font sizes with names like Subtitle and Body1) then those list item specs should have enough room to hold the text even at the larger font settings. Both lines in that image can pretty much double in height and still fit
The nice thing about these specs is someone's done the work coming up with a design for you! And they're working with fixed sizes, so you get a nice consistent look to your lists
I've had the same problem for a long time now, which is basically what the title says. I have drawn some pictures to visualize what I'm trying to achieve.
Picture of man on ground
So on this first pickture, we have a man(player) standing on the GROUND of the Background image. In a 1024x728 Device this player is positioned somewhere around the Screen Height - 120px. I wan't to support multiple screen resolutions, but when I run the App on a bigger screen, this happens of course:
picture of man not on ground
The height of the background is bigger, therefor the ground is futher away from the bottom and -120px is not enough.
(In the app; pictures are drawn with Canvas as bitmaps)
So my question is, how do I achieve to get this man to always have the position on the ground? What's best practise? And can you show me some examples?
Thanks a lot.
There are two aproaches:
1) Using RelativeLayout child of this layout can be specified in relation to other childern
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/name"> <<<< look at this
</RelativeLayout>
2) Using just added percentage support library. You can specify % position on the screen. Read this for it
This will be a pretty simple question, because I might have been doing the wrong thing for a long time.
I will introduce my problem with an example: Let's say that the designer defines the following design for a screen of our app (phone only):
Which I then would implement with the following layout code:
<?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:background="#android:color/white"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example text"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="56sp" />
</LinearLayout>
Which looks fine on the Nexus 5, but when I switch to a Nexus One, I get this picture, which is definitely not what I wanted:
So my solution for this was: defining a dimens.xml both in /values, and in /values-sw360dp. Then adding a dimension for the text size. Where the size would be 56sp in values-sw360dp/dimens.xml, and 52sp in values/dimens.xml
Of course, when the project got bigger and bigger, the amount of dimens just kept rising (we have about a hundred now). It's not only text sizes of course, but widths, heights, margins and paddings too.
Lately I got a remark, which said that keeping track of all the dimens is just hell, because sometimes you forget to edit one (like when making something smaller), and then it looks good on all screen sizes except one. I completely agree with this, but can't think of a better solution. Is this what dimens.xml is for, or is there a way better solution I just didn't find yet?
I get this picture, which is definitely not what I wanted
I am going to guess that you mean that you do not want this text to be word-wrapped.
is there a way better solution I just didn't find yet?
With respect to text size, you are welcome to use any number of libraries that can automatically adjust the font scale, such as AutoSizeTextView.
With respect to "margins and paddings", those are usually handled by dimension resources, akin to handling them in CSS rules in Web development.
Beyond that, design fluid layouts (so you can hard-code fewer "heights, widths") that can handle word-wrap (particularly for text not knowable in advance, such as user-entered values or data retrieved from the Internet).
It's really not just text, this was just a good example of showing the difference.
Most widgets don't have "word wrap" as an option; for those, you really need to use fluid layout designs, where the size of the widget is derived at runtime from other rules (wrap_content, match_parent, weights on LinearLayout, spanning rules on GridLayout, anchoring rules on RelativeLayout, column-sizing rules on TableLayout, etc.).
Okay this is probably a newb question but I can't find any solutions on google.
I'm a new android programmer and I made a simple hello world type program. I didn't do any funny configurations in the layout xml file but for some reason on certain devices the screen looks really zoomed in and almost kinda pixelated.
Is there some kind of common reason for this? All I have on my linear layout is an EditText widget, a textview widget, and two button widgets.
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/txtUser"
android:layout_height="wrap_content"
android:layout_width="200px"
android:text="" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Submit"
android:onClick="userSubmit" />
</LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clear Screen"
android:onClick="clearScreen" />
<TextView
android:id="#+id/txtMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16px"
android:text="Ready\n\n"
/>
</LinearLayout>
It's probably some version of the screen compatibility mode which attempts to make old apps compatible with newer large displays, scaling the UI up as necessary. Scaling causes pixelated look as well.
If you don't specify a targetSdkVersion in your manifest, it defaults to 1, enabling all compatibility modes.
Solution: Specify an actual target SDK version you're developing your app on. Usually it should be the highest one available.
It is because you should use DP dp units instead of Pixels px since DP works with the screen density. Remove things like android:layout_width="200px" and change them to android:layout_width="200dp"
PS: note the following:
Generally you do not want to set dp values, you should do pretty well using wrap_content or match_parent, since the LinearLayout calculate widths and heights for you. Try to resist the temptation of setting fixed values to views, unless is extremely necessary. As an experience Android developer I tell you that cases you wanna set fixed width/height are cases like:
You are downloading images from the Internet and you want the ImageView that will show it to be displayed as a placeholder until the download completes.
You're creating an horizontal gallery and you need to give it a maximum height so it wont surpass the screen.
In resume, complicated stuff like that. If you're starting, try to learn to use wrap_content and match_parent, it will save you a lot of headache later on :)
I'm trying to do my first app.
I'm doing a layout that contains four rows. There will be a title in the first row (10% height), two images in the second (40% height), two images in the third (40% height) and a button in the fourth (10% height).
Right now, I'm using a linear layout with vertical orientation. Using weight sum and weight, I have the correct proportion on each row.
But, if I use in the second and the third rows linear layout weight, then I get a warning about nested weights and bad performance. I understand the bad performance issue, but I don't know how to solve my problem without them.
I need each image be a 50% of its parent width.
Thanks for your help.
EDIT: That is a quick mockup of what i'm trying to accomplish
https://dl.dropbox.com/u/252856/androidlayout.jpg
In your particular situation you could make the two ImageViews occupy 50% of the parent's width without using weights with a block like this:
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="your_value">
<View android:id="#+id/anchor" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_centerHorizontal="true"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_alignParentLeft="true"
android:layout_alignRight="#id/anchor" />
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_alignParentRight="true"
android:layout_alignLeft="#id/anchor" />
</RelativeLayout>
You may want to take a look and consider whether you could make your layout work as a GridLayout. It's designed to handle situations where you want to arrange UI elements into a roughly grid-like pattern, while avoiding the performance issues that nested weights can cause. If your UI lends itself to this sort of layout, you can achieve both simpler implementation and faster performance by taking advantage of it.
GridLayout is supported as far back as API level 7, I believe, via the support library.