Related
I have created a custom view that only extends the View class. The custom view works perfectly, except when being used inside a RecyclerView. This is the custom view:
public class KdaBar extends View {
private int mKillCount, mDeathCount, mAssistCount;
private int mKillColor, mDeathColor, mAssistColor;
private int mViewWidth, mViewHeight;
private Paint mKillBarPaint, mDeathBarPaint, mAssistBarPaint, mBgPaint;
private float mKillPart, mDeathPart, mAssistPart;
public KdaBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.KdaBar,
0, 0);
try {
mKillCount = a.getInt(R.styleable.KdaBar_killCount, 0);
mDeathCount = a.getInt(R.styleable.KdaBar_deathCount, 0);
mAssistCount = a.getInt(R.styleable.KdaBar_assistCount, 0);
mKillColor = a.getColor(R.styleable.KdaBar_killBarColor, ContextCompat.getColor(getContext(), R.color.kill_score_color));
mDeathColor = a.getColor(R.styleable.KdaBar_deathBarColor, ContextCompat.getColor(getContext(), R.color.death_score_color));
mAssistColor = a.getColor(R.styleable.KdaBar_assistBarColor, ContextCompat.getColor(getContext(), R.color.assist_score_color));
} finally {
a.recycle();
}
init();
}
public void setValues(int killCount, int deathCount, int assistCount) {
mKillCount = killCount;
mDeathCount = deathCount;
mAssistCount = assistCount;
invalidate();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0f, 0f, mViewWidth, mViewHeight, mBgPaint);
canvas.drawRect(mKillPart+mDeathPart, 0f, mKillPart+mDeathPart+mAssistPart, mViewHeight, mAssistBarPaint);
canvas.drawRect(mKillPart, 0f, mKillPart+mDeathPart, mViewHeight, mDeathBarPaint);
canvas.drawRect(0f, 0f, mKillPart, mViewHeight, mKillBarPaint);
}
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
mViewWidth = xNew;
mViewHeight = yNew;
float total = mKillCount + mDeathCount + mAssistCount;
mKillPart = (mKillCount/total) * mViewWidth;
mDeathPart = (mDeathCount/total) * mViewWidth;
mAssistPart = (mAssistCount/total) * mViewWidth;
}
private void init() {
mKillBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mKillBarPaint.setColor(mKillColor);
mDeathBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mDeathBarPaint.setColor(mDeathColor);
mAssistBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mAssistBarPaint.setColor(mAssistColor);
mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBgPaint.setColor(ContextCompat.getColor(getContext(), R.color.transparent));
}
}
The linked image is what the custom view currently looks like (The custom view is the rectangle above the numbers at the center) http://imgur.com/a/Ib5Yl
The numbers below that bar represents their value (They are color-coded in case you haven't noticed). It is obvious that a value of zero on the first item shouldn't show a blue bar on the custom view. Weird, I know.
The method below is where the values are set (it is inside the RecyclerView.Adapter<>):
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
MatchHistory.Match item = mDataset.get(position);
MatchHistory.MatchPlayer[] players = item.getPlayers();
for(MatchHistory.MatchPlayer player: players) {
int steamId32 = (int) Long.parseLong(mCurrentPlayer.getSteamId());
if (steamId32 == player.getAccountId()) {
mCurrentMatchPlayer = player;
}
}
...
holder.mKdaBar.setValues(mCurrentMatchPlayer.getKills(), mCurrentMatchPlayer.getDeaths(), mCurrentMatchPlayer.getAssists());
...
}
This is the onCreateViewHolder:
#Override
public MatchesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_match_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
and the ViewHolder class:
public static class ViewHolder extends RecyclerView.ViewHolder {
KdaBar mKdaBar;
public ViewHolder(View v) {
super(v);
...
mKdaBar = (KdaBar) v.findViewById(R.id.kda_bar);
...
}
}
I think it is useful to note that the dataset being used by the adapter changes the position of the items from time to time (since it is being fetched all at the same time but are inserted so that the dataset is ordered). I almost forgot that I also tested not changing the positions of the items inside the dataset, but still there aren't any good results. If you checked the image, you can see that there are other info inside the items and I am 100% sure those are all correct with the exception of the data in the custom view.
I am thinking that I am forgetting some methods that must be overridden but I already saw a lot of tutorials and none of them mentioned about this issue. Looking forward to solving this issue. TIA!
It is pretty hard to tell what is going on exactly especially if this code is working elsewhere, but I'll take a couple guesses.
The main things I noticed:
Comparing int from long where numbers are dangerously close to max
Calling Invalidate from a View inside a RecyclerView (especially onBindView)
Issue 1
In your picture, I'm guessing you are the steamId which are the numbers on the bottom left corner of each RecyclerView's view holder, for example: '2563966339'. You should know that "usually" in Android, Integer.MAX_VALUE = 2147483647. This pretty much means you should use long or things won't be equal when you think they are... (so maybe the boxes are being drawn correctly, but you just don't think the steamId at position 0 is the guy you think?!?!).
(If you want to learn more about it just looked up signed vs usigned bytes for int and long).
So you might have to change some code, but I recommend using long or Long. Two of Many Possibilities Below
Example 1
long steamId32 = Long.parseLong(mCurrentPlayer.getSteamId());
if (steamId32 == player.getAccountId()) {
mCurrentMatchPlayer = player;
}
Example 2
Long steamId32 = mCurrentPlayer.getSteamId();
if (steamId32.equals(player.getAccountId()) {
mCurrentMatchPlayer = player;
}
Issue 2:
A lack of understanding of how RecyclerView works might be causing some problems. In onBindView, you should setup and draw the view as much as possible (without calling invalidate()). This is because RecyclerView is meant to handle all 'recycling'. So you invalidate() call might be causing some strange problems.
I know that onDraw() isn't normally called every time a view is bound, but only upon creation with RecyclerView. This would explain why it worked elsewhere!
Summary and Analysis:
Number 1:
I would call (inside onBindView before setValues)
Log.d("Whatever", "At position: " + position + " we have " + <steamId> + <kills> + <other desired info>).
After you scroll up and down you will see the person on the top and what values are being called and see if it is a problem mentioned in #1 or a problem with your position. If the person should have 0, then let position 0 show 0 kills.
This could also point out one of these problems that I didn't think were as likely, but definitely possible:
I still don't know what mCurrentPlayer is exactly which could cause a problem. Also, if you need to update a 'item' in the adapter simply call mAdapter.updateItemAt(position) from the Activity/Fragment with recyclerView. If you have to move it call mAdapter.notifyItemMoved(fromPos, toPos). All these mean that maybe things aren't what you think when onBindView is being called.
Number 2:
I would recommend putting Log statements also in onDraw() to see if you know when it is ACTUALLY being called, and not just expect it after invalidate(). Most likely invaidate() is being queued by the main thread / recycler view until it decides it wantes to call onDraw().
(Because it already created/drew the item in onCreateView())
You might be surprised by what RecyclerView, LayoutManager, and the Adapter do and how they call the view methods. (You might also just want to put Log statements in onBindView and onCreateView to understand the whole process with onDraw()).
Understanding RecyclerView (and it's parts)
Videos to Learn Basics:
RecyclerView ins and outs - Google I/O 2016
RecyclerView Animations and Behind the Scenes (Android Dev Summit
2015)
And for the readers, Android documentation provided this summary:
Adapter: A subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set.
Position: The position of a data item within an Adapter.
Index: The index of an attached child view as used in a call to getChildAt(int). Contrast with Position.
Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
Scrap (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
Dirty (view): A child view that must be rebound by the adapter before being displayed.
RecyclerView
RecyclerView.ViewHolder
RecyclerView.LayoutManager
RecyclerView.Adapter
The problem is not with the dataset but with my understanding of how RecyclerView works underneath (just as napkinsterror have mentioned in his answer).
This it the revised custom view:
public class KdaBar extends View {
private int mKillCount, mDeathCount, mAssistCount;
private int mKillColor, mDeathColor, mAssistColor;
private int mViewWidth, mViewHeight;
private Paint mKillBarPaint, mDeathBarPaint, mAssistBarPaint, mBgPaint;
private float mKillPart, mDeathPart, mAssistPart;
public KdaBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.KdaBar,
0, 0);
try {
mKillCount = a.getInt(R.styleable.KdaBar_killCount, 0);
mDeathCount = a.getInt(R.styleable.KdaBar_deathCount, 0);
mAssistCount = a.getInt(R.styleable.KdaBar_assistCount, 0);
mKillColor = a.getColor(R.styleable.KdaBar_killBarColor, ContextCompat.getColor(getContext(), R.color.kill_score_color));
mDeathColor = a.getColor(R.styleable.KdaBar_deathBarColor, ContextCompat.getColor(getContext(), R.color.death_score_color));
mAssistColor = a.getColor(R.styleable.KdaBar_assistBarColor, ContextCompat.getColor(getContext(), R.color.assist_score_color));
} finally {
a.recycle();
}
init();
}
public void setValues(int killCount, int deathCount, int assistCount) {
mKillCount = killCount;
mDeathCount = deathCount;
mAssistCount = assistCount;
}
private void calculatePartitions() {
float total = mKillCount + mDeathCount + mAssistCount;
mKillPart = (mKillCount/total) * mViewWidth;
mDeathPart = (mDeathCount/total) * mViewWidth;
mAssistPart = (mAssistCount/total) * mViewWidth;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
calculatePartitions();
canvas.drawRect(mKillPart+mDeathPart, 0f, mKillPart+mDeathPart+mAssistPart, mViewHeight, mAssistBarPaint);
canvas.drawRect(mKillPart, 0f, mKillPart+mDeathPart, mViewHeight, mDeathBarPaint);
canvas.drawRect(0f, 0f, mKillPart, mViewHeight, mKillBarPaint);
}
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
mViewWidth = xNew;
mViewHeight = yNew;
}
private void init() {
mKillBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mKillBarPaint.setColor(mKillColor);
mDeathBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mDeathBarPaint.setColor(mDeathColor);
mAssistBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mAssistBarPaint.setColor(mAssistColor);
mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBgPaint.setColor(ContextCompat.getColor(getContext(), R.color.transparent));
}
}
These are the changes I made:
Removed the invalidate() call from inside the setValues() since the onDraw() callback is invoked when the parent adds a view.
Moved the assignment of mKillPart, mDeathPart, and mAssistPart to calculatePartitions() which is, in turn, called inside onDraw(). This is because the values needed for the calculation are asssured to be complete inside onDraw(). This will be explained below.
This is what I've gathered from Mr. napkinsterror's answer:
When the LayoutManager asks the RecyclerView for a view, ultimately, the onBindViewHolder() method is called. Within that method, data is bound to the views, thus setValues() is called.
The view is returned to the LayoutManager, which will then add the item back to the RecyclerView. This event will trigger onSizeChanged() because the dimensions of the view are not known yet. That's where the mViewWidth and mViewHeight are retrieved. At this point, all the necessary values for calculatePartitions() are complete.
onDraw() is also called because the parent just added an item (check this image). calculatePartitions() is called inside onDraw() and the view will be drawn on the canvas without any problem.
The reason I get wrong values before is because I do the calculatePartitions() inside onSizeChanged() which is very, very wrong since mViewWidth and mViewHeight are yet to be known.
I will mark this as the answer but many thanks to mr. napkinsterror for providing resources so that I can research in the right direction. :)
I am currently optimising a complex view containing many nested views. therefor i have created a custom layout extending the relativelayout class.
Based on data set from outside the class i further build the view with childs.
The child building is done within the custom layout. I got it working and the performance gain is enourmous. But before i can create and add the childs i need to know the width of the view.
There are several ways of getting the width of the view:
1 add global layout listener
public void init(){
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//width is known -> create child views
}
});
}
This solution does not always fire an event. For example when the view is inside a fragment and restored from a backstack state. Also there seem to be a 100 to 500ms delay before this event is triggered.
in onMeasure
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
w = MeasureSpec.getSize(widthMeasureSpec);
h = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(w, h);
post(new Runnable() {
#Override
public void run() {
//width is known -> create child views
MyCustomViewGroup.this.postInvalidate();
}
});
}
The difficulty here is that adding views inside onmeasure will result into a call to onmeasure again. And endless loop is the result. Logic needed to prevent this. Could't figure out how.
3 add views in onLayout
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//width is known -> create child views
}
Various unexpected layout problems. Views not respecting layoutparams and showing weird behaviour. Not really sure how to solve it.
hacky timer implementation
private Handler ha = new Handler();
private Runnable r;
public void init() {
if (w > 0) {
//width is known ->create childs
return;
}
r = new Runnable() {
private long time = 0;
#Override
public void run() {
Log.d(TAG, "run");
if (w > 0) {
//width is known ->create childs
} else {
init(); //width is not known -> check later
}
}
};
ha.postDelayed(r, 5);
}
Ironically the last solution works best for me.
I know its a hell of a hack.
is there is anyone out there knowing alternatives? or can give me tips.
From my understanding, there's no API for developers to determine when AdapterView's are getting redrawn.
We call notifyDataSetChanged() and then, at some point in the future, with no event for us to listen for, the ListView redraws it's views.
I say this because I've encountered a situation where I am updating images in a ListView when the scroll has stopped.
Every time I set a new list source - i.e. call notifyDataSetChanged() from my adapter, I then call my updateImagesInView() method - kind of like this:
//MyListView.java
public void setDataSource(SomeClass dataSource) {
((MyListAdapter)myListView.getAdapter()).setSomeDataSource(dataSource);
updateImagesInView();
}
public void updateImagesInView() {
for (int i = 0; i <= mListView.getLastVisiblePosition() - mListView.getFirstVisiblePosition(); i++) {
View listItemView = mListView.getChildAt(i);
...
}
}
//MyListAdapater.java
public void setSomeDataSource(SomeClass dataSource) {
mDataSource = dataSource;
notifyDataSetChanged();
}
The child views I get from the loop in the updateImagesInView method always belong to the previous dataSource.
I've hacked in a workaround, so I'm not looking for a "how to do this" answer, but more along the lines of - is there anyway to know when the views in a ListView have actually been updated after calling notifyDataSetChanged()? (or am I just doing something crazy wrong because the views should effectively be updated immediately after calling notifyDataSetChanged()?)
Well you can add a listener to yourListView's layout like:
mListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mListView.removeOnLayoutChangeListener(this);
Log.e(TAG, "updated");
}
});
mAdapter.notifyDataSetChanged();
Otherwise you should listen on your adapter, as when notifyDataSetChanged is called, your adapter gets calls to getView() to update all the views that are currently visible.
I want to get the number of lines of a text view
textView.setText("Test line 1 Test line 2 Test line 3 Test line 4 Test line 5.............")
textView.getLineCount(); always returns zero
Then I have also tried:
ViewTreeObserver vto = this.textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
ViewTreeObserver obs = textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
System.out.println(": " + textView.getLineCount());
}
});
It returns the exact output.
But this works only for a static layout.
When I am inflating the layout dynamically this doesn't work anymore.
How could I find the number of line in a TextView?
I was able to get getLineCount() to not return 0 using a post, like this:
textview.setText(“Some text”);
textview.post(new Runnable() {
#Override
public void run() {
int lineCount = textview.getLineCount();
// Use lineCount here
}
});
As mentioned in this post,
getLineCount() will give you the correct number of lines only after
a layout pass.
It means that you need to render the TextView first before invoking the getLineCount() method.
ViewTreeObserver is not so reliable especially when using dynamic layouts such as ListView.
Let's assume:
1. You will do some work depending on the lines of TextView.
2. The work is not very urgent and can be done later.
Here is my solution:
public class LayoutedTextView extends TextView {
public LayoutedTextView(Context context) {
super(context);
}
public LayoutedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LayoutedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public interface OnLayoutListener {
void onLayouted(TextView view);
}
private OnLayoutListener mOnLayoutListener;
public void setOnLayoutListener(OnLayoutListener listener) {
mOnLayoutListener = listener;
}
#Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mOnLayoutListener != null) {
mOnLayoutListener.onLayouted(this);
}
}
}
Usage:
LayoutedTextView tv = new LayoutedTextView(context);
tv.setOnLayoutListener(new OnLayoutListener() {
#Override
public void onLayouted(TextView view) {
int lineCount = view.getLineCount();
// do your work
}
});
textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
// Remove listener because we don't want this called before _every_ frame
textView.getViewTreeObserver().removeOnPreDrawListener(this)
// Drawing happens after layout so we can assume getLineCount() returns the correct value
if(textView.getLineCount() > 2) {
// Do whatever you want in case text view has more than 2 lines
}
return true; // true because we don't want to skip this frame
}
});
I think the crux of this question is that people want to be able to find out the size of a TextView in advance so that they can dynamically resize it to nicely fit the text. A typical use might be to create talk bubbles (at least that was what I was working on).
I tried several solutions, including use of getTextBounds() and measureText() as discussed here. Unfortunately, both methods are slightly inexact and have no way to account for line breaks and unused linespace. So, I gave up on that approach.
That leaves getLineCount(), whose problem is that you have to "render" the text before getLineCount() will give you the number of lines, which makes it a chicken-and-egg situation. I read various solutions involving listeners and layouts, but just couldn't believe that there wasn't something simpler.
After fiddling for two days, I finally found what I was looking for (at least it works for me). It all comes down to what it means to "render" the text. It doesn't mean that the text has to appear onscreen, only that it has to be prepared for display internally. This happens whenever a call is made directly to invalidate() or indirectly as when you do a setText() on your TextView, which calls invalidate() for you since the view has changed appearance.
Anyway, here's the key code (assume you already know the talk bubble's lineWidth and lineHeight of a single line based on the font):
TextView talkBubble;
// No peeking while we set the bubble up.
talkBubble.setVisibility( View.INVISIBLE );
// I use FrameLayouts so my talk bubbles can overlap
// lineHeight is just a filler at this point
talkBubble.setLayoutParams( new FrameLayout.LayoutParams( lineWidth, lineHeight ) );
// setText() calls invalidate(), which makes getLineCount() do the right thing.
talkBubble.setText( "This is the string we want to dynamically deal with." );
int lineCount = getLineCount();
// Now we set the real size of the talkBubble.
talkBubble.setLayoutParams( new FrameLayout.LayoutParams( lineWidth, lineCount * lineHeight ) );
talkBubble.setVisibility( View.VISIBLE );
Anyway, that's it. The next redraw will give a bubble tailor-made for your text.
Note: In the actual program, I use a separate bubble for determining lines of text so that I can resize my real bubble dynamically both in terms of length and width. This allows me to shrink my bubbles left-to-right for short statements, etc.
Enjoy!
You could also use PrecomputedTextCompat for getting the number of lines.
Regular method:
fun getTextLineCount(textView: TextView, text: String, lineCount: (Int) -> (Unit)) {
val params: PrecomputedTextCompat.Params = TextViewCompat.getTextMetricsParams(textView)
val ref: WeakReference<TextView>? = WeakReference(textView)
GlobalScope.launch(Dispatchers.Default) {
val text = PrecomputedTextCompat.create(text, params)
GlobalScope.launch(Dispatchers.Main) {
ref?.get()?.let { textView ->
TextViewCompat.setPrecomputedText(textView, text)
lineCount.invoke(textView.lineCount)
}
}
}
}
Call this method:
getTextLineCount(textView, "Test line 1 Test line 2 Test line 3 Test line 4 Test line 5.............") { lineCount ->
//count of lines is stored in lineCount variable
}
Or maybe you can create extension method for it like this:
fun TextView.getTextLineCount(text: String, lineCount: (Int) -> (Unit)) {
val params: PrecomputedTextCompat.Params = TextViewCompat.getTextMetricsParams(this)
val ref: WeakReference<TextView>? = WeakReference(this)
GlobalScope.launch(Dispatchers.Default) {
val text = PrecomputedTextCompat.create(text, params)
GlobalScope.launch(Dispatchers.Main) {
ref?.get()?.let { textView ->
TextViewCompat.setPrecomputedText(textView, text)
lineCount.invoke(textView.lineCount)
}
}
}
}
and then you call it like this:
textView.getTextLineCount("Test line 1 Test line 2 Test line 3 Test line 4 Test line 5.............") { lineCount ->
//count of lines is stored in lineCount variable
}
Based on #secnelis idea, there is even a more clean way if you target API 11 or higher.
Instead of extending a TextView you can use already built-in functionality if View.OnLayoutChangeListener
In ListAdapter.getView(), for instance
if (h.mTitle.getLineCount() == 0 && h.mTitle.getText().length() != 0) {
h.mTitle.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(final View v, final int left, final int top,
final int right, final int bottom, final int oldLeft,
final int oldTop, final int oldRight, final int oldBottom) {
h.mTitle.removeOnLayoutChangeListener(this);
final int count = h.mTitle.getLineCount();
// do stuff
}
});
} else {
final int count = h.mTitle.getLineCount();
// do stuff
}
You can also calculate the amount of lines through this function:
private fun countLines(textView: TextView): Int {
return Math.ceil(textView.paint.measureText(textView.text.toString()) /
textView.measuredWidth.toDouble()).toInt()
}
Keep in mind that It may not work very well on a RecyclerView though.
textview.getText().toString().split(System.getProperty("line.separator")).length
It works fine for me to get number of lines of TextView.
Are you doing this onCreate? The Views aren't laid out yet, so getLineCount() is 0 for a while. If you do this later in the Window LifeCycle, you'll get your line count. You'll have a hard time doing it onCreate, but onWindowFocusChanged with hasFocus=true usually has the Views measured by now.
The textView.post() suggestion is also a good one
I am trying to apply an animation to a view in my Android app after my activity is created. To do this, I need to determine the current size of the view, and then set up an animation to scale from the current size to the new size. This part must be done at runtime, since the view scales to different sizes depending on input from the user. My layout is defined in XML.
This seems like an easy task, and there are lots of SO questions regarding this though none which solved my problem, obviously. So perhaps I am missing something obvious. I get a handle to my view by:
ImageView myView = (ImageView) getWindow().findViewById(R.id.MyViewID);
This works fine, but when calling getWidth(), getHeight(), getMeasuredWidth(), getLayoutParams().width, etc., they all return 0. I have also tried manually calling measure() on the view followed by a call to getMeasuredWidth(), but that has no effect.
I have tried calling these methods and inspecting the object in the debugger in my activity's onCreate() and in onPostCreate(). How can I figure out the exact dimensions of this view at runtime?
Use the ViewTreeObserver on the View to wait for the first layout. Only after the first layout will getWidth()/getHeight()/getMeasuredWidth()/getMeasuredHeight() work.
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
viewWidth = view.getWidth();
viewHeight = view.getHeight();
}
});
}
There are actually multiple solutions, depending on the scenario:
The safe method, will work just before drawing the view, after the layout phase has finished:
public static void runJustBeforeBeingDrawn(final View view, final Runnable runnable) {
final OnPreDrawListener preDrawListener = new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
runnable.run();
return true;
}
};
view.getViewTreeObserver().addOnPreDrawListener(preDrawListener);
}
Sample usage:
ViewUtil.runJustBeforeBeingDrawn(yourView, new Runnable() {
#Override
public void run() {
//Here you can safely get the view size (use "getWidth" and "getHeight"), and do whatever you wish with it
}
});
On some cases, it's enough to measure the size of the view manually:
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width=view.getMeasuredWidth();
int height=view.getMeasuredHeight();
If you know the size of the container:
val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxWidth, View.MeasureSpec.AT_MOST)
val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxHeight, View.MeasureSpec.AT_MOST)
view.measure(widthMeasureSpec, heightMeasureSpec)
val width=view.measuredWidth
val height=view.measuredHeight
if you have a custom view that you've extended, you can get its size on the "onMeasure" method, but I think it works well only on some cases :
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final int newHeight= MeasureSpec.getSize(heightMeasureSpec);
final int newWidth= MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
If you write in Kotlin, you can use the next function, which behind the scenes works exactly like runJustBeforeBeingDrawn that I've written:
view.doOnPreDraw { actionToBeTriggered() }
Note that you need to add this to gradle (found via here) :
android {
kotlinOptions {
jvmTarget = "1.8"
}
}
implementation 'androidx.core:core-ktx:#.#'
Are you calling getWidth() before the view is actually laid out on the screen?
A common mistake made by new Android developers is to use the width
and height of a view inside its constructor. When a view’s
constructor is called, Android doesn’t know yet how big the view will
be, so the sizes are set to zero. The real sizes are calculated during
the layout stage, which occurs after construction but before anything
is drawn. You can use the onSizeChanged() method to be notified of
the values when they are known, or you can use the getWidth() and
getHeight() methods later, such as in the onDraw() method.
Based on #mbaird's advice, I found a workable solution by subclassing the ImageView class and overriding onLayout(). I then created an observer interface which my activity implemented and passed a reference to itself to the class, which allowed it to tell the activity when it was actually finished sizing.
I'm not 100% convinced that this is the best solution (hence my not marking this answer as correct just yet), but it does work and according to the documentation is the first time when one can find the actual size of a view.
Here is the code for getting the layout via overriding a view if API < 11 (API 11 includes the View.OnLayoutChangedListener feature):
public class CustomListView extends ListView
{
private OnLayoutChangedListener layoutChangedListener;
public CustomListView(Context context)
{
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
if (layoutChangedListener != null)
{
layoutChangedListener.onLayout(changed, l, t, r, b);
}
super.onLayout(changed, l, t, r, b);
}
public void setLayoutChangedListener(
OnLayoutChangedListener layoutChangedListener)
{
this.layoutChangedListener = layoutChangedListener;
}
}
public interface OnLayoutChangedListener
{
void onLayout(boolean changed, int l, int t, int r, int b);
}
You can check this question. You can use the View's post() method.
Use below code, it is give the size of view.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.e("WIDTH",""+view.getWidth());
Log.e("HEIGHT",""+view.getHeight());
}
This works for me in my onClickListener:
yourView.postDelayed(new Runnable() {
#Override
public void run() {
yourView.invalidate();
System.out.println("Height yourView: " + yourView.getHeight());
System.out.println("Width yourView: " + yourView.getWidth());
}
}, 1);
I was also lost around getMeasuredWidth() and getMeasuredHeight() getHeight() and getWidth() for a long time.......... later i found that getting the view's width and height in onSizeChanged() is the best way to do this........ you can dynamically get your CURRENT width and CURRENT height of your view by overriding the onSizeChanged() method.
might wanna take a look at this which has an elaborate code snippet.
New Blog Post: how to get width and height dimensions of a customView (extends View) in Android http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html
In Kotlin file, change accordingly
Handler().postDelayed({
Your Code
}, 1)
You can get both Position and Dimension of the view on screen
val viewTreeObserver: ViewTreeObserver = videoView.viewTreeObserver;
if (viewTreeObserver.isAlive) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
//Remove Listener
videoView.viewTreeObserver.removeOnGlobalLayoutListener(this);
//View Dimentions
viewWidth = videoView.width;
viewHeight = videoView.height;
//View Location
val point = IntArray(2)
videoView.post {
videoView.getLocationOnScreen(point) // or getLocationInWindow(point)
viewPositionX = point[0]
viewPositionY = point[1]
}
}
});
}
If you need to know the dimensions of a View right after it is drawn you can simply call post() on that given View and send there a Runnable that executes whatever you need.
It is a better solution than ViewTreeObserver and globalLayout since it gets called repeatedly not just once.
This Runnsble will execute only once and you will know the views size.
works perfekt for me:
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
CTEditor ctEdit = Element as CTEditor;
if (ctEdit == null) return;
if (e.PropertyName == "Text")
{
double xHeight = Element.Height;
double aHaight = Control.Height;
double height;
Control.Measure(LayoutParams.MatchParent,LayoutParams.WrapContent);
height = Control.MeasuredHeight;
height = xHeight / aHaight * height;
if (Element.HeightRequest != height)
Element.HeightRequest = height;
}
}