When To Get a Custom View's Width and Height Properties - android

I have a Class called LineGraphView which is a subclass of View. I want to be able to pass some data to this view so that it can then draw accordingly in its onDraw() method.
Analysis Fragment (Where the view is referenced and method is called from)
LineGraphView altitudeGraph;
LineGraphView speedGraph;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
super.onCreateView(inflater, group, saved);
View view = inflater.inflate(R.layout.analysisfrag, group, false);
this.altitudeGraph = (LineGraphView) view.findViewById(R.id.linegraph_Altitude);
this.speedGraph = (LineGraphView) view.findViewById(R.id.linegraph_Speed);
return view;
}
My question is, if I then want to be able to access that views width and height attributes using getWidth() and getHeight(), when am I able to do so because so far all thats being returned is 0.
Do I need to create a listener which lets the LineGraphView's parent know its ready to be accessed?
Thanks in advance.

If you're already creating your own custom view, I would just add some private variables with getters/setters for the information you need to pass. From there, just handle the drawing in your onDraw() method -- from there, you can get your View's width and height (or just the Canvas width and height, which might be the same values).

The answer is here: https://stackoverflow.com/a/3594216/969325
Basically you are calling getWidth/height too early (during onCreate). I believe you could use a handler to get it after onCreate. You could also try this: https://stackoverflow.com/a/10118459/969325 (using onWindowFocusChanged).

Related

How onCreateView works

I started a TabLayout Activity, which includes the following code to create the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
I've read the official documentation and still unsure how it works. If somebody could explain in detail how each part here is working that would be great.
Edit: Mainly referring to View rootView = inflater.inflate(R.layout.fragment_find, container, false); what each of these 3 parameters are doing and how inflater.inflate() is working here.
OK, here we go.
The process of inflating is simply creating your view explicitly instead of doing this implicitly, this is by using this:
public void onCreate(){
setContentView(R.layout.your_layout);
}
Compare with this question.
Now with the arguments. Compare this with this section.
R.layout.fragment_find returns ID of a fragment used somewhere in the code. R is a dynamic android class used for manipulating some of your app's resources such as views, strings etc. Compare.
container is a root from some ViewGroup. So you hgave a group of, say, buttons doing common things (for example choosing some colour in your application), and they all have same parent, in your case called a container.
attachToRoot is the last argument. According to docs:
If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
So it is not attached to the parent we talked about in last point. Compare here.
I hope this helped.
onCreateView():
After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).
Here, it's a method of the lifecycle for Fragment.

Android - onGlobalLayout is not called by ViewTreeObserver

I have a headache in current Android project. I want to detect the changing of the current page. For example, there is a TextView to display device time, which is updated per second. How to detect this change? I searched a lot on SO (thanks SO), but none works for me.
More information: I don't use standard Activity to create page. My way is:
All widgets are created into a View object which is then used to create a container object. After that, I just handle this container to draw on a canvas with a VSYNC callback Choreographer.FrameCallback periodically.
Indeed, it works to draw. All are ok. Except: I want to draw canvas only when the page's content changed. So back to my beginning question, how to detect this "changing" event? I am sure there is some kind of callback to handle this problem. I used the following solution, but onGlobalLayout is not called when textview's text changed.
CanvasAppViewContainer container;//CanvasAppViewContainer extends AbsoluteLayout
LayoutInflater li =(LayoutInflater)getService().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(resourceId, null);//passed a correct the layout id
container = new CanvasAppViewContainer(getService(), view, getWidth(), getHeight(), getSurface());
rootView = (LinearLayout) findViewById(R.id.rootView); //root element of layout
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressLint("NewApi")
#Override
public void onGlobalLayout() {
Log.d("test", "onGlobalLayout");
}
});
BTW: Even if I register the view tree observer for the textview, onGlobalLayout is still not called.
Thanks
onGlobalLayout() is only called when the layout changes. When you want to detect a change in the text use TextView.addTextChangedListener()

View added in ViewGroup is not displayed

I'm trying to add a view in a viewGroup (without xml) but can't make the view appear. I can't figure out what I'm missing...please help, I've been looking all the web for hours now.
Here is my code (the Background class extends ViewGroup) :
public void setupBackground()
{
this.backgroundView = new Background(activity);
View bgGround = new View(activity);
bgGround.setX(100);
bgGround.setY(200);
bgGround.setBackgroundColor(activity.getResources().getColor(R.color.black));
bgGround.setBackgroundResource(R.drawable.mario_ground);
this.backgroundView.addView(bgGround, 100,100);
this.activity.addContentView(backgroundView, new LayoutParams(bgWidth,bgHeight));
}
Ok, I found it. ViewGroup is not enough for drawing content, I had to extend RelativeLayout (or another kind of layout) instead.
What made me loose lot of time is that I had overrid onLayout method while still in ViewGroup and then changed to RelativeLayout, so the onLayout for RelativeLayout wasn't calling super.onLayout.
Thanks anyway, and sorry for my poor english :)

Fragments : what is the best place to measure Views?

