Dynamic Android TextView size - android

I have a RelativeLayout where I have 2 TextView side by side (the left one it's the field name and the right one it's the value).
What I need it's to have the left textview always showing the full field name and the other TextView using the remaining space in that line to print the field value. Imagining that I have a field named "Field" with the value "That's a huge value!" and the remaining width of screen just leaves us with space to have half of the field value I need to have it like this:
| Field: That's a huge v... |
If, for some reason, the field name would fill the entire screen I don't event want to appear the value (I have another way of solving that).
I'm trying to find an adaptive way to do this (for any field and any value) but I can't find the right settings.

If I understood your question, the answer is simple:
<?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="wrap_content">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FieldHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH:"/>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tv1"
android:singleLine="true"
android:text="Value lllllllllllllllllllllllllllllllggggggggggggggggggggggggggggggggggggggggT"/>
</RelativeLayout>

Related

Fixed-width TextView with stretched compound drawable

I'm trying to achieve the following layout: a fixed width TextView aligned to the left of its parent, with the text inside it aligned to the right side of that TextView (that's why fixed width, can it be done other way?) and the rest of the parent is filled with a drawable (simple line). Like this:
It's a ListView containing 2 types of rows and the layout for the rows with lines is quite trivial - LinearLayout with TextView and ImageView (I can post the exact code later if needed). And I'm getting a warning that it could be replaced with a single TextView with compound drawable.
I'm all for optimization so I really tried to follow that advice. Unfortunately I wasn't able to get the same result - the line is either constrained to TextView's width or text is aligned to the right side of the ListItem, now to fixed position.
Am I missing something?
Edit: Apparently it is not actually possible and since there are some other complications (the drawable is now a level-list drawable, which is not always a line and sometimes it has a non-fixed height that I have to set) I will leave it as it is now - linear layout, containing one TextView and one ImageView.
I don't think that you're missing anything. The TextView compound drawable features are not very customizable and in general are not worth the time you spend trying to get them to look right. Some lint warnings are a little overzealous and premature.
The optimization that the lint refers to is something that is better attributed for a fixed size image. In your case, the line has to stretch the rest of the screen length and as such it is not something that can be done with a textview with compound drawable. This kind of lint warning is more of a suggestion rather than something that MUST be done and is detected by just checking for a linear layout with only a textview and an imageview rather than checking what would need to go in the image view. If you already have it working the way you did it I think you should leave it alone.
Your view create from this -
<?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:id="#+id/time"
android:layout_width="#dimen/today_time_width"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="right"
android:layout_marginRight="5dp" />
<ImageView
android:layout_gravity="center"
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="#dimen/today_current"
android:src="?attr/item_boundary" />
</LinearLayout>
There is no way to achive this using only standart TextView. If you really want to reduce view count you can create your custom TextView class, set layoutWidth to matchParent and draw line from text end to right border. But it's not worth to be doing. Some extra views won't slow your list.
I am not sure if you will be able to achieve what you really want to , but then you could change the linear layout in the link you posted to something like this:
<RelativeLayout
android:id="#+id/relTrial"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="12:45 AM"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:id="#+id/lnrSep"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_toRightOf="#+id/txtTime"
android:layout_alignParentRight="true"></LinearLayout>
</RelativeLayout>
This way the time text will be right aligned although being at the left side, and the line will also be visible.
Hope that helps.
If I got you right, you want to add bottom border to list view item?
What about to try this:
android:drawableBottom="#drawable/line"

Android TextView doesn't do linebreak in ListView (multiline text)

I have created a 3-level ExpandableListView and have the problem that the TextViews which are used for the 2nd and 3rd level do not support line-breaks if the content is too long. It should be dynamically over more than one line, if needed. The 1st level TextView does it well (automatically) and I actually had the same settings in the xml for all three TextViews. Followed are the layout xmls, the one TextView with the id groupname is for the 2nd level (e.g. the first red X in the picture below) and the one with id childname is for the 3rd level (e.g. the second and third red X in the picture below). It should all be like at the green hook in the picture.
"singleLine=false" seems not to work. Also tried some different options found in other SO posts, but what I've testet haven't worked for me. Like ellipsize, scroll horizontale, different layout_width and so on. The only thing worked is to set a fixed layout_width on x hundred dp, but this is not dynamically, I'm right?
Would be great if anybody could help me with this. Lot of thanks!
Here's a screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/childname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:textColor="#AAAAAA"
android:singleLine="false"
android:text=""
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginRight="60dp"
android:textColor="#555555"
android:singleLine="false"
android:gravity="center_vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
Add this line in your xml
android:inputType="textMultiLine"
or
Add text using coding like this, where you can add line break using '\n'(But here you have to manually add breaks where you want them)
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");
Results will be
Hi
Hello
How are You
Edit
Accepted Answer - removing the line 'android:layout_alignParentLeft="true"
try using LinearLayout instead of RelativeLayout as parent for the TextView
I had add this attribute to my TextView inside ListView, and makes it do line break correct.
android:maxWidth="xxxdp"
F.Y.R.

