Image beside text with more than 2 lines android - android

How to display an image next to last alphabet of a text ?
i got a problem when having a textview with more than 1 lines, the image wont follow the last alphabet of the text
The output that I should make should be like number 1 and number 2, but my output just be like number 1 and number 3 (image below)
*I tried to make my explanation clear so I make it with png to figure what I mean.
To make the correct output, what I should do on my XML ? or I should go with additional programming code in java. please help, thanks

I might try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textLine1"
android:layout_width="wrap_content"
android:layout_height="12sp"
android:text="Username of" />
<TextView
android:id="#+id/textLine2"
android:layout_below="#id/textLine1"
android:layout_width="wrap_content"
android:layout_height="12sp"
android:text="a man" />
<ImageView
android:id="#+id/image"
android:layout_width="12sp"
android:layout_height="12sp"
android:layout_below="#+id/textLine1"
android:layout_toEndOf="#+id/textLine2"
android:src="#drawable/image" />
</RelativeLayout>
So yeah this will help. And as you can I haven't use android:layout_marginTop attribute here but if you use it make sure it's the same for textLine2 and iamge so that it would look good. And if you want to add more such name with images just copy -paste the same code below it and simply change their android:id.
Also change the id here in this line: android:layout_below="#+id/textLine1"
So that new views will be added below the previous once.
If you need more clarification please drop a comment below.

Try it
<TextView
android:id="#+id/TextView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/star"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:text="Username" />

Try this:
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:text="Testing my \n text for images"
/>
and in your java code:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Testing for text view \n and images")
.append(" ", new ImageSpan(getActivity(), R.drawable.com_facebook_button_icon), 0)
.append(" ");
myTextview.setText(builder);

Related

How to change the text size of a checkbox

It looks like this.
I want to change the text size of a checkbox in a fragment.
The text size itself became bigger, but the little field of the checkbox is now a little bit higher than the text.
I hope someone can help me with this.
Thanks
This is my code for the CheckBox:
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Text"
android:paddingTop="#dimen/my_padding"
android:id="#+id/text" />
<CheckBox
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Text"
android:textColor="#000000"
android:textSize="20dp" />
Simply set the textAppearance attribute to your desired value:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
Try to scale it as you need
P.S check at run time
If you got a problem with text size when you scaling
use
this
checkBox.TextSize = (int)(TEXT_SIZE/SCALE_FACTOR);
<CheckBox
...
android:scaleX="0.60"
android:scaleY="0.60"
/>

android id in RelativeLayout

<TextView
android:id="#+id/first_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/second_textview"
android:layoutbelow="#id/first_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is my first Android Application!" />
<Button
android:id="#+id/first_button"
android:layoutbelow="#id/second_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="And this is a clickable button!" />
I got error when tried to run.
There is a "red" cross at the lines of:
- second
Any idea?
At following two places:
android:layoutbelow="#id/first_textview"
android:layoutbelow="#id/second_textview"
Change layoutbelow to layout_below. Note the use of underscore: _. If you use Ctrl + Spacebar while writing these attributes, eclipse will show you the options available(In case you use eclipse). This way you decrease the chances to such typo mistakes. Hope this helps.
Replace your textview and button line-
android:layoutbelow="#id/first_textview"
with this one-
android:layout_below="#+id/first_textview"
Use "#+id" in giving reference, not the "#id" here and you are missing the "_" in the layoutbelow attribute.

RelativeLayout textviews overlapping

I have a rather simple ListView row:
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
When the text of "#+id/tournament_name"is long - it overlaps with the one from "#+id/tournament_winner" and I don't understand why.
I tried using android:singleLine="false"to no avail. I also tried using android:inputType="textMultiLine"as the docu says android:singleLine="false" is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ? so no good here as well.
I also tried using android:ellipsize="end" but this doesn't work. I assume it is because the text in the left TextView ("#+id/tournament_name") is NOT long enough to fill up the full width of the ListView (which code is not sowing here).
I was sure that if I use android:layout_width="wrap_content"the two TextView fields shouldn't overlap.
Here is an example (see the second line):
Any further ideas how this could be fixed?
android:layout_toLeftOf="#id/tournament_winner" in First TextView.
Also set android:maxLines="1" and Fix width for tournament winner because when it gets long tournament name cant see...
row.xml
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/tournament_winner"
android:maxLines="1"
android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="WINER"
android:textSize="25dp" />
</RelativeLayout>
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="#+id/tournament_winner" but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="#+id/tournament_winner"and not android:layout_toLeftOf="#id/tournament_winner" - pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="#id/tournament_winner": Error: No resource found that matches the given name... so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tournament_winner"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
You can use single text view in place of two and simply display both strings in one text view !!

