How to center a ImageView vertically in its parent?
My layout design is a TextView with long text and a ImageView to the right of the TextView. I want to center the ImageView in its parent vertically, so I used android:layout_centerVertical="true", but it turned out that the ImageView was aligned to the bottom of its parent. If I don't use layout_centerVertical property, the ImageView will be aligned to the top of its parent. How can I solve this problem? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#000000">
<ImageView android:id="@+id/t3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/arrow_right"
android:layout_alignParentRight="true" android:background="#ff0000"
android:layout_centerVertical="true" android:scaleType="centerInside" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This a very very very very very very very very very very very very very very very very long sentence"
android:textColor="#231ACC" android:layout_toLeftOf="@id/t3" />
</RelativeLayout>
Asked by: Agata321 | Posted: 25-01-2022
Answer 1
Try android:layout_gravity="center_vertical"
Answer 2
Add this xml attribute to the child you wish to center within it's parent:
android:layout_centerInParent="true"
This will center it vertically/horizontally.
Answered by: David985 | Posted: 26-02-2022Answer 3
change android:layout_height="wrap_content"
to android:layout_height="fill_parent"
of RelativeLayout and add android:layout_centerVertical="true"
to TextView, it will work like charm.
Answer 4
You could use a LinearLayout
with layout_width
and layout_height
set to fill_parent
and gravity set to vertical_center
.
Something like this:
<?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="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input_field"
android:gravity="left"
android:layout_weight="1"
android:text="Left Aligned"/>
<EditText
android:id="@+id/input_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:text="Right Aligned"/>
</LinearLayout>
</LinearLayout>
Answered by: Lenny734 | Posted: 26-02-2022
Answer 5
This will put your image to the vertical center of it's parent:
android:layout_gravity="center_vertical"
Demo:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:layout_width="48dip"
android:layout_height="48dip"
android:src="@drawable/ic_launcher"
android:contentDescription="Application icon"
android:layout_gravity="center_vertical" />
</LinearLayout>
Answered by: Miller886 | Posted: 26-02-2022
Answer 6
Simply add this:
Answered by: Thomas853 | Posted: 26-02-2022android:layout_gravity="center_vertical|center_horizontal"
Answer 7
I found this question while building in Android Studio 4.2.1 and the solution after some tinkering was:
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
this way, constraining to both sides automatically centers the widget and satisfies the need for constraints. The same is true when centering vertically, by using:
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Answered by: Kellan890 | Posted: 26-02-2022
Similar questions
android - How can I make a ListView fit between a Button and ImageView vertically
I am trying to create a ListView which fits between a Button and an ImageView.
I have a RelativeLayout which has a Button at the top and an ImageView at the bottom.
I want a ListView between them. This is what I have done.
I see my listView under the Button, but it is overlapping with the ImageView at the bottom.
So how can I make it so that the ListView above the ImageView?
<RelativeLayout xmln...
java - Align imageview vertically center in tablerow
I have created a dynamic table. The table is supposed to have a row in which there will be an imageview in the extreme left, a textview in the middle and a checkbox in the extreme right. All the controls are showing up. The textview and the checkbox is also vertically centered, but the inageview could not be centered, thus, the row looks bad. I want to put the imageview in a line with the textview and the checkbox....
android - Rotate ImageView vertically
I want to do flipping animation of Image View horizontally ( around x-axis ). I have done the clockwise and anti-clockwise animation. Here is the code I have used ...
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:duration="30...
Android Flip ImageView Vertically
I am trying to flip and ImageView vertically but it just won't work.
Java:
public static void flipImageVertically(final Bitmap bmp, final ImageView imageView) {
final Matrix matrix = new Matrix();
matrix.preScale(1.0f, -1.0f);
imageView.setImageBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true));
}
XML:
&l...
android - How to center ImageView with TextView vertically, yet making sure the TextView has enough space to be shown?
Background
This is a very simple question.
I want to have an ImageView and TextView centered, so that the TextView (and optionally more views with it) is below the ImageView, yet should always be shown (ImageView can scale down if needed, just keep aspect ratio).
The problem
For some reason, all of the ways I've thought about don't work. There is always a scenario (usually easy to reprod...
Why this Android view doesn't fill the screen vertically?
Having this XML view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/myScrollLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="@+id/widget29"
android:layout_width="fill_parent"
and...
android - How do I vertically align an item within a list using relative layout?
I am using a list view in Android 1.5 to show a list of images and text next to the image. I am trying to vertically center the text but the text is at the top of the row instead of centered. Below is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="fill_parent"
...
android - How to vertically align text in a button with a background drawable?
I had a simple button set up with a background image defined like
android:background="?attr/button"
where ?attr/button was a reference to a simple 9-patch png. Everything worked fine, text in the button was aligned correctly.
Then I needed to have a different background for a pressed state of the button. So I changed that to
android:background="@drawabl...
Is it possible to write vertically in a textview in android?
Let's say you have a normal TextView, with "Stackoverflow" written in it, Is it possible to rotate the TextView by -90°, to have the S at the bottom and the W at the top of the screen?
Of course I could write my text as an image, rotate it and use it that way, but I am interested in the text right now.
Thanks.
Custom Alert dialog not centered vertically on Android
I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().
I think this is the same problem as
android - Issue in Image scrolling vertically
I have to load the view in webview by executing its url,and the two relative layouts there in Linearlayout i hide one at the time showing url in webview,it have scroll on both vertical and horizontal,the problem i'm facing here is it
only scrolls horizontally only but i need to view it also in vertical also,
here the xml code for layout
<LinearLayout xmlns:android="http://schemas.android.com/apk...
xml - Android layout -- stack two TextViews vertically, inside a ListView Row
I started off with Fedor's ListView implementation. Here's the XML for my ListView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_...
android tab to show vertically at the left hand side of screen in landscape view
I know that it is possible to place the tabs at the top or bottom view in portrait mode. However when i moved the orientation to the landscape view, the tabs will be in a row horizontally at the top.
I saw that it is possible when i opened up the Calendar application in my android phone, because the tabs are vertical in horizontal view, at the left hand side too.
Is there any way to change it ...
layout - Android: vertically stretching buttons
I have an application in which there are two buttons side-by-side at the bottom (in a TableLayout inside a RelativeLayout). This looks fine on small screens, but on larger screens there is an ugly black space left at the bottom. I want to expand the buttons vertically to fill the space, but can't work out how!
I've included the code below (with the contents of the RelativeLayout removed for simplicity; the below be...
java - How to align text vertically?
Target: Android >= 1.6 on a pure Canvas.
Suppose I want to write a function that will draw a (width, height) large red rectangle and then draw a black Hello World text inside. I want the text to be visually in the center of the rectangle. So let's try:
void drawHelloRectangle(Canvas c, int topLeftX,
int topLeftY, int width, int height) {
Paint mPaint = new Paint();
// heigh...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android