Align textview according to the language selection (LEFT -RIGHT)

i have a linear layout , which contains a textView and EditText arranged horizontally.i have an option for selecting language in prvious activity (english and arabic). when i select english the current alignment is fine but when i select Arabic it should show right to left that means the textView position should go to right (in layout it will start from left) after that the editText.
<?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="match_parent"
android:background="#drawable/darkblue_bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editusername"
android:layout_width="196dp"
android:layout_height="wrap_content"
android:background="#drawable/textfield_default"
android:ems="10" >
</EditText>
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
anybody pls give me solution...
You can specify a layout that is specific to a language, by placing that layout inside an appropriate layout folder. I don't know what the code is for Arabic, but it should be something like /layout-aa/
Far better today, however, is to use a relative layout, where you use the android:gravity="start" or similar, aligning it based on the language orientation and not strictly left/right. This is available with API version 14 and above.
If you want to use both of arabic and English for example in a textview, first you should put it's layout:gravity = "start"
This help you if you have an English sentence it starts by left hand side and arabic started sentence with right hand side.
If you want it'a combination, you must use like this
Arabic string + \u200e\u202a English string + \u202c + arabic string
And in reverse:
myEnglish String + "\u202B" + myArabicString + "\u202C" + moreEnglish
You can use android:layout_alignParentRight="true" in textview

How to align name:values pairs as ListView item

I need to show a list of name:value pairs in ListView. And I want to align values to longest name width. But length of names and its count is unknown until data is received.
For example:
name1: value1
name111: value2
someOtherName: value3
----------------------next ListView Item
name3: value4
longestNameeee: value5
----------------------next ListView Item
For now I've created Adapter with LinearLayout as item layout and I'm adding inner LinearLayouts with name - value pairs with weigth 0.5 - 0.5 (means values shows after middle of layout).
But how to set name - value weights according to longest name length?
maybe you can consider using a relative layout in you listview row layout and align the name to the left and value to the right?
Another option might be to can use Paint.measureText to measure text length - but to need to set the paint properties that same as the textview display properties (textSize, texyStyle,font,...)
http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String, int, int)
you should be able to get a decent measurement of the text length using this - it give you a sixe in pixels Which you can use to set the layout params of all the name textViews in your list rows.
Do you need the values to commence exactly after the longest name? Or just to be able to show that longest name? Well what I would suggest (and did in a similar problem) is not using a LinearLayout, but a RelativeLayout.
In there you would define two TextViews, one of which would have gravity on the right. From what I can see from your code, the right field is always same size, correct? So, adjust it according to that and let the left space free to be filled.
An example layout of mine:
<?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="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="3dip" >
<!-- Name-->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#040404"
android:typeface="sans"
android:textSize="20dip"
android:text="Name"
android:textStyle="bold"/>
<!-- Amount -->
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_marginRight="5dip"
android:text="money"
android:textSize="20dip"
android:textColor="#FF8B1500"
android:textStyle="bold"
/>
</RelativeLayout>
The right TextView fills as much space as it needs and the left is left for the names.
hope I helped giving an idea!

Characters appear out of the screen in the TextView!

Hey, I've been trying to fix this problem for a long time, The problem
is that some characters in the text are out of the screen, here is a
screenshot of what I mean:
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/list"
android:orientation="vertical"
android:padding="15dip"
android:layout_width="fill_parent"
android:id="#+id/relativeLayout1"
android:layout_height="fill_parent">
<TextView
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:id="#+id/Munawwat_Text_TextView_text"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Can anyone suggest a way/workaround to fix this problem?
A workaround for this is to put it into an EditText. If I put the text
into the EditText, the EditText gets horizontally scrollable
(scrollable for ~3px) which is good so you can see the characters that
are out of the screen but i dont want to let the users mess with the
text.. So:
Is it possible to not allow the users to edit in the EditText and
in the same time let the EditText get scrollable?
Is there any other view that i can put the TextView into it, so it
can get scrollable a bit so I can see the characters? (limit the
scroll of the view)
Is there a way to limit the number of characters in each line in
the TextView?
I know I asked a lot though this is one of the last problem I have so
I would really appreciate any comment/suggestion!
Thanks.
You could wrap your TextView in a ScrollView. Or make the text smaller. Or try adding padding on the left to see if it forces linebreaks.
and try to use weight :D
it limites number of caracteres in textview
android:maxLength

Categories

Resources