How can I center two TextViews vertically within a Layout - android

I have a RelativeLayout (that I am not married to) and two TextViews. I would like the bottom of the first TextView to line up with the center of it's parent, and the top of the second TextView to line up with the center of it's parent.
I put a green line in the picture above at the (vertical) center of the parent. The TextViews are currently in exactly the right spot, but I did this as I will describe below with margin's and aligning them to the parent and knowing the parent's size.
For various reasons I can't use just one TextView and center it. Also I don't know the height of the parent, so I can't align each to the top/bottom of the parent and margin them down/up to center them.
I also tried having another View that takes up half the height and be invisible, but layout_height doesn't take percents (or at least it gave me errors).
Any ideas how I can accomplish this?
Should I just put the two TextViews inside another layout and have that center itself?

Create a 0-height view that is centered in its parent and serves as an anchor. (Use the attribute android:layout_centerVertical="true" for this.) Position one text view above the anchor and the other below the anchor.

I've solved this problem before by using a vertical LinearLayout with the two TextViews inside it.
ps.: the Android Design Guidelines http://developer.android.com/design/patterns/pure-android.html specific points out to Don't use right-pointing carets on line items. It might not be a line item that you're doing, but in case it is, just to point it out.

Related

Make middle View not push other view outside of bounds

I have multiple TextViews inside a horizontal LinearLayout and i need the middle text view to have ellipse=middle so that when the middle text is long enough, it pushes on both sides but the other views don't go out of bounds, but instead the middle TextView shows the '..."
Here's how it should look.
Setting the items normally, wrap_content for all in a horizontal LinearLayout will make the at ASAP text be pushed outside of the screen on Android (the above screens are from the iOS app).
Any ideas on how to accomplish this? Perhaps with a ConstraintLayout somehow ?!
Yes, i would recommend a ConstraintLayout. Top item to the top of the view, bottom item to thr bottom of the view then the middle item attached to these two views. You could also use barriers.

What is the best practise for padding or margin in Android?

I am relatively new to Android UI. I always get confuse in providing margin to different view like should i provide bottom margin or should i use top margin(to view below it). Also should i use RelativeLayout or LinearLayout if both can solve my problem.
Thanks
It Depends on your need
Linear Vs Relative
If you just want to stack your TextView and Button horizontally or vertically you should go with LinearLayout.
If you want to make a layout that is more complex for example you can have an ImageView covering all of the screen and Button over the ImageView or if you want your layout elements to be stack on corners or at bottom, RelativeLayout is your Guy.
Top margin vs Bottom Margin
It doesn't make much a difference its a personal preference, I Use margin-bottom on first element rather than margin-top for second element.
One noticeable difference is when you are working with Show layout bounds during development. You can see here those pink coloration indicate that it is using margin on its view while padding has no coloration. Recently I prefer to use padding if applicable with my requirements as it seems more cleaner to inspect UI when Show layout bounds is enabled from Developer option.
The image is not mine and was just use as a quick sample.
If both Linear and RelativeLayout will solve your layout, then you should use Linear as it is faster to render.
With regards to top vs. bottom Margin. That's entirely your preference and how you want to think about the elements. Does Item A always sit 40dp above the next item or does Item B always sit 40dp below the previous item?

Center view to another in RelativeLayout

I have two views that I want to center vertically in a RelativeLayout.
Is there a way to do this without using gravity/layout_gravity ?
My problem behind this question :
I need to do a layout with several squares and under each one a TextView. The main problem is that the TextViews must overlap. Only one TextView will be visible at a time. Each TextView has a different lengh.
I started with a RelativeLayout but encountred the previous problem. And I can't group the views 2 per 2 because I need to set a layout_toRightOf of the previous square.
Current layout :
For the moment, I have set a magic number in layout_marginLeft for each square (to the border of the parent view) but it isn't clean at all.
Thanks
Use android:layout_centerInParent="true" to center something into a RelativeLayout
You can use android:layout_below="#id/your_first_view" to put your second View below the first !
If you want to do more complex stuff you may separate your different Views and store the into new LinearLayout that you set to horizontal or vertical depending on your needs.
Another trick can be to create empty Views with small height or width and that can help you to position thing around them !
This combined to the toRightOf toLeftOf stuff will do what you want
Finaly, I kept the the layout_marginLeft but I put the values in my res/values/dimens.xml, it is cleaner and I can have a dimens.xml per screen dimension.
A trick is to set the width of the textViews deliberately big so it won't depend on the strings lenght.

(Android) How to put a TextView int the Center of the screen (Not in the Layout or Parent View) if I have a Horizontal Scroll?

This is my problem: I have a (horizontally) very large View and, of course, I have a scroll to move it side by side. I want to put a TextView in middle, but I want it so, that when I scroll horizontally and the View moves this TextView stays in the center of the screen.
If I use android:gravity="center" or something like this, as the view is very large, I will NOT see the TextView in the center unless I am in the center of the view (Not in the beginning, and not in the end) but I need that the TextView is in the center during the whole scrolling.
I think that you can achieve that by positioning the element in the middle by setting its x-origin (with left-padding or left-margin?) to (scrollview.width/2 - textview.width/2), then add the scrollview.offset.x whenever the scrollview scrolls (add a listener to get this) so it maintains still at the relative center.
However the simplest thing may be to place a FrameLayout which contains the ScrollView and above it the TextView, which you could then center with the gravity property.
Presumably your content is all inside a ScrollView. You will need your TextView to be outside of this scroll view: you can then use the technique described in "Android overlay a view ontop of everything?" to place the TextView where you want it.

Method to allow a single child view to overflow the viewgroup bounds?

The following screenshot illustrates a simple example of what I have now:
What I'd like to achieve, is that the selected (blue) view not be clipped at the boundary of the red container. My first try was clipChildren="false", which causes the blue view to expand outside of its borders, filling the the red area. I just want to see the portion overlaying the green area.
I think you'll have to float the blue on top of both the red and green. You can't have a child outside of its parent ViewGroup (AFAIK). You'll need to redesign your layout.
Getting what you want should be pretty easy, though. I don't use the graphical designer, so would need XML.
FrameLayout with LinearLayout inside to show the Red/Green, then another Linear or Relative after the first LinearyLayout (inside the FrameLayout). With LinearLayout, I'd align right, and give the blue element some padding.
It may be possible to do this all with RelativeLayout, but I tend to stay away from it.
Essentially what you're looking for is overlapping views. This can be done with a FrameLayout. For information on how to do this, please checkout this example.

Categories

Resources