Center text vertically independently of View height? - android

I'm doing my best to center the text vertically but I cannot manage to do it. The problem is that I have rows of buttons with height=fill_parent and weight=1 and as the rows become smaller my text starts touching the bottom of the view as seen here:
I tried removing the padding, margin, changing the height and so on. But nothing seems to do it.
How can I align it vertically even when the text size is close the the view height?
Here's the code for the view containing number 5:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:padding="0dp" />

As per my advice if you have two options to make such layout and getout from the issue you are having.
1. Use GridView
Using gridview you will have equal space in all four directions(left, right, top, bottom). You dont have to worry about the equal spacing for the grid item.
<GridView
android:id="#+id/album_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:drawSelectorOnTop="true"/>
Just try above code with which you will have all view equally distributed in the grid.
2. Use TableLayout and TableRow
Please check below code to use TableLayout and TableRow with its attribute to have all view equally arranged. Even if you smaller the height and width of the TableLayout, it will remain equally arranged in that view.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="5dp">
<TableRow android:weightSum="3"
android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
<TableRow android:weightSum="3" android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
<TableRow android:weightSum="3" android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
</TableLayout>
Please find output of this TableLayout.
Let me know if this issue is resolved. If not, I will be happy to help you again.
Enjoy Coding... :)

If this button is inside a linearlayout with vertical orientation. Since you define the weight, you want to make the height to be "0dp" instead of "fill_parent".
To make the text center vertical. Did you try android:gravity:"center_vertical"?

Try this
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/dialer_title"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn1"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_1"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_1_no_vm" />
<ImageButton
android:id="#+id/btn2"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_2"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_2" />
<ImageButton
android:id="#+id/btn3"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_3"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn4"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_4"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_4" />
<ImageButton
android:id="#+id/btn5"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_5"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_5" />
<ImageButton
android:id="#+id/btn6"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_6"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn7"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_7"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_7" />
<ImageButton
android:id="#+id/btn8"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_8"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_8" />
<ImageButton
android:id="#+id/btn9"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_9"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_9" />
</LinearLayout>
<LinearLayout
style="#style/DialPadRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btnStar"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_astr"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_star" />
<ImageButton
android:id="#+id/btn0"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_0"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_0" />
<ImageButton
android:id="#+id/btnHash"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_hash"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_hash" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btnContacts"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/string_menu_contacts"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/selector_dial_contact_b" />
<ImageButton
android:id="#+id/btnCall"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/string_menu_call"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/dial_num_call" />
<ImageButton
android:id="#+id/btnDelete"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_backspace"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/dial_num_delete" />
</LinearLayout>
</LinearLayout>
This will create phone dealer with equal button size.
For style="#style/DialPadButton" create style with name DialPadButton and customize buttons as you want. In my case I have added item name="android:layout_margin" with value 5dp.

design this using a grid view you will have some more functionality. else take one Linear Layout inside that set another three linear layout orientation horizontal and weight sum= 3 ,put each button on the child layout and give layout weight =1 for each button.

It does not mean that the text "5" is not centered. The text "5" appears slightly below center because the button is not given enough space to layout its text content. Even though it is provided the adequate height, the text "5" will appear slightly above center. This is because, there are characters('y', 'g', etc) with larger descent than "5" (maximum possible ascent and descent are considered during layouting the text even tough your text contains no taller glyph or glyph with non zero descent). Also see this.
If more height is provided for the buttons in row, they will neatly center align their texts.

I would suggest using a TextView instead of a Button, because that has less tricky margins and gives you more control on how to place your Text. (In general I only seldom use buttons, but tend to make other views TextView, ImageView,... clickable instead)
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:padding="0dp" />
In case you need some kind of indication on whether or not the TextView was pressed, you can add a selector as a background.
For this, have a look here.
Finally if you want your text to fit for any possible view sizes, have a look at the AutoFitTextView.

I think the simplest way to achieve this by using AutoFitTextView
You can get this library from this link : https://github.com/grantland/android-autofittextview
It will dynamically resize text size with current height and width.
I hope this will help you.
Good Luck.

