I have two textviews different in font and style I need to put them in one line but seems to the user one textview.
more details if the 2nd text is big from the screen I need it to be completed in the second line under the first text.
ex-1: text1 text2
ex-2: teeeeeext1 teeeeee
eeeext2
I don't know if it possible to do that or not so I asked for help.
[Edit]
it's important to make them in two textviews because I'll set two different click listeners to them.
thanks.
Asked
Active
Viewed 125 times
-1

Tefa
- 328
- 1
- 4
- 15
-
you can use LinearLayout put both the text view in linearlayout, and make orientation horizontal – Pramod Yadav Sep 07 '17 at 12:00
-
@PramodYadav but this way will make the 2nd textview will be completed under its side, I need it to be completed under the 1st textview from the start of the line. – Tefa Sep 07 '17 at 12:03
-
1@Tefa it is completely possible to add two textviews side by side, But if you want to send the text of second textview under first then it won't be possible the way you are asking. Because if that text goes into second line it will be it's area, not under the first one. – Umair Sep 07 '17 at 12:06
-
@Umair yes, this is the problem I faced so I tried to get any Idea. – Tefa Sep 07 '17 at 12:10
-
@Tefa what have you tried so far can i see some code so i will be able to help you more ? And secondly I can give you the example how to show the textview side by side but showing the text in second line you have to come up with something else :) – Umair Sep 07 '17 at 12:13
-
@Tefa Please check this https://stackoverflow.com/a/7388398/1518273 – Napolean Sep 07 '17 at 12:23
4 Answers
1
If u want u can use fromHtml
tag so that both font and style in one textview only..that would satisfy the need of text coming in next line too

Adarsh
- 95
- 12
-
I forget to add that I need to add different click listeners to each one. – Tefa Sep 07 '17 at 12:05
-
u can try by adding two transparent views over the text..n check which text is in one line ..bring the view of text which have 1 line by bringToFront(). And handling clicks on the views rather then `textview`. – Adarsh Sep 07 '17 at 12:13
1
You can use this custom span class I wrote with the help of this and android's StyleSpan
class.
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
/**
* Created by aminmsvi on 9/7/2017
*/
public class CustomClickableSpan extends ClickableSpan {
private final int mStyle;
private final ClickableSpanListener mListener;
/**
* @param style An integer constant describing the style for this span. Examples
* include bold, italic, and normal. Values are constants defined
* in {@link android.graphics.Typeface}.
*/
public CustomClickableSpan(@Nullable ClickableSpanListener listener, int style) {
mStyle = style;
mListener = listener;
}
/**
* Copied this method from {@link android.text.style.StyleSpan} source code
*/
private static void apply(Paint paint, int style) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int want = oldStyle | style;
Typeface tf;
if (old == null) {
tf = Typeface.defaultFromStyle(want);
} else {
tf = Typeface.create(old, want);
}
int fake = want & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
@Override
public void onClick(View widget) {
if (mListener != null) {
mListener.onClick();
}
}
@Override
public void updateDrawState(TextPaint ds) {
apply(ds, mStyle);
}
public interface ClickableSpanListener {
void onClick();
}
}
And you can use it like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
TextView textView = findViewById(R.id.text);
SpannableString spannableString = new SpannableString("This is a text");
spannableString.setSpan(new CustomClickableSpan(() -> Toast.makeText(this, "This is a text".substring(0, 4), Toast.LENGTH_SHORT).show(), Typeface.BOLD), 0, 4, 0);
spannableString.setSpan(new CustomClickableSpan(() -> Toast.makeText(this, "This is a text".substring(5, 10), Toast.LENGTH_SHORT).show(), Typeface.ITALIC), 5, 10, 0);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
// if you want to remove highlight color, uncomment this line
// textView.setHighlightColor(Color.TRANSPARENT);
...
}
}
Hope this helps.

Amin Mousavi
- 1,220
- 10
- 21
0
You can achieve by using SpannableStringBuilder Class and I guess this is the answer you are searching for SpannableStringBuilder

Nabin Khatiwada
- 478
- 5
- 15
-
It will work if didn't need to add a click listener to each one, but unfortunately, I need to add a click listener to each one. – Tefa Sep 07 '17 at 12:22
0
If you want to show two textview's side by the without giving user the idea that it's a single thing then try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horzontal" >
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:padding="5dp"
android:gravity="center|start"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="TextView asdasdasd as das dasdasdas adasd asdas d" />
Hope it helps.

Umair
- 6,366
- 15
- 42
- 50
-
-
@Tefa if you need two click listeners then you need two textviews. ;) and secondly when you make your layout like this then these widgets will seems to be in the same line or a single thing. – Umair Sep 07 '17 at 12:23