In my chat program, in order to show the length of the voice, I use a custom LinearLayout to display for users, it has two TextView ,one is to show how much time the voice has, the other is to show how long the voice is,
For example: a 10s length voice's TextView width is 20dp, a 5s length voice's TextView width is 10dp.
How can I fix it ?
You can try this way:
textView.getLayoutParams().height = 50;
textView.getLayoutParams().width = 100;
Or if you want to assign values in dp than you can specify height and width values in dimen file and can do it this way:
textView.getLayoutParams().height = (int)getResources().getDimension(R.dimen.textview_height);
textView.getLayoutParams().width = (int)getResources().getDimension(R.dimen.textview_width);
Related
I'm using a (horizontal) PagerView which shows 1 page on the screen, the user can switch pages by swiping left or right.
Every page holds several views in a RelativeLayout, one of them is a description text in a TextView (its size is calculated with weight).
I want the description text to be in the same font size for all pages.
This needs to be according to the longest description (the TextViews in all the pages are in the same height).
I have an array of all the descriptions in advance but I don't have rendered TextViews since they are in different pages and android won't draw it until scrolled to the page.
Any idea how the correct font size could be calculated?
Found the solution so just sharing.
This is easily implemented using StaticLayout. When creating StaticLayout you assign its width and can check its height (it will automatically start the text in a new line if it's too long).
Just iterate over your "texts array", for every line of text create a StaticLayout with your desired width and make sure it's height fits inside your TextView height, if not, decrease the font size and recheck. You'll eventually get the largest font size that fits the TextView size.
Get the 1st textview text size & typeface. Then set that size and typeface to other textviews. Or you can get height & width of the textview & set that to others.
float size = textView1.getTextSize(); //get the size of the textview text
textView2.setTextSize(size) //set the size for other textview's texts
//if you want look in same size, all text must have same TypeFace(Font). To do that use.
textView1.getTypeFace();
textView2.setTypeFace();
//or you can get hight & width of the textview.
textView.setText("GetHeight");
textView.measure(0, 0); //must call measure!
textView.getMeasuredHeight(); //get height
textView.getMeasuredWidth(); //get width
//set textview height & width
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
params.width = 100;
textView.setLayoutParams(params);
I have a TextView with few lines and i need to calculate just the height of one line, not the total TextView height.
TextView's height divided by TextView's rows should give you the height of one row hence every line has the same height with one text size.
Having some imageViews in a list row.
The count of the imageView in the row are different, i.e. could be three or two imageView.
For the width it could be specified using
layout_width="0dp"
layout_weight="1dp"
for every imageView in the row. But the problem is the height, it should be 90% of the width of the imageView if the row has two imageView, and 80% of the width is the row has three imageView.
Is there way to do that in xml?
If not, where is the best place to do calc and set the height without too much overhead on the drawing?
Thanks!
Not sure if there is a way to do in xml. But this works in code, do it in the adapter::getView(), after get the corresponding imagView, change its height layoutParam but keep the width one.
Point size = new Point();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int columnNumber = (isItThree ? 3 : 2);
double ratio = (isItThree ? 0.8 : 0.9)
int desiredImageHeight = screenWidth/colunmNumber * ratio;
ViewGroup.LayoutParams imglayoutParams = imgView.getLayoutParams();
imglayoutParams.height = desiredImageHeight;
imgView.setLayoutParams(imglayoutParams);
In order to maintain the ratio between height and width the best practice is:
Defining one of its property with a value in dp and another with wrap content.
In your case:
android:layout_width="20dp"
android:layout_heignt="wrap_content"
By doing this the height should be adjusted according to the ratio of given width.
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.
EDITED
I have a TextView. I am getting the TextView width programmatically and then setting it to the width of my Layout. Say, the width of my TextView is 60, then i apply it to my Layout using myLayout.getLayoutParams().width. Why there is a lag of width for the Layout and for the TextView i.e. the layout shows different width on screen , not equal to that one of my TextView.
In short, 60 width for TextView is different from 60 width of Layout.
I am getting the width of TextView like this :
int textWidth = getTextWidth(text,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editText.getTextSize(), getResources().getDisplayMetrics()), null);
Why are you stuffing your logic programmatically. Simply use wrap_content in your layout, and it will do all the logic that you are doing in you java class.
Change the width of your layout to android:layout_width = "wrap_content"