I'm implementing a ListView where the elements are expanded by clicking on them to show all the text. I'm using this implementation.
As you can see that project was made considering hardcoded strings and one of the arguments is the height of the TextView after the expansion. Since I can't know the final height o the TV because my strings are fetched from the internet I set the expanded height to:
AbsListView.LayoutParams.WRAP_CONTENT;
As you may know WRAP_CONTENT to android is just a -2 Integer. My problem is that the -2 is passed to the animaton method which does the following calculation:
float height = (ToHeight - FromHeight) * interpolatedTime + FromHeight;
Because ToHeight is -2 (and assuming FromHeight is 200) the TextView animation height goes someting like this:
200.0
191.05173
175.801
158.36632
134.70096
105.341835
78.51846
53.146957
31.025742
9.73967
-1.203598
-2.0
So the animation gives the impression is closing but after that height gets just fine because is -2 (WRAP_CONTENT). How can I solve this?
You may need to show some more code or your XML file that contains the TextView you're trying to animate. A solution that might work for you is to dynamically calculate the height your TextView should be when expanded. You can use TextView.getLineCount() to get the number of lines of text and multiply that with TextView.getLineHeight() to get the size, or at least something close, of how tall the TextView should expand out to.
In general, the WRAP_CONTENT constant is used as a flag for Android's framework to specify dimensions during layout.
Related
I am using ListBox with dynamically creatable ListBoxItems (Wrap enabled) to display some text. I have a problem to calculate Heigth property of ListBoxItem in code to make sure my string are fully visible in ListBoxItem rectangle. Now I am doing it like this:
I am put Label in form (opacy = 0) with autosize = true (a call it measure_Lbl), then I am prepared to create ListBoxtem, I put my text into this Label and read Width property. Then I am divide it by ListBowItem.Width and find some approximate value (may be bigger may be less). But I think there is a better way to do so...?
I set the button's height via XML. Suppose it to be X
<Button
android:layout_height="wrap_contnet"
android:textSize = " <X> dp"
....
....
/>
If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-
As shown above, text size is so small but still height doesn't decrease!
How can I force it to be of appropriate height?
Thanx in advance!
Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe
EDIT
wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button
EDIT 2
As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview
Just Set android:minHeight="xxdp"
There is one ImageView in my homescreen widget. In the layout file, I am setting the height and width to wrap_content. But depending on user's input, I have to change its height and width to match_parent. How can this be done ?
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Now I tried to use the setInt() method on this RemoteViews object like this to just check if it is possible to change height and width:
rv.setInt(R.id.widgetImageView, "setMinimumHeight", 300);
But this is what I get on the logcat:
couldn't find any view, using error view
android.widget.ImageView can't use method with RemoteViews: setMinimumHeight(int)
So how do I change its height and width to match_parent ?
For those wondering how this can be done, I used two ImageView in the layout. One with height and width set to wrap_content and another with height and width set to match_parent. Depending on the user's input, I called the setVisibility to hide the ImageView which was not required through RemoteView.
rv.setInt(R.id.widgetImageView1, "setVisibility", View.GONE);
There are a very few ImageView methods which we can call through RemoteView.
Use:
ImageView view = new ImageView();
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
First parameter of LayoutParams is for width, second for height.
Here, I have created a new ImageView for explanation. You should use your own ImageView variable from your code, to do the above. I hope that's clear.
[Update to code]: Sorry, the MATCH_PARENT variable is under LayoutParams not under View. I've updated the code.
[Update]: Above answer does not work for RemoteViews as RemoteViews cannot have Layout Params. Based on the answer given in this SO Question, there is no way to set or find dimensions for RemoteViews by normal methods.
I found this Android Widget Tutorial, where under the section Widget Size, the author says:
A Widget will take a certain amount of cells on the homescreen. A cell is
usually used to display the icon of one application. As a calculation rule
you should define the size of the widget with the formula:
((Number of columns / rows)* 74) - 2.
These are device independent pixels and the -2 is used to avoid rounding issues.
As of Android 3.1 a Widgets can be flexible in size, e.g. the user can make it
larger or smaller. To enable this for Widgets you can use the
android:resizeMode="horizontal|vertical" attribute in the XML configuration file
for the widget.
From this, I can suggest you that instead of setting a specific integer size value directly, why not set the number of rows and columns to resize? For example, if your initial widget size was 4 Columns and 1 Row, then on user input, you can change it to 4 Columns and 4 Rows, thus making it occupy the whole screen (Max size for widgets is 4 Rows and 4 Columns)
I know the above method is not what you wanted, but this seems like the only way to me. Try and see if it's helpful.
I have built a ListView and my items - at least in part - contain titles of various (text) lengths.
In order to enable the user to read as much of the title as possible, I'm trying to change my adapter to auto-pick a feasible font size for my texts.
So I'm working with the TextView's paint object to measure the text in a basline font size (14dp) and try to compare against the available space. If the text is too big, I reduce the font size to 12dp (later I might think about reducing it even further).
// Note: vh.filmTitleTextView is my TextView, filmText contains the title I want to display
filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
float textWidth = vh.filmTitleTextView.getPaint().measureText(filmText);
if (textWidth > vh.filmTitleTextView.getWidth())
vh.filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
The issue is that on first run, vh.filmTitleTextView.getWidth() always returns zero. I guess this is because the layout has not been rendered before and the size is not yet known.
I can't just go with the full size of the ListView because the textView doesn't have the same width (despite the fact that it is set to layout_width="fill_parent") - there are some elements around it.
Any ideas?
Had a similar problem that was my bane for a long time - this might help ya: Auto Scale TextView Text to Fit within Bounds
I'm attempting to build a layout programmatically.
This layout is dynamic. Based on the data read from a database, it could look different every time.
I have a LinearLayout with its orientation set to vertical.
I want to fit as many TextViews with text (the data from database) as I can on a "row".
What I'm doing is building LinearLayouts that are the rows. These LinearLayouts are populated with TextViews. I build the TextView, set the text, and then check the width to see if it will fit on this row (by subtracting the sum of all TextView widths from the screen width and see if the new TextView will fit). If not, I create a new LinearLayout and start adding TextViews to that one.
The problem is myTextView.getWidth() and myTextView.getMeasuredWidth() both return 0.
Why? How can I get the width of the TextView?
Well first of all, please post your code. Second the getWidth/getHeight returning zero question gets asked A LOT so you could search SO for more answers.
Assuming your calling these methods during onCreate (which is probably the case), the UI hasn't been drawn yet so the returned value is zero. You can't get the width or height of the view until it has been drawn.
You can use this library to schedule the task of perform calculation on the width to the correct time after the view had been completely drawn
You can call it on onCreate() and from any Thread
https://github.com/Mohamed-Fadel/MainThreadScheduler
Sample of use:
MainThreadScheduler.scheduleWhenIdle(new Runnable() {
#Override
public void run() {
int width = textview.getWidth();
int height = textview.getHeight();
textview.setText( String.valueOf( width +","+ height ));
}
});