How to get maximum width of multiline TextView in android? - android

I am new in android and facing the problem to get the maximum width of the multiline TextView.
I used textview.getWidth() and textview.getMeasuredWidth() but both gives garbage value.
I used also textview.length(), but retuns zero.
Please note that i have not set the maximum width of the TextView.I want to get it runtime.

TextView_name.setLayoutParams(new LayoutParams(250,LayoutParams.WRAP_CONTENT));
by using the above code we can set the width for textview dynamically

I would try to use getWidth(). If you want to do that though, you have to be careful when you call it. It only makes sense after the view hierarchy is laid out. That means that trying to check onCreate, onStart or even onResume will probably not get the results that you want. As an experiment, you can call it in a click handler of a button and you should see the expected value correctly being returned. I can't really say any more about where you should get it without knowing what you want to use it for.

Related

Changing appearance of RecyclerView row on interact?

In my bindRow method of my RecyclerView adapter I am checking to see if an EditText has been modified such that its value falls below 100. If so, I change the color of the text to red.
However the text does not update immediately unless I rotate the screen.
How do I get it to change the color immediately?
From my experience, this isn't the place where I would be doing this. I have had a similar concern where the ViewHolders in my Adapter weren't accurate - and then rotating the screen 'fixed' it.
My solution involved updating my Adapter's data List<> and then calling swapAdapter(...)

RecyclerView parameters stick around after re-drawing the list

So, I've got a RecyclerView with some textviews on the cards. One of these can turn red&bold, based on some parameters, using holder.DeviceTV.setTextColor(Color.RED); & holder.DeviceTV.setTypeface(null, Typeface.BOLD); This works perfectly fine, as you can see in the image below, above the black bar.
Later, I remove all the cards with the red&bold textview, and notify the adapter. This results in what you see below the black bar in the image, which should NOT be the case. I'm guessing this is because (duh) this is a RecyclerView, so the parameters I set on it before, have stayed around. I don't know why it chooses to use the cards with the red&bold text, but it does, every time.
What is the best way to fix this issue?
You would need to call holder.DeviceTV.setTextColor(Color.BLACK) and holder.DeviceTV.setTypeface(null); in the onBindViewHolder method to make sure everything is displayed as you want.
There might be a small overhead to that but it's definitely much faster than creating a new View from scratch.

Empty gridlines in android gridview?

I want a gridview with empty grid lines as follows
Here is what i got till now, gridview with gridlines in the occupied cells only. I want gridlines in empty cells also. Is this possible?
I'm almost positive a GridView was never intended for this, so you have two options.
-Write your own GridView that supports a defaulted view.
OR
-If this is not a dynamic view that is changing all the time in real time:
In your adapter, set a minimum in your getCount method (return Math.max(actualSize, minimumCount)). And set the views with no data to your empty boxes.
Make sure the count is always some mod of 4 to ensure each row will be filled beyond that.
That's just what I'm coming up with on the top of my head, there's most likely a better way to do it, but hopefully I'm moving you in the right direction.
Try putting an empy View within the empty cells of the grid.
For example, use a TextView with no text, or simply a new View().

Android Spinner.setSelection(int, false) creates messed up Spinners

after using setSelection(int, false) like suggested here because i had troubles using the default setSelection(int) for initial setup it turns out that using the two param version messes up the spinner layout till the first manual selection takes place, details see image below.
Is there a way to "update" the spinner layout?
Okay i got it. I extedned the Spinner Class, added a var for saveing that this is the "first" pass and have overrwitten the onDraw method. after super.OnDraw() is called i can be sure that the layout has been drawn the first time and all data is passed to the spinner so to following requestLayout() will fix any layout errors. so i just test if this is the first onDraw with my var, if so i call requestLayout() and set the var to false. it's not the best way and maybe there is another event i could use that is run bevore the draw happens, but it's good enough for my needs.

How to get around the text limitations of a TextView element?

Apparently there seems to be a text length limit on the TextView element when used within a ListView. I have to admit I have not checked the actual limit that I am facing, but it is clear to me that 2k characters is no biggie, where 4k characters is simply not being displayed.
And "not displayed" is not even right, as the screen seems to be scrollable for the full length of the TextView (although all I get to see is whitespace).
My issue: I am displaying a ListView for an email message. All parts of the mail are in seperate TextView elements that get dropped into the ListView.
When a lengthy message (somewhere above 2k characters) is put into a TextView, the TextView remains empty but does occupy space.
My question: What alternatives/options are there to make this problem go away?
Eg Making the entire message displayable (even long ones).
Thanks to anyone in advance for assistance.
Is there any reason you have to use a ListView? It seems like you'd be better served with a LinearLayout or RelativeLayout inside a ScrollView, or maybe just put the body of the email inside a ScrollView if you want to keep the other TextViews on the screen.
If you want to stick with the ListView, you might consider using String#substring(int beginIndex, int endIndex) to ellipsize the body and show the full body in another Activity launched in the onListItemClick handler for the list view. You can use the android:ellipsize property in your XML layout (or TextView#setEllipsize in Java code) to limit the text to a single line, but that is probably not what you want.
you can set the singleLine property to true so that only the text visible in single line can be shown on listview or in addition set the maxline property to 1
Have you tried to set android:maxLength property for TextView element in xml
or
to set filter new InputFilter.LengthFilter(maxlength)?
I have found the reason for this quirk!
This is a BUG that exists in Android < 2.2 !
After dziobas pointed out that he filled 10k char strings on 2.2, I tried this out on a new emulator image.
I am still going to change my code in the end to not use the ListView and reimplement my own ListView like class as Brian suggested

Categories

Resources