I want to align my text at the bottom left in android.
If you want to align the text in your TextView to the bottom left, use android:gravity="bottom|left". If you want to align the TextView to the bottom left of it's container, use android:layout_gravity="bottom|left".
I suspect that you're mis-communicating your true intentions, here, though. You want to align some text to the bottom left of what? The screen? The text's container?
Please try to be more specific with your question.
Bottom left of the screen:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="bottom|left"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
</FrameLayout>
I would recommend a Relative layout.
Your view would look like this:
<?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="fill_parent">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text aligned bottom left"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
Must be written inside your TextView
Related
I have no idea on how to achieve this layout:
So basically I want a title bar, bellow that I want a listview, while anchored to the bottom I want a button in the right side of the screen.
Right now I am trying with this code (unsuccessfully)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="436dp"
android:id="#+id/listView"
android:layout_gravity="right" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="ok"
android:layout_weight="0.45" />
</LinearLayout>
</LinearLayout>
By using this layout, my listview does not resize when changing from portrait to landscape so my bottom button goes out of screen. Also the bottom button is not aligned to the right side.
Any idea on how to achieve this?
So I need 2 "bars" one anchored to the top, and one anchored to the bottom. On the top "bar" I want a TextView, and on the bottom "bar" I want a button aligned to the right.
And I want the listView to fill the space between the 2 bars.
So there may be empty space in the middle, when listview has just 1-2-3 items (just like in the picture).
Can this be done?
Please advise...
Change your top layout to a relatuve layout, then place the button outside your linearlayout and inside your relative layout. Then set gravity layout_gravity to bottom||right
U can also drag t where u want it in the visual part of the xml layout
Change your top layout to a relatuve layout, then place the button outside your linearlayout and inside your relative layout. Then set gravity of the button tolayout_gravity to bottom||right
U can also drag t where u want it in the visual part of the xml layout
How can I align a child view in the horizontal LinearLayout?
I have two textviews (width height is wrap_content) in the horizontal manager. How I can align one to right and the other one to the left?
Without knowing what you mean by "horizontal manager" it's really hard to answer your question. Do you mean a horizontal layout? Is this what you're looking for?
<?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="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
I have a listview with custom rows. In the row there are these items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:focusable="false"
android:scaleType="fitXY">
</ImageView>"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
<TextView android:id="#+id/textView1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:maxLines="1"
android:ellipsize="end">"
</TextView>
<TextView android:id="#+id/textView2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:maxLines="1"
android:ellipsize="end">
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll2"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="10dp">"
<TextView android:id="#+id/textView3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingBottom="5dp"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:background="#87EB81">
</TextView>
</LinearLayout>
</LinearLayout>
If both textviews has some content, they are centered vertically in the row (almost).
But if the lower textview is empty, I call:
holder.textView2.setVisibility(View.INVISIBLE);
holder.textView.setGravity(Gravity.CENTER_VERTICAL);
Still, the upper textview gets into the top of the row. Why?
If I set it to View.GONE, I get this, but I dont want the height of the row to change:
Use android:layout_weight to set all the childs of the main LinearLayout to mesure the same.
Use a LinearLayout as parent for textView1 and textView2. Use Use android:layout_weight to have the same size for each one.
Define TextView one with android:gravity="center_vertical|left" Then, if you don't want to show the second TextView, set its visibility as View.GONE.
Maybe this will resolve your problem,
Update:
In a RelativeLayout, views are aligned with their parent, with the RelativeLayout itself, or with other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called layout_alignWithParentIfMissing.
This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.
For more look at Layout Tricks: Creating Efficient Layouts
This can be possible in your RelativeLayout by setting up the layout_below and layout_above with the help of parent alignment by using layout_centerInParent and set the value true of layout_centerHorizontal
Hope, this will work.
Try to use:
<TextView
...
android:layout_height="?android:attr/listPreferredItemHeight"
...
/>
in your_list_item.xml. And use View.GONE
Try to fix some value to your RelativeLayout's height like-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
To Solve for your problem what u can do is have the LinearLayout fill Parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
And use View.GONE This will make the Row Height Same.
If you see a gap after Best Car in World then try using Relative Layout and align last Layout to bottom.
I have a button and a expand list above this button , when the list is expanded the button below it disappears . how to make this button in fixed location?
<?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">
<include layout="#layout/ads_bar" ></include>
<TextView
android:text="Level 1:"
android:textSize="20dp"
android:id="#+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<TextView
android:text="lorem ispum something about this level ..... :)"
android:textSize="16dp"
android:id="#+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<ExpandableListView
android:id="#+id/listView"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ExpandableListView>
<include
android:layout_alignParentBottom="true"
layout="#layout/quick_links_bar"></include>
</LinearLayout>
where quick_links_bar represents button !
To acheve button aligned to the bottom it's better to use layout_weight attribute of the linear layout.
Simple example:
<LinearLayout>
<TextView layout_weight=1/>
<Button layout_height="wrap_content"/>
<LinearLayout>
This is better than using RElative layout because if using one you will have the bottom of your text cut off by the button, but in this case it will not happen.
Sounds like you want the quick_links_bar layout to always stay at the bottom? You'll need to use RelativeLayout for that.
Wrap your whole layout in a RelativeLayout and move your quick links bar layout outside of the LinearLayout and set the property android:layout_alignParentBottom="true" on it. For this next bit, you'll need to assign an id to your quick links bar. On your LinearLayout, set the property android:layout_above="#id/quicklinksbarid". Make sure your quick links bar include statement is above the LinearLayout.
I have a LinearLayout, which only contains one button. I want this button to be centered vertically and aligned to the right. I tried many ways, but I couldn't make this button centered vertically. It is always aligned to the top. I also tried to put a button in RelativeLayout, the button can not be centered vertically either.
The XML is as below. Is there anything wrong with this layout? Thanks.
<?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="wrap_content"
android:background="#E8E3E4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More"
android:layout_gravity="right" />
</LinearLayout>
Changing android:layout_gravity="right" to android:layout_gravity="right|center_vertical" didn't resolve the problem in my question.
You say in words that you want this to be centered vertically, but you have not said in XML that you want this to be centered vertically. You will need to adjust your android:layout_gravity attribute to specify both right and center_vertical.
However, I would recommend you go back to RelativeLayout. Use android:layout_centerVertical="true" and android:layout_alignParentRight="true" to make the button be centered vertically and right-aligned.
Also, bear in mind that your current LinearLayout has android:layout_height="wrap_content", which means there is nothing to be centered inside. You need the container to have more space than its contents (e.g., fill_parent) if you want centering to have any meaning.
This code will put the button in vertical center and on the right screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Click Me" />
</LinearLayout>
Try adding android:gravity="center" to your LinearLayout. I remember having read somewhere that that might do the trick.