Extending margins on larger Screens - android

Is there a way in android to make the margins extend to fill screens? The problem is that layouts I build for Nexus devices look great, but then when previewing on a regular device without the bottom controls there is an ugly space between the rest of the layout and the bottom. I would like the margins between items to increase when there is available space.
Pictures are added below. Sorry for the ugly cutting of some of the fields, I'm unable to show them at this time to due a contract. Notice how "advacned search" is far from the bottom, I would like the vertical margins between all items to increase and make sure this doesnt happen.
How can I acheive this in a relative layout?

Try using LinearLayout and empty Views between each of the items.
just empty View:
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/transparent" />

Related

why the upper part of recyclerview item is erased?

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

How do I properly align ImageViews on top of one another?

I'm currently trying to make a simple tic-tac-toe game, consisting of ImageViews on top of the board (also an ImageView). However, when I run the app and click on a tile, the picture appears out of place, even though it seems I had lined up the ImageViews correctly.
What I did was I set each original tile to a blank square, and then when the user clicks on a tile, it would change to either "X" or "O," depending on whose turn it was.
I'm pretty sure the problem has to do with dpi scaling on different devices, but I'm not quite sure how to fix it. Any help would be greatly appreciated!
I've included an image of my problem below:
Instead of using an image to display the grid, I would suggest to use XML views.
Horizontal line:
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#android:color/black"/>
Vertical Line:
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#android:color/black"/>
And constraint them between the images.

Problems with backgrounds and layers (Android Studio)

I have a problem with the background of a View in this project.
There is one View with a transparent layer, which is not always displayed... It looks like if, in some cases, the height of this View was automatically set to 0, as I can see in the preview of the activity that contains the View. The code of this element is as follows:
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:alpha="0.5"
android:background="#drawable/background_layers" />
The main view group in this activity is a ScrollView. If the whole layout is displayed and is visible on the screen (so you don't need to scroll down) everything works fine (look at the transparent layer, which is the topic of this question):
But if the screen density is low, or if the device is in portrait orientation, and I must use the scroll because the full layout is not displayed at the same time... the previous View is not shown, the transparent layer is not visible :(
It happens when the app is running and also in Android Studio, in the preview screen, as I said. Here is an example with vertical orientation and low screen density, as seen on the preview tab of the activity in Android Studio:
Any idea to fix this isue?
Thanks in advance.
David.
I think I found the problem with your layout. The transparent_background view sometimes takes height 0 because its height is match_parent and its parent (the main_relative_layout view) has its height set as wrap_content. You should set the transparent_background dimensions relatively to another view, the LinearLayout that wraps the content maybe, using android:layout_alignTop, android:layout_alignBottom, android:layout_alignLeft and android:layout_alignRight attributes. You may need to remove the LinearLayout margin and use padding instead to align the transparent_background view correctly, let me know if you need some help.

Create a layout based in percentages

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.

Android Layout & overlapping issue

The image is what i'm trying to achieve. I tried using tab layout, relativelayout and linearlayout (and even a combination), but in no scenario i got it exactly right.
The requirements:
no overlapping of any component
no clipping of the image in the button (the green one)
the red bar is always at the bottom
in landscape, the components should still be in their same place, but you can scroll down
If required I can paste my current variations of the different layouts, but they are all rather big and messy.
Professional insights on which layout approach you would use is also appreciated, at least i can keep trying using the right things then.
At the moment, the orange things are normal buttons, defined like so:
<Button
android:id="#+id/btn_web"
style="#style/NewButton2"
android:onClick="webClick"
android:text="#string/Text_Web"
android:drawableTop="#drawable/jc_menu_web"
android:layout_weight="0.3"
/>
This is a problem on its own,because if you stretch the buttons using fill_parent, the result is that the image will be high at the top, whereas the text will be entirely at the bottom.Would be nicer if it were a centered image in the button.

Categories

Resources