I have a fragment and I need to measure location/width/height of its views on screen and pass to some other class.
So what I have is a function which does it, something like this :
private void measureTest(){
v = ourView.findViewById(R.id.someTextField);
v.getLocationOnScreen(loc);
int w = v.getWidth();
...
SomeClass.passLocation(loc,w);
...
The problem is that the location/width/height of views is not ready within fragment lifecycle.
So if I run that function within these lifecycle methods :
onCreateView
onViewCreated
onStart
onResume
I either get wrong location and width/height measurments or 0 values.
The only solution I found is to add a GlobalLayoutListener like this to mainView
mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
if(alreadyMeasured)
mainView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
else
measureTest();
}
});
this gets the job done.. but its just Yack! IMO.
Is there a better way of doing this? seems like such a basic thing to do
inside onActivityCreated of your fragment retrieve the currentView (with getView()) and post a runnable to its queue. Inside the runnable invoke measureTest()
There is no better way. That code isn't that bad! It's fired as soon as the view is layed out (my terminology might be a bit weird there) which happens right after measuring. That is how it is done in the BitmapFun sample (see ImageGridFragment, line 120) in Google's Android docs. There is a comment on that particular piece of code stating:
// This listener is used to get the final width of the GridView and then calculate the
// number of columns and the width of each column. The width of each column is variable
// as the GridView has stretchMode=columnWidth. The column width is used to set the height
// of each view so we get nice square thumbnails.

Trying to understand view hierarchy

I'm writing an app with the sole purpose of trying to understand how the view hierarchy in Android works. I am having some really harsh problems in it right now. I'll try to be concise in my explanation here.
Setup:
Currently I have three Views. 2 are ViewGroups and 1 is just a View. Let's say they're in this order:
TestA extends ViewGroup
TestB extends ViewGroup
TestC extends View
TestA->TestB->TestC
Where TestC is in TestB and TestB is in TestA.
In my Activity I simply display the views like so:
TestA myView = new TestA(context);
myView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(myView);
Problems:
The onDraw(Canvas canvas) method of TestA is never called. I've seen a couple solutions to this saying that my view doesn't have any dimensions (height/width = 0), however, this is not the case. When I override onLayout(), I get the dimensions of my layout and they are correct. Also, getHeight()/Width() are exactly as they should be. I can also override dispatchDraw() and get my base views to draw themselves.
I want to animate an object in TestB. Traditionally, I would override the onDraw() method on call invalidate() on itself until the object finish the animation it was supposed to do. However, in TestB, when I call invalidate() the view never gets redrawn. I'm under the impression that it's the job of my parent view to call the onDraw() method again, but my parent view does not call the dispatchDraw() again.
I guess my questions are, why would my onDraw() method of my parent view never get called to begin with? What methods in my parent view are supposed to be called when one of it's children invalidate itself? Is the parent the one responsible for ensure it's children get drawn or does Android take care of that? If Android responds to invalidate(), why does my TestB never get drawn again?
Ok, after some research and a lot of trying, I've found out I was doing three things wrong in regards to problem #2. A lot of it was simple answers but not very obvious.
You need to override onMeasure() for every view. Within onMeasure(), you need to call measure() for every child contained in the ViewGroup passing in the MeasureSpec that the child needs.
You need to call addView() for every child you want to include. Originally, I was simply created a view object and using it directly. This allowed me to draw it once, but the view was not include in the view tree, thus it when I called invalidate() it wasn't invalidating the view tree and not redrawing. For example:
In testA:
TestB childView;
TestA(Context context){
****** setup code *******
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childView = new TestB(context);
childView.setLayoutParams(params);
}
#Override
protected void dispatchDraw(Canvas canvas){
childView.draw(canvas);
}
This will draw the child view once. However, if that view needs updating for animations or whatever, that was it. I put addView(childView) in the constructor of TestA to add it to the view tree. Final code is as such:
TestB childView;
TestA(Context context){
****** setup code *******
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childView = new TestB(context);
childView.setLayoutParams(params);
addView(childView);
}
#Override
protected void dispatchDraw(Canvas canvas){
childView.draw(canvas);
}
Alternatively, I could override dispatchDraw(Canvas canvas) like so if I had many more children to draw, but I need some custom element between each drawing like grid lines or something.
#Override
protectd void dispatchDraw(Canvas canvas){
int childCount = getChildCount();
for(int i = 0; i < size; i++)
{
drawCustomElement();
getChildAt(i).draw(canvas);
}
}
You must override onLayout() (it's abstract in ViewGroup anyway, so it's required anyway). Within the this method, you must call layout for every child. Even after doing the first two things, my views wouldn't invalidate. As soon as I did this, everything worked just perfectly.
UPDATE:
Problem #1 has been solved. Another extremely simply but not-so-obvious solution.
When I create an instance of TestA, I have to call setWillNotDraw(false) or else Android will not draw it for optimization reasons. So the full setup is:
TestA myView = new TestA(context);
myView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
myView.setWillNotDraw(false);
setContentView(myView);
This isn't a direct answer, but here is a fantastic tutorial on how to draw custom views and gives a great crash cours in how to animate a view. The code is very simple and clean too.
Hope this helps!

Categories

Resources