Try to use a negative top padding, for example:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:paddingTop="-2dp" />
You can play with that, and get the necessary value for top padding. Of course, you can use different values for different screen densities by using #dimen.

you can't use upper spacing for centering because it belongs to font. I mean every font has ascent and descent metric. See pic below.
so when android centers text it calculates it's overall height (including upper nad bottom spacing). And than centers resulted rect. You can get FontMetrics to see font acsent and descent values.
E.g. calling Paint.FontMetrics fm = textView.getPaint().getFontMetrics();we get:
fm = {android.graphics.Paint$FontMetrics#3668}
ascent = -25.976562
bottom = 7.5878906
descent = 6.8359375
leading = 0.0
top = -29.572266
Intresting? So how to remove top and bottom spacing and use this space to center font? I see two solutions:
create custom component and layout text excluding those spacings. If
you want only single line text it will be easy. See TextView code.
we know spacings, so we can move text accordingly. Set translation to
a Button content. To remove top spacing you can do:
button.setTranslationY(fm.top - fm.ascent); If you want text centered
without spacing, do following: button.setTranslationY((fm.top - fm.ascent) + (fm.bottom - fm.descent));

Related

Native Android: Create dynamically a grid-like scrollable layout with equal width buttons

I am trying to create a layout as per this picture (paint'ed).
Layout Design
Target level is API 17. This has to be created programmatically, not using XML. This has to be a responsive design.
I extensively researched and attempted other partially similar situations on stackoverflow, using GridLayout, TableLayout, GridView, various layout parameters, gravity, weight, view nesting and so on. However,
(a) I can't get the text buttons width to fill the available width real estate as per device screen size and orientation. Buttons with shorter texts are coming with shorter width.
(b) The plus, minus and number buttons are of fixed height and width irrespective of screen sizes and orientation. But they are not aligning with the text button in the left on the same row. only the bottom few pixels are visible.
I would appreciate any code snippet that can achieve the above layout. Thanks a million.
UPDATE:
Following inputs from #tiny-sunlight, I did this. Next I will recreate this programmatically.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutTable"
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layoutRow"
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginEnd="10dp"
android:layout_weight="17"
android:text="This is my button"
android:textSize="15sp"
android:textAllCaps="false"
android:gravity="start"
android:layout_width="0dp"
android:layout_height="40dp" />
<Button
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="-"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="0"
android:textSize="15sp"
android:gravity="center"
android:layout_width="40dp"
android:layout_height="40dp" />
<Button
android:layout_weight="1"
android:text="+"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Try to build layout below programmatically.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="1111"
android:layout_width="0dp"
android:layout_height="40dp" />
<ImageView
android:layout_marginRight="5dp"
android:src="#mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginRight="5dp"
android:background="#44bb11"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="11"
android:layout_width="40dp"
android:layout_height="40dp" />
<ImageView
android:src="#mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
You can use a recycler view, which will provide you scrolling option plus bind views on the fly for you.
So load a linear layout (orientation horizonal) into the recycler view. now for the layout, it then becomes easier. You need a textview, button,textview,button.
For each of these textview and buttons, set widht = 0dp and weight as 7,1,1,1 respectively i.e. your leftmost textview occupy 70% of width whereas all other occupy 10%. Obviously you can change these weights as per your requirements. Thats it for the layout.
Now just fill your data using the adapter.

Align multipile images evenly horizontaly

I have 2 image views in a linear layout like so:
<LinearLayout
style="#style/home_icon_row"
android:orientation="horizontal">
<ImageView
android:id="#+id/ibtn_home_bus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_bus_selector" />
<ImageView
android:id="#+id/ibtn_home_butoday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_butoday_selector" />
</LinearLayout>
How can i align the two images so that they are evenly placed within the linear layout. Something similar to "justified text" so that it has equal spacing on the left and right of the image from each other and the border of the screen.
it's not the best solution, but it should work:
<LinearLayout
style="#style/home_icon_row"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:enable="false"
android:focussable="false"
android:clickable="false"
/>
<ImageView
android:id="#+id/ibtn_home_bus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_bus_selector" />
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:enable="false"
android:focussable="false"
android:clickable="false" />
<ImageView
android:id="#+id/ibtn_home_butoday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_butoday_selector" />
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:enable="false"
android:focussable="false"
android:clickable="false" />
</LinearLayout>
if this is for some kind of landing it's ok but if it's a hard used layout think about implementing it dinamically via javacode to make it faster.
EDIT: i added 3 attributes ( enable, focussable, clickable ) to disable the placehodler view so that they'll be considered only at measures/layout time, but not during events handling.
// try this way here is alternative to use two sub linear layout rather three View.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/home_icon_row"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="#+id/ibtn_home_bus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_bus_selector" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="#+id/ibtn_home_butoday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_butoday_selector" />
</LinearLayout>
</LinearLayout>
You should be able to get similar effect by using layout_width="0dp", layout_weight="1" and scaleType="centerInside" for both ImageViews.
The only difference is that space between the images can be bigger.
<LinearLayout
style="#style/home_icon_row"
android:orientation="horizontal"
android:padding="#dimen/padding">
<ImageView
android:id="#+id/ibtn_home_bus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_bus_selector"
android:layout_marginRight="#dimen/padding"/>
<ImageView
android:id="#+id/ibtn_home_butoday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_butoday_selector" />
</LinearLayout>

layout element control in Android

I want to layout 3 element Button , TextView , Button on the bar like this
[Button1] --- TextView --- [Button2]
2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). How can I do it??
It would look something like this.-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
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_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Notice that you still need to set other properties such as texts, ...
You can use LinearLayout with android:weightSum
Try Like this, By using android:weightSum, you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="LeftButton" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:layout_weight="4"
android:gravity="center"
android:text="Center TextView text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="Right Button" />
</LinearLayout>
android:weightSum work fine for me, I resolved all my troubles :)

