I'm new on Android.
How can I align a textView with the center of an ImageView using a relative layout?
I have try the following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="15dp"
android:maxWidth="15dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/item_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/item_icon"
android:text="Counter" />
</RelativeLayout>
but the text is not aligned with the center of the picture.
Thanks!
Use layout_alignTop and layout_alignBottom to align the top and bottom of the textview to the imageview. Then make sure the text has center gravity. This should align it to the middle. ( I believe it should be the right gravity by default, but forcing it never hurts).
You just need to align your textview between the borders of your imageviews top and bottom. And also be sure to center your textview's gravity, so that it will just centered inside your imageview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="15dp"
android:maxWidth="15dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/item_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/item_icon"
android:layout_alignTop="#+id/item_icon"
android:gravity="center"
android:text="Counter" />
For a better approach, IMHO, add your imageview to your textview as a drawableLeft, so that it will represent much more to user.
Relative layout children can use:
android:layout_centerInParent="true"
Related
I have a simple TextView inside of a RelativeLayout.
I want to position end edge of TextView to center of the RelativeLayout as in image below
I found something like android:layout_toStartOf=...
Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf=...
/>
</RelativeLayout>
But this only aligns the end edge to start of something. (Can't be used for align end edge to CENTER of layout).
NOTE: There is already a similar question and answer here.
You need to have a widget in the RelativeLayout that you can use as a reference to position the TextView. An empty Space widget (0dp x 0dp) centered horizontally works well for this.
<RelativeLayout>
<!-- Empty space widget (0x0 dp) centered horizontally -->
<Space
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<!-- TextView to left of the spacer-->
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/spacer"
android:text="awesome text!"/>
</RelativeLayout>
You could create a dummy View centered within your RelativeLayout and then align the TextView left to it:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/center_anchor"
android:text="hello world" />
</RelativeLayout>
You may also have a look at the PercentRelativeLayout which supports percentage based dimensions and margins. That means you can specify a right margin of 50% for example.
You can do that using PercentRelativeLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/white"
android:text="TextView"
android:textColor="#android:color/black"
app:layout_marginRightPercent="50%" />
I am trying to center a TextView in a LinearLayout and it is centering horizontaly but not vertically.
below is my code
<LinearLayout
android:orientation="vertical"
android:layout_width="320dp"
android:layout_height="50dp"
android:background="#drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvExVin" />
</LinearLayout>
In the end I would like it to be centered vertically to the left of the Linearlayout, if someone could figure out how to do that, that would be great.
Thanks!
You have the gravity and layout_gravity reversed. You can have multiple values on the gravity attribute separated by "|". Try this:
<LinearLayout
android:orientation="vertical"
android:layout_width="320dp"
android:layout_height="50dp"
android:background="#drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvExVin" />
</LinearLayout>
For clarification:
android:layout_gravity="center_horizontal" will center the LinearLayout horizontally in its parent.
android:gravity="center_vertical|left" will center the TextView (and any other children) vertically inside the LinearLayout and align it to the left.
Try to change your gravity to
android:layout_gravity="center_vertical"
gravity is used to position the content inside your View and layout_gravity is used to position the View within its parent.
But I'm confused by "In the end I would like it to be centered vertically to the left of the Linearlayout". But from the way it sounds, my answer should give you what you want.
Also, unless there is a very good reason, you shouldn't need to use a LinearLayout, or any parent, for a single child. If you can give a better explanation of what you want or even a screenshot then we may be able to help with a better solution.
This will give you your desired effect
<LinearLayout
android:layout_width="320dp" <!-- removed orientation (horizontal by default) -->
android:layout_height="50dp"
android:background="#drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:layout_gravity="center_vertical" <!-- changed gravity here -->
android:layout_width="fill_parent"
android:layout_height="wrap_content" <!-- change to wrap_content -->
android:id="#+id/tvExVin" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AppLock Demo"
android:textSize="30dp" />
</LinearLayout>
my layout is very simple. i use a vertical relative layout which contains an imageview on top of 2 textviews (heading and text). In portrait it looks fine. However in landscape it happens, that the imageview will not fill out the whole width of the screen but the text below still does. that doesnt look good. is there a simply way i can make the text width as wide as the imageview above, so all views will have the same right boarder on any screen size?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:src="#drawable/picture" />
<TextView
style="#style/DescriptionTitle"
android:id="#+id/textView1"
android:layout_alignLeft="#id/imageView1"
android:layout_below="#id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/heading" />
<TextView
style="#style/DescriptionText"
android:id="#+id/textView2"
android:layout_alignLeft="#id/imageView1"
android:layout_below="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text" />
</RelativeLayout>
</ScrollView>
Add android:layout_alignRight="#id/imageView1" to your TextView. You may need to use android:layout_width="0dp"; also on the same TextView.
insted of using android:src="#drawable/picture"
use android:background="#drawable/picture" hope it will work for you
I has the following XML:
<RelativeLayout android:id="#+id/cover_box"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="#+id/cover"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="#+id/download" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/mark_download"
android:layout_centerInParent="true" android:layout_marginTop="90px" />
</RelativeLayout>
But it's look's like the marginTop is being ignored.
If you want the 2nd image to be 90dp under the center of the screen, you could replace it with a FrameLayout where you can control the padding to move your image downwards.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="90dp">
<ImageView android:id="#+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark_download"/>
</FrameLayout>
When you use center in parent the view is put directly in the center. The margin top will only come into play if an object is within 90px of the top of your view. Thus pushing the centered view down to keep at least 90px of space on top of it. So it isn't being ignored but it is not having the effect that you think it should have.
I want the progressbar to shown under android:layout_centerInParent="true" so i have added a dummy TextView and set it to centerInParent .Then i put my progressbar under it. Now you can increase its distance from center in two ways . First by increasing marginTop in TextView and second increasing TextView height.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash" >
<FrameLayout
android:id="#+id/splash_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.s3.tdd.interfaces.CircularProgressBar
android:id="#+id/circularprogressbar2"
style="#style/Widget.ProgressBar.Holo.CircularProgressBar"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_below="#+id/txt1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can put your imageview inside of other ViewGroup (LinearLayout of RelativeLayout layout), leaving the margin of imageview, and doing android:centerInParent="true" for the ViewGroup:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:centerInParent="true">
<ImageView android:id="#+id/download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/mark_download" android:layout_marginTop="90px" />
</LinearLayout>
</RelativeLayout>
You can make a dummy view and center it to parent. Now align your view relative to dummy view using layout:alignComponent and give the marginTop. Now in the code move it according to the width of your view to center it.
What I'm trying to do is have a TextView, and to the right of it an editText that takes up the rest of the space. The textView should be centered vertically along the editText. Now I can do this using an linearlayout, however I would prefer to do it in a relativelayout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login:"
android:paddingRight="20dp" />
<EditText
android:id="#+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/loginText" />
</RelativeLayout>
What you want to do is possible by using the android:layout_alignBottom and android:layout_alignTop properties provided by the RelativeLayout to its children and the android:gravity property of the TextView.
These properties allow you to set the bottom/top edge of a view to match the bottom/top edge of another view. By applying them to the TextView you can effectively make the TextView match the height of the EditText control. Then, with the gravity property you can make the text in the TextView center vertically inside the TextView control.
The following example should work as you intended:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<TextView android:id="#+id/user_name_label"
android:text="#string/user_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="#+id/user_name_text"
android:layout_alignTop="#id/user_name_text"
android:layout_marginRight="5dp"
android:gravity="center_vertical" />
<EditText android:id="#id/user_name_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/user_name_label" />
</RelativeLayout>
add android:layout_gravity="center_vertical" to your Textview
this would place the textview centered vertically in the parent