I am currently writing an App for Android and in one of the layouts I have a users name and beside it a couple of tags.
If both the users name and the tags are short enough, they fit neatly into one line. However, when either of the two are longer I want the name to stay fixed to the left, and the tags to drop to a line below and align to the right.
Currently I have the following code:
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/name"
android:textColor="#000000"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"
android:id="#+id/tags"
android:textColor="#000000"
android:layout_toRightOf="#+id/name"
android:layout_alignTop="#+id/name"
android:textAppearance="?android:attr/textAppearanceSmall" />
This just simply displays both on one line. I presume I'll need to do the text wrapping in the java or is there a neat XML solution?
As far as I know, there is no XML solution for this problem.
You need to calculate the size of the name and then adjust the tag on the new line or keep it at same line.
Related
I'm developing an application which involves messing around with some Greek and math characters. I have been trying for some time to render them into my XML, but I don't know what I'm doing wrong, since I have used the unicode for the Strings that are looking bad, Html.fromHtml, strings.xml and even tried another font.
These are the letters that are not showing correctly: β, μ, σ and ω.
This is my XML for beta:
<Button
android:id="#+id/btn_beta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/special_chars_background"
android:text="#string/beta"
android:textColor="#color/black"
android:textSize="21sp" />
Where #string/beta equals to \u03B2 and it looks like a normal capital B.
Can you guys please help me? Thanks in advance.
try this : add android:textAllCaps="false" in Button property
<Button
android:id="#+id/btn_beta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/special_chars_background"
android:text="#string/beta"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="21sp" />
in string.xml put this string
<string name="beta">β</string>
I'm trying to add "Read more" to a the end of a textView if the text is more than two lines long, but I'm having a hard time doing so. My TextView looks like the following:
<TextView
android:id="#+id/reviewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="1dp"
android:text=""
android:maxLines="2"
android:ems="3"
android:ellipsize="end"
android:textColor="#FFFFFF" />
So to clarify, right now I get three dots because of the ellipsize but I really want it to write "Read more".
Incidentally, my software is supporting API level 14+.
Try to add following two lines:
abdroid:focusable="true"
android:focusableInTouchMode="true"
to your TextView
I have edittext view in my application but when i insert long text in edit text this increases view size.I do not want happened in my app because it created bad imapact on app so I decided to use
android:singleLine="true"
android:ellipsize="end"
android:maxLine="1"
xml attribute to the edit text but it not worked for me .I don't have any idea how to prevent this from my problem.I did lot of R&D but i am not get satisfactory answer.Here is my Edit Text.
<EditText
android:id="#+id/etaccountholdersname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvaccountholdername"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/ic_input_field"
android:hint="Account holder's name"
android:singleLine="true"
/>
Here Is full code of Xml file
<RelativeLayout
android:id="#+id/innerrel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<TextView
android:id="#+id/tvaccountholdername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/ic_account_holders_name_text"
android:gravity="left" />
<EditText
android:id="#+id/etaccountholdersname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvaccountholdername"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/ic_input_field"
android:hint="Account holder's name"
android:singleLine="true"
android:scrollbars="vertical" />
</RelativeLayout>
Please provide some help me to solve this issues.
In your layout design you have "wrap_content" for both parent and children views. Make the parent layout width as static. If you working with multiple screens, use the dimens.xml for the sizes of the views in different screen sizes.
Use either android:layout_weight property or use android:ems="7" it will work for you definetely
do not use
android:ellipsize="end"
its uses for textview
use
android:ems="10" (it limits edittext to show 10 charecters but you can insert many characters)
and you are good to go.
I am using GridLayout from the support Library. Some of the cells contain player information - I use a Linear Layout with two TextViews. I have not been able to get the name (the second text view) to wrap.
Current behavior:
123
John Cochtousczyon
Desired behavior:
123
John
Cochtousczyon
Here's the current state of the cell layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/player_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="test" />
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:minLines="2"
android:scrollHorizontally="false"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
</LinearLayout>
and the code that adds him to the grid:
lp = (GridLayout.LayoutParams) playerCell.getLayoutParams();
lp.columnSpec = GridLayout.spec(nameCol);
lp.rowSpec = GridLayout.spec(nameRow);
playerGrid.addView(playerCell, lp);
Ideally I would like a solution that sets the column width for the widest cell AFTER wrapping the name. I've gotten close to the desired result using maxEms, but that requires balancing the most likely needed limit against how much truncation to tolerate with longer names.
P'r'aps there's nothing for it but to fix the width - just thought I'd ask in case all the smarter people than me out there have solved this.
Here's what I've settled on for now, until something better comes along:
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="4"
android:maxLines="2"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
combination of fill_parent, ellipsize END, maxEms and maxLines gets me close enough.
If anyone is still looking this in 2020 Android now has a inbuilt way of doing this. Just add this code to your java file
TextviewCompat.setAutoSizeTypeWithDefaults(<your textview refernce here>, TextviewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Also make sure you add this to your textview xml
android:ellipsize="none"
Hopefully this will help someone;
I simply used the good old "\n" to wrap.
I know this question has been asked a number of times but I still can't seem to get this to work.
I have tried all sorts of combinations and it just refuses to wrap to 2 lines.
This is what it is now:
<TextView
android:id="#+id/doctor_details_clinic_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="3dip"
android:textSize="12sp"
android:maxLines="1"
android:layout_gravity="right"
android:gravity="right"
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none"
/>
I've tried make the width 0dip and all other things suggested but to no accord.
First of all you should set Max lines to 2 instead of 1.
Something like this will work:
<TextView
android:id="#+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="this is the most interesting use of text view I have seen so far,this is the most interesting use of text view I have ever seen so far,this is the most interesting use of text view I have ever seen so far this is the most interesting use of text view I have ever seen so far"/>