Align android text and images in listview to be the same

I need some help with alignment of custom list view with image and text.
I'm trying with this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center"
android:paddingTop="20dp"
android:layout_weight="2"
android:text="TextView" />
</LinearLayout>
And I thought that weight is keeping the ratio of element, not the content of the element, but apparently it takes care about things inside the text view, so my list looks messy.
How to align it to be the same, no matter how long the text is in text view?
So the green line should be straight vertical line, no matter of text on right hand side.
Tnx
Basically, your layout should be this:
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center"
android:paddingTop="20dp"
android:layout_weight="2"
android:drawableLeft="#drawable/ic_launcher"
android:text="TextView" />
The android:drawableLeft="#drawable/ic_launcher" defines the leftmost compound drawable, and it will align nicely.
Remove android:layout_weight.
This will align you images since you define the width.
If you want to align the text too, remove android:gravity or set it to left.
You can add android:marginRight to the imageView for better readability.
use
android:layout_height="0dp"
in your textView. it will give fixed size to all your elements. here, the size shared by the weight param is variable as it depends on the amount of text.
Try this instead:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:text="TextView" />
</LinearLayout>

how to separate textviews in android?

It is a simple question but I don't know how to do it because I'm new in android.
I have two TextViews in a relative layout and when the first TextView getting bigger it writes on the other TextView which I want to avoid it. How can I do it ?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:gravity="left"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second"
android:layout_alignParentRight="true"/>
</RelativeLayout>
but when the text = firstfirstfirst.... it overrides the second textview.
You need to give you TextViews ids. Since you're using a RelativeLayout you can use layout_toLeftOf and layout_toRightOf, but you only need to use one of them.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toLeftOf="#+id/textView2"
android:gravity="left"
android:text="firstfirstfirstfirstfirstfirsitseirodsifjdsoijfoisddsefdfadafdafad" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:text="second" />
give some advice:
first you can set the left textview's max width using android:maxWidth="" to control the left size, but if the size bigger than the size the text will become like this:text...
or you need make the left and right area cleanly, for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="firstdasssssssssssssssssssssssssssssssssssssssssssssssssssssssss" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="left"
android:text="secondsdasdasdarerewrwcxcadasdasdsadsdasdasdadadsd" />
it make the left and right half area, also you can change the android:weightSum size.

Categories

Resources