I am trying to implement ticker board where you can see all ticker in scrolling mode check below image,
now I want continue scrolling for example
in #user5 after some space and start again with this same value no blank area like in init space
I want like this
scrolling text from one user 3 hours #user3 <some space> scrolling text from...
sorry for bad english
here is my code
View view = parent_layout.getChildAt(index);
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
// gets the screen width
view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
(int)height));
Log.e("contact details","width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());
// performs the calculation
toXDelta = width - (screenWidth - 0);
// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// tickerList.add(view);
// animList.add(mAnimation);
// Animation parameters
mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
// mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
My solution is just create double of string means appending current string with itself if the length is lower then screen width and using marquee feature in Textview.
private float screenWidth;
onCreate(){
TextView tv = (TextView)findViewById(R.id.textview);
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
screenWidth = outSize.x;
StringBuilder ticker = new StringBuilder("This is my String");
do{
tv.setText(Html.fromHtml(ticker.toString()));
int w = ticker.length();
if(screenWidth>w){
ticker.append(" ");
ticker.append(tickerList.get(index));
tv.setText(Html.fromHtml(ticker.toString()));
}else
break;
}while(true);
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:singleLine="true"
android:textSize="18sp"
android:padding="5dp"
android:id="#+id/textView1">
</TextView>
Related
how does one scale an ImageView, yet still have it clip (bottom only) when it/parts of it are outside its parent view?
My code;
XML has 3 elements; image in raw size, view 200px high, view 150px high.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1000px"
android:layout_height="850px"
android:background="#FFFEC6"
android:orientation="vertical">
<!-- single image in raw size -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fauxcard"
/>
<!-- should be SCALED with NO CLIPPING -->
<RelativeLayout
android:id="#+id/Wrapper1"
android:layout_width="1000px"
android:layout_height="200px"
android:background="#cccccc">
</RelativeLayout>
<!-- should be SCALED with the BOTTOM CLIPPED -->
<RelativeLayout
android:id="#+id/Wrapper2"
android:layout_width="1000px"
android:layout_height="150px"
android:background="#C6FFD1"
android:clipChildren="true">
</RelativeLayout>
</LinearLayout>
java;
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.Wrapper1);
RelativeLayout r2 = (RelativeLayout) findViewById(R.id.Wrapper2);
int marginLeft = 0;
for (int i = 0; i < 10; i++)
{
// IV 1 - the parent view for these is the correct 200 high, so these will be unclipped/unscaled
ImageView myImg1 = new ImageView(this);
myImg1.setImageResource(R.drawable.fauxcard);
RelativeLayout.LayoutParams layout1 = new RelativeLayout.LayoutParams(100, 200);
layout1.setMargins(marginLeft, 0, 0, 0);
r1.addView(myImg1, layout1);
// IV 2 - the parent view for these is NOT high enough (only 150 high), so I want these SCALED but CLIPPED (bottom of image clipped)
ImageView myImg2 = new ImageView(this);
myImg2.setImageResource(R.drawable.fauxcard);
// scaling
// myImg2.setScaleType(ImageView.ScaleType.CENTER); // NOT scaled - clipped
// myImg2.setScaleType(ImageView.ScaleType.CENTER_INSIDE); // scaled - NOT clipped
myImg2.setScaleType(ImageView.ScaleType.CENTER_CROP); // scaled - clipped at TOP and bottom
// myImg2.setScaleType(ImageView.ScaleType.FIT_END); // scaled TOO MUCH - clip not needed
// myImg2.setScaleType(ImageView.ScaleType.FIT_START); // scaled TOO MUCH - clip not needed
// myImg2.setScaleType(ImageView.ScaleType.FIT_CENTER); // scaled TOO MUCH - clip not needed
// myImg2.setScaleType(ImageView.ScaleType.FIT_XY); // scaled incorrect ratio - clip not needed
// myImg2.setScaleType(ImageView.ScaleType.MATRIX); // NOT scaled - clipped
RelativeLayout.LayoutParams layout2 = new RelativeLayout.LayoutParams(100, 200);
layout2.setMargins(marginLeft, 0, 0, 0);
r2.addView(myImg2, layout2);
// I am using marginLeft as my real scenario is more complex - there are varying gaps between the images based on other logic
// the important issue is CLIPPING the images whilst scaling them
marginLeft += 100;
}
To both of the RelativeLayouts I'm adding 10 images. I want both sets of images to be scaled the same, but as the 2nd RL isn't high enough I want those images CLIPPED (the bottom of the image gone).
I've tried all ScaleTypes, but none of them achieve this. The closest is CENTER_CROP, which has the correct scaling, but both the top and bottom are clipped (and, again, I just want the bottom clipped).
Here's a image;
https://imgur.com/a/QLJkm
put this code in viewtreeobserver or you can have custom imageview
Matrix matrix = imageView.getImageMatrix();
float scale;
final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
final int drawableWidth = drawable.getIntrinsicWidth();
scale = (float) viewWidth / (float) drawableWidth;
matrix.setScale(scale, scale);
setImageMatrix(matrix);
I have to do some animation in my application but i have a problem with textview.
I need to animate a textview and make it compare from right corner.
this is my layout:
<RelativeLayout
android:id="#+id/ThirdPartBottomLayout"
android:layout_width="2000dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:background="#color/RedTA"
android:paddingTop="20dp"
android:paddingBottom="80dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/ThirdPartText1"
android:textSize="20dp"
android:textColor="#ffffff"
android:lines="1"
android:text="#string/Onboarding_Page3_Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="#+id/ThirdPartText2"
android:textSize="16dp"
android:textColor="#ffffff"
android:layout_below="#+id/ThirdPartText1"
android:text="#string/Onboarding_Page3_Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
and this is where I inizialize variable:
int widhtR1 = 0;
if (ThirdPartText1.Width > WidthPixel - PixelsToDp(50))
widhtR1 = WidthPixel - PixelsToDp(50);
else
widhtR1 = ThirdPartText1.Width;
lp = new RelativeLayout.LayoutParams(widhtR1,
ThirdPartText1.Height);
lp.LeftMargin = WidthPixel;
ThirdPartText1LeftMargin = (WidthPixel - widhtR1) / 2;
ThirdPartText1.LayoutParameters = lp;
int widhtR2 = 0;
if (ThirdPartText2.Width > WidthPixel - PixelsToDp(50))
widhtR2 = WidthPixel - PixelsToDp(50);
else
widhtR2 = ThirdPartText2.Width;
lp = new RelativeLayout.LayoutParams(widhtR2,
ThirdPartText2.Height);
lp.LeftMargin = WidthPixel;
lp.TopMargin = PixelsToDp(10);
lp.AddRule(LayoutRules.Below, Resource.Id.ThirdPartText1);
ThirdPartText2LeftMargin = (WidthPixel - widhtR2) / 2;
ThirdPartText2.LayoutParameters = lp;
To animate i use a ValueAnimator that move LeftMargin from WidhtPixel to the minium left margin of textview.
And I do with this code.
ThirdPartText1Animator = ValueAnimator.OfInt(1);
ThirdPartText1Animator.SetDuration(
ThirdPartText1AnimatorDuration);
ThirdPartText1Animator.SetInterpolator(new
AccelerateDecelerateInterpolator());
var lpTxt1 =
(RelativeLayout.LayoutParams)ThirdPartText1.LayoutParameters;
ThirdPartText1Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT1:" + val);
lpTxt1.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText1LeftMargin) * (val / 100f));
ThirdPartText1.LayoutParameters = lpTxt1;
};
ThirdPartText2Animator = ValueAnimator.OfInt(1);
ThirdPartText2Animator.SetDuration(
ThirdPartText2AnimatorDuration);
ThirdPartText2Animator.SetInterpolator(new
LinearInterpolator());
var lpTxt2 =
(RelativeLayout.LayoutParams)ThirdPartText2.LayoutParameters;
ThirdPartText2Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT2:" + val);
lpTxt2.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText2LeftMargin) * (val / 100f));
ThirdPartText2.LayoutParameters = lpTxt2;
};
/*** START WITH ****/
ThirdPartText1Animator.SetIntValues(0, 100);
ThirdPartText1Animator.Start();
ThirdPartText2Animator.SetIntValues(0, 100);
ThirdPartText2Animator.Start();
And here comes the problem when the animation start, text view compare from right but text will move to fit the textview dimension on screen instead of stay blocked on textview real dimension.
How could I avoid to make text move inside a textview.
Hope my information is enough and sorry for my bad english.
EDIT
WidthPixel = Resources.DisplayMetrics.WidthPixels;
AccelerateDecelerateInterpolator is an Interpolator Android.Views.Animation
Full classes
OnboardingPage.cs
OnboardingPageLayout.axml
Thanks in advance.
Matteo.
For everyone have the same problem i figured out a solution.
In a first time i use left margin to make textview compare, so when application start i set left margin to width of screen and when i need to make it appear i reduce left margin.
It seems that if you change somethings of textview it been forced to redraw everythings so also widht and height change.
To avoid this problem I create a layout and put textview inside it and use the same trick of left margin to layout instead on textview and everythings work.
Sorry for my bad english.
Hope that should be helpful for someone.
I have the following structure within a view hierarchy
ScrollView
LinearLayout (horizontal)
- RelativeLayout (X)
-LinearLayout
-CustomView
- RelativeLayout
- RelativeLayout
- RelativeLayout
- RelativeLayout
- RelativeLayout
Here is a sample of it in the xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="#+id/linLayoutWrapper"
android:layout_width="match_parent"
android:layout_height="1080dp"
android:background="#color/white">
<!--- note this include is a <RelativeLayout> -->
<include
android:id="#+id/dg_axis"
layout="#layout/day_grid_axis"></include>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/grayBorder"></View>
<--- start of repetitive substructure which his higlighted in screen shot -->
<RelativeLayout
android:id="#+id/sundayColumn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="1080dp">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
<com.mynursesstation.views.DayView
android:id="#+id/sundayDayView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/grayBorder"></View>
<!--- repeat above sub structure 6 more times --->
</LinearLayout>
</ScrollView>
where the inner structure (X) is repeated for all other RelativeLayout siblings. The Linear Layout, RelativeLayouts
NOTE: (X) is shown highlighted in its unusual behavior in Red, where as its parent is the entire screen width and is delimited with the grey vertical line above it.
The RelativeLayouts would all be flush to the top of the Linear Layout if all of them were the same size (That is my experimental opinion which has yet to be proved). If one is bigger than the rest, then they by default align to the bottom. My question is, how can These children of the RelativeLayout's exceed their parent when they are defined by the parent? Could it be that I am programmatically creating views whose intrinsic heights exceed the height of their parents? What is android's official handling of this case? Does the view overflow like an HTML DOM element?
UPDATE:
On inspecting each RelativeLayout, my hypothesis that one of them exceeded 3240pixels was wrong. None of them do. Only there seems to be a top margin applied to some (Which would theoretically increase the height of its parent, being wrap_content) but it does not.
The definition of the RelativeLayout called CustomView, is a RelativeLayout within which are programmatically positioned relativelayouts. They are positioned within their parent as follows:
int pixels = (int) (height * scale + 0.5f);
pixels = (pixels + 4) / 5 * 5;
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
columnWidth / maxConflicts,
pixels));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
start = cal2.get(Calendar.HOUR_OF_DAY);
height = start > 0 ? start * 44.000000000f + (start - 1) : start * 44.00000000f;
height += cal2.get(Calendar.MINUTE) / 60.00000000000f;
pixels = (int) (height * scale + 0.5f);
if (offsetAmount != null){
params.setMargins( (columnWidth / maxConflicts) * offsetAmount, pixels, 0, 0);
} else {
params.setMargins(0, pixels, 0, 0);
}
relativeLayout.setLayoutParams(params);
However there is never a case where the top margin of the relativeLayout above plus the height of the of the relativeLayout exceeds 1080dp. I verify this dynamically by logging the height of the CustomView DayView with the following code:
columnHeight = MeasureSpec.getSize(heightMeasureSpec);
which always logs out as 3240 pixels which at a scale of 3 is exactly 1080dp.
So this eliminates the possibility that one of the DayView exceeds 1080 and that is why not all the children of the top level LinearLayout are not flush to its top. However, I still cannot comprehend what could be causing this behavior. All elements programmatically added to the DayView are alignTop to their parent and are positioned with a vertically margin which, with the height of the view being programmatically added` never exceeds 1080dp.
Notice that the red views have a mysterious mTop added even though their dynamic height is 1080dp. mTop + 1080dp > 1080dp yet there parent is 1080dp (it is wrap_content) !
Take a good look at the view which is flush. It also has the same height but has a zero mTop. I don't understand why this is or how can be possible.
enter image description here
UPDATE Saturday:
Good News! i have narrowed down the problem to a subview of a subview, which when omitted, does not cause this strange inconsistency in my layout logic / intention. The culprit is a <TextView> which is programmatically added to the two types of events which are programmatically added <RelativeLayout>s added to DayView. when both types of views have textviews added as subviews it breaks the view hierarchy. When only one is present, the views present as intended. Here is the code for programmatically laying out the calendar events the entirety of which consist of all the subviews of a DayView:
private void createEventLayout(CalendarEvent e, Integer offsetAmount, int maxConflicts) {
final float scale = getContext().getResources().getDisplayMetrics().density;
int idForMyView = e.assignmentId > 0 ? e.assignmentId : -e.conflictId;
RelativeLayout relativeLayout = new RelativeLayout(getContext());
relativeLayout.setId(idForMyView);
Calendar cal2 = Calendar.getInstance();
cal2.setTimeZone(TimeZone.getTimeZone("UTC"));
cal2.setTime(e.startDate);
Calendar cal3 = Calendar.getInstance();
cal3.setTimeZone(TimeZone.getTimeZone("UTC"));
cal3.setTime(e.endDate);
float start = cal2.get(Calendar.HOUR_OF_DAY);
start += cal2.get(Calendar.MINUTE) / 60.0000000f;
float end = cal3.get(Calendar.HOUR_OF_DAY);
end += cal3.get(Calendar.MINUTE) / 60.00000000f;
float height = (end - start) * 45.0000000f;
int pixels = (int) (height * scale + 0.5f);
pixels = (pixels + 4) / 5 * 5;
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
columnWidth / maxConflicts,
pixels));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
start = cal2.get(Calendar.HOUR_OF_DAY);
height = start > 0 ? start * 44.000000000f + (start - 1) : start * 44.00000000f;
height += cal2.get(Calendar.MINUTE) / 60.00000000000f;
pixels = (int) (height * scale + 0.5f);
if (offsetAmount != null){
params.setMargins( (columnWidth / maxConflicts) * offsetAmount, pixels, 0, 0);
} else {
params.setMargins(0, pixels, 0, 0);
}
relativeLayout.setLayoutParams(params);
if (e.assignmentId > 0){
// company name
TextView tv = new TextView(getContext());
tv.setId(idForMyView);
tv.setText(e.companyName);
LayoutParams tvParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvParams.setMargins(0,0,0,0);
tv.setLayoutParams(tvParams);
RelativeLayout.LayoutParams companyNameParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setTypeface(null, Typeface.BOLD);
companyNameParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//time of assignment / conflict
TextView tvTime = new TextView(getContext());
tvTime.setTextColor(Color.parseColor("#ffffff"));
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String startTime = sdf.format(e.startDate);
String endTime = sdf.format(e.endDate);
tvTime.setText(startTime + " - " + endTime);
tvTime.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout.LayoutParams timeParams = (RelativeLayout.LayoutParams) tvTime.getLayoutParams();
timeParams.addRule(RelativeLayout.BELOW, idForMyView);
relativeLayout.addView(tv); // ALSO this will break if present with the other kind
relativeLayout.addView(tvTime); // this too
relativeLayout.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.gradient_background_home));
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final int assignmentId = view.getId();
if (assignmentId > 0) {
Intent intent = new Intent(getContext(), AssignmentDetailActivity.class);
intent.putExtra(getContext().getString(R.string.extra_assignment_id), assignmentId);
getContext().startActivity(intent);
}
}
});
} else {
final CalendarEvent event = e;
relativeLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.conflict));
if (isConflictMode && e.isAllDay == 1){
TextView tv = new TextView(getContext());
tv.setId(idForMyView);
tv.setText(ALL_DAY_TEXT);
LayoutParams tvParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tvParams.setMargins(0,0,0,0);
tv.setLayoutParams(tvParams);
RelativeLayout.LayoutParams companyNameParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setTypeface(null, Typeface.BOLD);
companyNameParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.calendar_small_font));
relativeLayout.addView(tv); //THIS will break the layout logic
}
}
this.addView(relativeLayout);
return;
}
I tried making sure the <TextView> is clipped by its parent by setting
tv.setGravity(Gravity.CLIP_HORIZONTAL | Gravity.CLIP_VERTICAL)
when I inspect it in the view it appears that it is clipping exactly to the bounds of the tv, yet, something seems to be exceeding the height of its parent based on the fact that when the <TextViews> are gone everything addes up, but when they are present, there seems to be too much vertical content.
So my problem was that i was assigning the same id to the <RelativeLayout> which was the particular CalendarEvent as well as to the children of this view, the <TextViews>. This created circular logic which referred to the parent and child which caused the views to not properly by aligned, I think specifically with the BELOW requirement of one of the textViews, being essentially made to be below itself. By creating a unique id with View.generateId() each time I created a <TextView> then the intended layout was made possible.
I'm using this method to calculate the height of listview, but it's not really exactly as I want. Because, my listview have a TextView could be multiline when the content is so long. It's right when TextView is oneline, but when it has 2 lines, it's wrong height. Thank you!
Please see the error in this picture
code
public static boolean setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/red"
android:orientation="vertical">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/border_margin"
android:text="name"
android:paddingLeft="200dp"
android:textColor="#color/header_lounge"
android:textSize="#dimen/text_normal"
android:textStyle="bold" />
</LinearLayout>
All the methods that are available to calculate height of ListView based on children fail when the TextView becomes multiline.
I too faced the same problem and after a lot of tries, I found the solution of this problem.
The main concept is if you set the exact number of lines your TextView is having then the height will be calculated correctly i.e. you any how need to do the following,
textView.setLines(numberOfLines)
With this your problem will be solved.
Now the next question is how you can come to know the exact number of lines your TextView will be having dynamically.
I will say it totally depends on your scenario. In my case what I did was,
textView.setText(fullString);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth(); // Get full screen width
int eightyPercent = (screenWidth * 80) / 100; // Calculate 80% of it
// as my adapter was having almost 80% of the whole screen width
float textWidth = textView.getPaint().measureText(fullString);
// this method will give you the total width required to display total String
int numberOfLines = ((int) textWidth/eightyPercent) + 1;
// calculate number of lines it might take
textView.setLines(numberOfLines);
I want to see the sum of textview's height and of textview's padding.
I set padding with method:
textView.setPadding(0,x,0,x);
But if I use
textView.getHeight();
I will receive 0.
If I use
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
Log.d(TAG,params.height+"");
I will receive -2 that's impossible.
What have I to do?
Edit: This is the XML code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/try"
android:textColor="#fff"
android:id="#+id/tv"
android:gravity="center_horizontal" />
Edit2: I use this code for setting width of text view and it work correctely.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
params.width = width/2;
I don't want to set the height programmately because I set a padding. But I want to get the measure
Edit3:
I try to use
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
as Slartibartfast told me. But there is a problem:
Probably this is the measurement of text size... But it isn't the correct measurement of height of textview. I tried to sum this number and the 2 measure of padding and try to set:
params.height = bounds.height() + 2*x;
The view is smaller than the view without this line of code.
Take a look at : How to retrieve the dimensions of a view?
Basically the size is known only after the layout is finished, one way is to create a custom text view and use onSizeChanged method
EDIT:
How about
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content