Android: how to align two text views based on length of text

I have a layout in which two TextViews are to be displayed on the same line such that
If TextView1 is a short text, TextView2 should be immediately right to TextView1(IMAGE1) and if the TextView1 is a long text, TextView2 should be at right corner of the Layout on the same line(IMAGE2)
How can I achieve this ?
i use simple horizontal LinearLayout with android:layout_weight attribute and it worked like you want.
Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="teeeeeeeext1"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"/>
</LinearLayout>
Use a layout like this..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/editText1" />
</RelativeLayout>
edittext 1 will push edittext2 to its right...depending on text in it..
You can set android:maxWidth property for first text view. So your layout would look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:text="sdfksdofsdfsdfsdfsadfgwagrswargweqrgeqrgqwergeqrgeqrg"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="text2"
/>
Edit:
I apparently misread (or did not read fully) your question. Just don't mind my answer - I vote for mxy's :)
The last time I had the same problem, I wasn't able to find or hack away a straightforward solution. Counting pixels was not an option (at least not for me). But I found a workaround that eventually led to the same display concept, and that was to use SpannableStringBuilder.
As I'm assuming you want two different TextViews because you want them to have different colors (or size, or style, or typeface).
The idea is to use a single TextView for the whole thing, and prepare a SpannableString before doing setText on your view.
By using combinations of ForegroundColorSpan, TextAppearanceSpan, etc, you can make your single TextView look like different TextViews with different styles, sitting side by side, wrapping to the next line as necessary.
Links:
Setting font color at runtime
TextAppearanceSpan sample

Textview show partial text

I have a TextView in which I must load a message. The TextView has maximum 2 lines (android:lines="2"). The message may contain an '\n' character.
How should I load the message in that TextView so that words would be wrapped and if in those 2 lines message does not fit, at the end of last visible word I must add three dots (...)? How can I detect the length of text that fits in that TextView?
My code for TextView is
<TextView
a:id="#+id/tv_message"
a:gravity="top"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_toRightOf="#id/iv_icon"
a:layout_marginLeft="2dp"
a:layout_marginTop="4dp"
a:paddingRight="7dp"
a:paddingBottom="5dp"
a:textSize="12sp"
a:typeface="sans"
a:ellipsize="marquee"
a:lines="2"
a:maxLines="2"
a:textColor="#android:color/black"
/>
But in application, text appears on two lines, even if there is a line containing the signature. The message is like: "message text" + "\nSignature" but the message can be on 1,2,3 lines, depending of message length.
In XML, use the property android:ellipsize="marquee"for the TextView.
Your TextView can be defined like:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:lines="2"
android:ellipsize="marquee"/>
Edit: I've tried the following code, adapted from yours:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlt_news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_background"
android:padding="5dip" >
<TextView
android:id="#+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:text="Name"
android:textStyle="bold" />
<ImageView
android:id="#+id/rss_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_title"
android:layout_marginLeft="2dip"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:src="#drawable/rss_cell_icon"
android:visibility="visible" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#id/rss_icon"
android:ellipsize="marquee"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
android:textColor="#android:color/black"
android:textSize="12sp"
android:typeface="sans" />
</RelativeLayout>
And here is the output:
Use android:ellipsize="end" instead of "marquee" that did it for me.
Gabi,
I was also having the same problem. To enable ellipsize on a textview with 2 lines I use the following parameters:
android:maxLines="2" // You already got this one
android:ellipsize="end" // also this one
android:singleLine="false" // This is the command that solves your problem.
Unfortunately I got a new scenario for my app. I have to use 3 lines with ellipsize and this configuration only works for 2 lines... :( Good luck
If you set text from code, than you must set options once again!
textView.setText(content);
textView.setEllipsize(TruncateAt.END);
textView.setLines(5);
textView.setMaxLines(5);
It works for me when I set it in code not in the XML layout
holder.txtDescription.setText(text);
holder.txtDescription.setMaxLines(3);
holder.txtDescription.setEllipsize(TruncateAt.END);
String s = "line1 \nline2 \nline3";
String [] lines = s.split("\n");
String twolines;
if (lines.length > 2) {
twolines = lines[0] + "\n" + lines[1] + "...";
} else {
twolines = s;
}
This should give you the desired result, just use
((TextView)findViewById(R.id.text)).setText(twolines);
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
android:autoSizeTextType="uniform" did the trick for me. Text size reduces to fit available space but complete text is displayed.
Refer this link
https://developer.android.com/develop/ui/views/text-and-emoji/autosizing-textview

Categories

Resources