TextView and ImageView do not top-align at the same level - android

I have a very simply horizontal LinearLayout with one ImageView and one TextView and both layout_gravity is set to top. Both views do indeed align at the top of the layout, but the TextView is always slightly lower, and to get to align with the ImageView I have to give it a negative layout_marginTop.
Am I missing something here?
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.67"
android:layout_gravity="top"
android:layout_margin="16dp"
android:textSize="16sp"
android:text="This is some text\nwith several\nline breaks"/>
<ImageView
android:id="#+id/card_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_gravity="top"
android:layout_margin="16dp"
android:src="#drawable/placeholder"/>
</LinearLayout>
Result

Instead of android:layout_height="wrap_content"for textview try with android:layout_height="match_parent" and set its gravity to top

Related

TextView gravity bottom not working

I have a simple LinearLayout and I try to make it looks like this:
Instead it looks like:
My xml is:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="miasto"
android:textSize="10sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="ulica"
android:textSize="15sp"
android:gravity="bottom"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="22"
android:gravity="bottom"/>
</LinearLayout>
Why gravity="bottom" doesn't work?
You set this text view a width of wrap_content it means, what ever the text is, the view take the size of the text and for LinearLayout , the default gravity is center
Thus, Change
android:layout_height="wrap_content"
to
android:layout_height="match_parent"
Meanwhile, just for your information, gravity is the way the text will align itself in the TextView. The TextView being in wrap_content does nothing, as the TextView is exactly the size of the text.
layout_gravity is the way the TextView will align itself in its parent, in your case in the LinearLayout.
Try to make your textview height match_parent.

android gridlayout column width exceed parent (gridlayout) width

I am using gridlayout to group my views. inside it, I have two columns. one column has a child which is relativelayout and the other column has a child which is linearlayout.
My linearlayout orientation is vertical. it has two views, textview and an imageview.
The issue is when I type text in the textview, and the text is long, I cannot see
some characters in the textview
imageview which is below the textview
textview width is extending and and hiding some of it's characters and also imageview before entering 2nd line(textview is muitiline)
I don't want to use margin to solve this issue because if I use it, and the text in textview is short, unwanted space will appear at the right side of textview.
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="1" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill" >
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12345...50" >
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/vladmir putin"/>
</LinearLayout>
</android.support.v7.widget.GridLayout>
How can i solve the issue of textview width exceeding gridlayout width?
if i type 1234 everything is ok. but if i type from 1 to 50, i can't see some numbers like from 30 to 32 and imageview?.
To solve this problem, I used RelativeLayout instead of LinearLayout and aligned my textview to the start of the imageview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="1"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill" >
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/profile"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="12345...50"
android:layout_toStartOf="#id/profile">
</TextView>
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="#drawable/profile"
android:id="#+id/profile"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>

Align text with picture in Android

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"

Creating custom gridView item, using xml

I'm using the following RelativeLayout for creating an item in GridView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#drawable/border">
<ImageView
android:id="#+id/icon_service"
android:layout_width="290dp"
android:layout_height="268dp"
android:layout_alignParentTop="true"
android:src="#drawable/icon"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="My text"
android:layout_height="80dp"
android:id="#+id/service"
android:layout_width="wrap_content"
android:layout_below="#+id/icon_service"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:gravity="top|center"
android:textSize="24sp"
android:textColor="#ff0000"
android:ellipsize="marquee">
</TextView>
</RelativeLayout>
which gives me this:
Instead, what I want is this:
Your Image view is at right position,
Below, instead of putting textview directly..
put a linearlayout their with orientation horizontal,
and make two childs of this LinearLayout, TextView and ImageView...
thats it..
You are providing only one ImageView and expecting for two imageviews see your code..for achiving your desired result take one linear layout ,give it wightsum = 10 and orientation vertical and take one Image and another linerlayout inside this.give weight for imageview 7 and linearlayout 3..then the set the second one linearlayout weightsum equal to 6 and take one textview and one imageview inside the second layout and set textview weight equal to 4 and and imageview equal to 2..see the result if not equal to your expecting...customize the weightsums...like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:weightSum="6"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>

RelativeLayout - CenterInParent and marginTop

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.

Categories

Resources