0

I am creating a custom TextView which can draw text at both ends like this,and this supports multiline, so that the number of text views can be cut down to half(I got a lint warning, complaining about 80+ views, and most of the views are TextViews in my layout, like first name , last name added in a grid layout)

Please see the screen shot, this is the current state of the view

Screen shot

It will show the text when the height is fixed, i don't want this behavior because the text on the right can be of any length and it should wrap the height to the desired height. This is the overridden onMeasure method

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (mRightText == null) return;
    initStaticLayout();
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mLayout.getHeight());
}

mLayout is a StaticLayout

private void initStaticLayout() {
    if (mLayout == null) {
        mLayout = new StaticLayout(mRightText, mPaint, 
                getWidth() / 2 - getPaddingRight() - getPaddingLeft(),
                Layout.Alignment.ALIGN_NORMAL, 
                1, 0, true);
    }
}

if the height is set to wrap_content it is not drawing anything.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sony
  • 7,136
  • 5
  • 45
  • 68
  • `if (mRightText == null) return;` you have to always call `setMeasuredDimension` – pskink Feb 20 '18 at 11:27
  • the `super.onMeasure(widthMeasureSpec, heightMeasureSpec);` set the measured dimension initially and only if the right text is not null i'll override the measured height and set the dimension again – Sony Feb 20 '18 at 11:30
  • fair enough, you're right... – pskink Feb 20 '18 at 11:31
  • whats the output of `adb shell dumpsys activity top`? post only ui tree part – pskink Feb 20 '18 at 11:33
  • Are you sure that `mLayout` is not set anywhere else before you call `initStaticLayout()`? Also have you tried using `DynamicLayout` instead of `StaticLayout`? – y.allam Feb 20 '18 at 11:35
  • sorry, i am not an expert especially with adb, how can i `adb shell dumpsys activity top` , and the screen shot is from the layout preview – Sony Feb 20 '18 at 11:36
  • btw instead of 40+ grid layout rows why dont you simply use `RecyclerView` / `ListView` ? – pskink Feb 20 '18 at 11:38
  • just launch the command `adb shell dumpsys activity top` – pskink Feb 20 '18 at 11:39
  • Because in every row the data is different, like first name, last name, job, qualification etc, if i use a recyclerView, it will be hard to track at what index first name belongs and it will get worse, if i add a middle name in between the first name and last name, i have to go and change all the indexes in, on bind view holder – Sony Feb 20 '18 at 11:42
  • honestly i dont see any difference from creating recycler view and adapter with 40 items and using grid view with 40 rows – pskink Feb 20 '18 at 11:48
  • one more thing i forgot to say, those 40 views are not one below the other, they are arraged in groups with custom headings and subheadings, at first i also thought about using recyclerview – Sony Feb 20 '18 at 11:51
  • it does not make any difference: you can do that easily with recycler view - `Adapter#getItemViewType()` – pskink Feb 20 '18 at 11:52
  • @pskink i fixed it – Sony Feb 20 '18 at 12:10
  • good, but imho you are following wrong path... you should really use recycler view – pskink Feb 20 '18 at 12:11
  • I will considre changing it to recycler view as you mentioned, but all i want is to make this view works as expected, atleast for fun :-) – Sony Feb 20 '18 at 12:13

2 Answers2

0

instead in xmlfile why can't you create two textviews keeping layout_weight as 1. so that entire screen will be divided into two equal partitions.

vinay
  • 149
  • 7
  • the layout will contain almost 80+ views after the layout design is finished when i use a `GridLayout` and if i set layout weights i have to use one linear layout with horizontal orientation for every left and right text, imagine if i have 40 text views and i have to wrap every two text views in a horizontal linear layout, and that will again increase the view count – Sony Feb 20 '18 at 11:34
  • @Sony use recyclerview to show your data and store data in an arraylist and iterate that data so that it will fit according to your requirement. – vinay Feb 20 '18 at 11:39
  • Because in every row the data is different, like first name, last name, job, qualification etc, if i use a recyclerView, it will be hard to track at what index first name belongs and it will get worse, if i add a middle name in between the first name and last name, i have to go and change all the indexes in, on bind view holder – Sony Feb 20 '18 at 11:43
0

When i tried the view in emulator, I got this exception as soon as the activity is launched,

java.lang.IllegalArgumentException: Layout: -xx < 0

that is from the initStaticLayout regarding the width

mLayout = new StaticLayout(mRightText, mPaint, 
                /*the error was here*/ getWidth() / 2 - getPaddingRight() - getPaddingLeft(),
                Layout.Alignment.ALIGN_NORMAL, 
                1, 0, true);

and i changed it from getWidth() to screenWidth and now i can wrap the height, no matter how big the right text is. I don't know why fixing a width issue fixed a height issue, may be because of the exception , the layout is not able to generate preview.

Thank you @pskink for the adb tip and thank you every one

Sony
  • 7,136
  • 5
  • 45
  • 68