I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding
android:layout_gravity="right"
but this doesn't seem to work. any other suggestions please?
You should use android:gravity="right". layout_gravity is for the view (EditText) alignment against the container.
You may use android:layout_alignParentRight="true" to align the EditText's right edge to its parent's right edge. Also, if you really want to use the layout_gravity or gravity attributes, check out this article that discusses the proper use of them.
You can jst set property for edit text in property window i.e. Gravity to right or by adding code of lin e in xml file of UI : android:gravity="right"
layout_gravity means that you are specifying the layouts attribute, not the element inside. In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not elements) are the same..
use layout width and height as wrap content inside relative layout :
<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="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:paddingEnd="16dp"
android:paddingStart="8dp"
tools:src="#mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:paddingRight="16dp"
android:paddingLeft="8dp"
android:contentDescription="#string/todo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/icon"
android:maxLines="1"
android:lines="1"
android:paddingTop="8dp"
tools:text="Google"
android:layout_toRightOf="#id/icon" />
<TextView
android:id="#+id/last_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="8dp"
tools:text="Last used: yesterday at 18.54" />
</RelativeLayout>
Related
I'm trying to create a simple component in my layout, where there are two TextViews horizontally next to each other. The one on the right should start where the one on the left finishes. My code for this is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="25sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
I programmatically set the text on each TextView after the view has rendered. However, sometimes the text does not display correctly in the first TextView- I can see that the width has been set correctly, as the second TextView is not next to it, but the text is truncated rather than using the space. If I lock/unlock the device to refresh the screen then the text displays correctly (without the widths of the TextViews changing).
I've tried changing this to use a RelativeLayout, but I see the same issue.
Any ideas?
Although i dont understant what exactly you mean, would suggest you to use weightSum property in the parent view and android:layout_weight in child views. The same allows to put many child views inside a parent view with respect to ratio (like navigation tabs).
for eg :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="25sp"
android:layout_weight="0.4" /> //60% width
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_weight="0.6" /> //40% width
</LinearLayout>
also, dont forget to put the width if child views as 0dp. as that will result in ignoring the calculations regarding the width of view. or you can set the width of child view as "match_parent" as well. any other property to width will not work. (and if you want half matchparent for both child views set layout_width to 0.5 both views.. ithink thats obvious to note)
Hopw it helps.
i want to set text alignment to right when my TextView is set to layout_weight
my application must be support old versions of android such as Android 2.2. this below layout does not set correctly alignment to right TextView:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/loading"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".42"/>
<TextView
android:id="#+id/response_text"
android:layout_width="0dp"
android:layout_height="43dp"
android:layout_weight="6"
android:textSize="18sp"
android:layout_gravity="right"
android:text="#string/please_wait_to_response_from_server"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".42"/>
</LinearLayout>
and i'm checking this ways: set layout_width to fill_parent, match_parent, wrap_contect
Using "weight" and "layout_gravity" won't work 'cause you are telling the layout 2 different things. If you want your text to be right aligned, you can use "gravity" instead of "layout_gravity".
android:gravity="right"
Hope it helps.
I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!
I have the following XML:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/verylongtext" android:layout_weight="1000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sometext" android:onClick="onClickButton" android:layout_weight="1" />
</LinearLayout>
Though I have set a layout_weight there is still a linebreak in the text on the button, the button should show
sometext
but it shows
somet
ext
Why is this and how can I fix it?
Thanks!
Include android:singleLine="true" to Button xml.
You don't specify the orientation of your LinearLayout, so it is defaulting to horizontal. The TextView is squeezing the Button. Also, when you use android:layout_weight, then you often don't want to constrain the objects in the layout in the relevant dimension, so you can put "0px" instead of "wrap_content" or "match_parent" and let the weight determine the respective proportional sizes.
I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.
how can I do that? I tried that, but both 'textview' and image view at left aligned.
<TextView
android:id="#+id/uptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"/>
<TextView
android:id="#+id/downtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="right|bottom"/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|bottom"/>
</LinearLayout>
Don't use a LinearLayout. Use a RelativeLayout, with
your first TextView set with android:layout_alignParentLeft="true"
your second TextView set with android:layout_alignParentBottom="true" and android:layout_alignParentRight="true"
something similar for your ImageView, which presently looks like it is going to overlap the second TextView
I realize this post is a bit old but just in case someone comes across this in their search for clarity;
The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">
<TextView
android:layout_width="40dp"
android:layout_height="20dp"
android:text="test"/></LinearLayout>
It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.
Hope this helps.
Using RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hi"
android:textStyle="bold|italic"
android:gravity="right"/>
<TextView
android:id="#+id/text2"
android:layout_below="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All"
android:gravity="right"/>
</RelativeLayout>