android - Cannot wrap blockquote-like TextView of arbitrary line count by width -


i need render quote block of arbitrary length. text must aligned left, while block aligned right, similar one: example

for i'm trying textview android:width="wrap_content", android:gravity="start", , android:layout_gravity="end". however, works expected when text fits single line — if text longer that, textview behaves this:

  • 1st quote block sentence spaces — devours parent's width;
  • 2nd block — spaces non-breaking: raw persistence may be only option other than giving up entirely. — still block behaves match_parent.
  • 3rd block uses explicit line break — looks closest what's required, that's least flexible option, , there's still padding on right.

screenshot

here's layout (paddingright replaced layout_marginright highlight purpose — behavior same either way):

<linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical">      <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="end"             android:textappearance="@style/textappearance.appcompat.body1"             android:linespacingextra="3.5dp"             android:paddingleft="24dp"             android:layout_marginright="24dp"             android:text="raw persistence may option other giving entirely."             />      <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="end"             android:textappearance="@style/textappearance.appcompat.body1"             android:linespacingextra="3.5dp"             android:paddingleft="24dp"             android:layout_marginright="24dp"             android:text="raw&#160;persistence may&#160;be only&#160;option other&#160;than giving&#160;up&#160;entirely."             />      <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="end"             android:textappearance="@style/textappearance.appcompat.body1"             android:linespacingextra="3.5dp"             android:paddingleft="24dp"             android:layout_marginright="24dp"             android:text="@string/raw persistence may option\nother giving entirely."             />  </linearlayout> 

is there way lay out adequately, without having resort multiple strings different device width etc? =\

ok, after examination of textview's code put solution need:

package com.actinarium.persistence.common;  import android.annotation.targetapi; import android.content.context; import android.os.build; import android.text.layout; import android.util.attributeset; import android.widget.textview;  public class blockquotetextview extends textview {     public blockquotetextview(context context) {         super(context);     }      public blockquotetextview(context context, attributeset attrs) {         super(context, attrs);     }      public blockquotetextview(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);     }      @targetapi(build.version_codes.lollipop)     public blockquotetextview(context context, attributeset attrs, int defstyleattr, int defstyleres) {         super(context, attrs, defstyleattr, defstyleres);     }      @override     protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {         super.onmeasure(widthmeasurespec, heightmeasurespec);          // fix width         float max = 0;         layout layout = getlayout();         (int = 0, size = layout.getlinecount(); < size; i++) {             final float linewidth = layout.getlinemax(i);             if (linewidth > max) {                 max = linewidth;             }         }          final int height = getmeasuredheight();         final int width = (int) math.ceil(max) + getcompoundpaddingleft() + getcompoundpaddingright();          setmeasureddimension(width, height);     } } 

the default implementation of onmeasure not take line widths account unless broken down \n's (code). used getlinemax instead of getlinewidth because latter measured trailing whitespace — culprit of misalignment in block #3 in original post.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -