4

In android we have constraints so we can put Views to start-of another Views and to end-of another Views

Specifically TextView when you set it's end to start of another view it will be look like:-

Tex | Another View
tVi |
ew  |

Is there any layout to help me getting dynamic TextView for example the above one would be look like:-

Tex | Another View
tView | Parent device border

Here is a visual example:-

enter image description here

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Ibrahim Ali
  • 1,237
  • 9
  • 20
  • There is a great answer provided here : https://stackoverflow.com/a/27064368/3696500 I hope this helps – Thiago Jun 28 '20 at 09:33

2 Answers2

0

Maybe FlowTextView can solve your problem. FlowTextView is based on a RelativeLayout with added TextView features that wraps its text around its children.

Alternatively, if it's ok for you if the TextView is at the right and the image is at the left, like this:

---------
| Image | Text that is so
--------- long that it's 
 wrapping around the image

you could try the LeadingMarginSpan2:

Implement it like this:

public class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {

int lines;
int offset;

public MyLeadingMarginSpan2(int lines, int offset) {
    this.lines = lines;
    this.offset = offset;
}

@Override
public int getLeadingMarginLineCount() {
    return lines;
}

@Override
public int getLeadingMargin(boolean first) {
    return first ? offset : 0;
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {

    }
}

and use it like this in your code:

float textLineHeight = textView.getPaint().getTextSize();
int lines = (int) (imageView.getMeasuredHeight() / textLineHeight) + 1;
int offset = imageView.getMeasuredWidth();

SpannableString text = new SpannableString("Text that is so long that it's wrapping around the image");
text.setSpan(new MyLeadingMarginSpan2(lines, offset), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(text);

In case you want to provide an additonal fallback for very old versions of Android (< 2.2), you can have a look at this answer to a similar question: https://stackoverflow.com/a/8463221/13792619

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
TobiWestside
  • 194
  • 2
  • 6
0

There is a great answer provided here : stackoverflow.com/a/27064368/3696500 I hope this helps ,

Also have a look at https://github.com/deano2390/FlowTextView A github library.

<uk.co.deanwild.flowtextview.FlowTextView
    android:id="@+id/ftv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:padding="10dip"
            android:src="@drawable/android"/>
</uk.co.deanwild.flowtextview.FlowTextView>
Thiago
  • 12,778
  • 14
  • 93
  • 110