I have guideline 60% from top and LinearLayout with bottom constrain to that guideline (so my LinearLayout will 60% of screen). Now Im need to put half of CardView to the bottom of that LinearLayout (screenshot added).
CardView height is wrap_content, cause will be added dynamically so I dont know height and cannot use something like margin bottom -50dp.
Thats what I have:
I need something like that:
I cannot find future like constrain by center. Could you help me, please.
You just need to set the top anchor of cardview to be connected to the bottom anchor of the linear layout (or to the guideline), and do the same for the bottom anchor. With both top and bottom anchors pointing to the same target, the cardview will be centered vertically on that target. Also, you might not need that linearlayout, depending on what you do. Check the chains functionality in ConstraintLayout.
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="200dp"
tools:layout_editor_absoluteX="28dp"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline"
tools:layout_editor_absoluteY="156dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
The eaiest way is to do this in code at runtime. Wait until the height has been calculated, then add a top margin of -.5*height.
Related
For card elevation to be visible, I use the attribute card_view:cardUseCompatPadding="true" in cardview XML. It added extra padding on all four sides of the cardview, but I do not need the bottom padding since the card below every card will already have a top padding. So currently it is like double padding between two cards because of top+bottom padding. How to reduce it by half?
P.S android:layout_marginBottom="-5dp" doesn't seem to work
Only use a margin at the bottom for the cards and set a padding to the top of your RecyclerView/ListView holding the cards:
<android.support.v7.widget.RecyclerView
...
android:paddingTop="5dp"
android:clipToPadding="false" />
My layout is the following:
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/pollenType"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/get_pollen_index"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="51dp"
android:textSize="40sp" />
As you can tell, Philadelphia, PA is left-aligned. No matter what I change, whether it's centering horizontal and vertical to true, it stays left-aligned.
I have tried changing the layout via the XML and graphical user interface. In the graphical user interface, it indeed is "centered", but it remains non-centered. My layout positions are consistent across all my textviews, so I am unsure as to why this particular TextView is not centered.
Two options...
One: Give the TextView android:layout_width="wrap_content"
Two: Keep the android:layout_width="match_parent" and use android:gravity="center_horizontal"
layout_gravity defines gravity of the view within its parent. Your textView already takes the full width because of match_parent, so centering it horizontally inside its parent does nothing.
gravity, on the other hand, defines the gravity of its contents.
You can center the text using the gravity xml attributes :
android:gravity="center"
Your width equals match_parent and therefore the edges of your TextView already touch the edges of the parent view. You should rather center the text in the TextView with android:gravity="center".
You're telling the display_city TextView to be as wide as the screen.
android:layout_width="match_parent"
However, its text is left aligned by default. That's why it looks this way.
You have two alternatives for what you want:
Keep the width as match_parent and center the text inside the TextView (with gravity).
Set the width to wrap_content and center the View inside its parent (with layout_gravity).
I have this ImageView
<ImageView
android:id="#+id/img_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/my_img_1" />
It is the child of a RelativeLayout. I am expecting the image to appear flushed with the upper-left corner of the page/parent. But it's not. There is about 20dp margin/padding between the parent's left and top and the child view. How do I get what I want?
What you expect is what should happen.
It sounds like the holding RelativeLayout has 20dp padding on it - this would lead to the behaviour you describe.
Make sure you've removed the padding from your parent RelativeLayout!
I'm doing a menu. The problem is that my buttons are too close to one another. I would like to separate them a bit.
Also I would like to extend them (rozszerzyć je) to the similar sizes.
Here you have the code:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
Here you have the image of this problem:
And each button out of the 4 ones has a similar code. (the difference is in layout_above.
How to make it?
Thanks in advance!
Any reason why you have to use a RelativeLayout? If you can use a LinearLayout I'd use something like:
<LinearLayout
android:layout_width="..."
android:layout_height="..."
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding_top="4dp"
android:padding_bottom="4dp"
... />
</LinearLayout>
This would get all your buttons to be the same size horizontal and vertical. You can tweak the padding top and bottom to get the desired effect.
The way layout_weight works is it takes the leftover space of the parent view, and portions them out to the children views according to their weights. Since the height of every button is 0dp, 100% of the vertical space is left to partition out. Since the weight of all the buttons is the same, they will be roughly the same size.
This explanation is for a vertical LinearLayout. For a horizontal, just switch the values of layout_height and layout_width.
I am having a spot of bother with a fairly simple layout. Here it is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
3 Questions:
How do I center vertical both TextViews (or perhaps better said, the text within those views) within the parent LinearLayout? The left view is vertically centered OK, but the right one (because it has a smaller font) is not. It seems to be centered vertically at the top. I obviously tried playing with layout_gravity of the second view but that makes no difference whatsoever. The only way I can solve it is to wrap the second TextView in a LinearLayout with its layout height parameter set to match_parent (but is this the correct way of doing this?)
Similarly, I want the View on the left horizontally centered on the left, and the View on the right horizontally centered on the right. Currently the right View is placed immediatly next to the left one. The only way I can solve this is by adding something like this inbetween the two text views:
<TextView
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="">
basically acting as a spacer which decreases in size depending on the legth of the text in both TextViews
I want the text in the left View to be truncated if the combined text of the Views does not fit horizontally. No wrapping onto a new line. Currently the text in the left View simply "pushes" the right one out of the parent. No idea how to achieve that (apart from adding android:maxLines="1" to stop the text from wrapping). I have tried android:ellipsize="end" but that does not seem to have any effect.
Best way is to use Relative layout , but still if you want to do the same thing in Linear layout than do some changes in your xml file
-First is set Linear layout hight as match parent :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizonatal">
-Second for making the views visible at centre vertical do
android:layout_gravity="center_vertical"
same property android:layout_gravity = "center_horizontal" , you have to add in second text view also.
It will make your both text view appear at centre vertical but one next to other.
To make the second view appear on right I think you can add
android:layout_marginLeft="xx dp"
put some value in place of xx.
For your third question about truncating your text, you should give some size to your TextView not wrap content..Like android:layout_width ="25dp"
and then use android:ellipsize="end".
I guess you will get that..Actually I am in hurry,time to leave the office.