is there a listener or some way to determine when contentView is created? I have used other type listeners and they work for other child views inside of content view. however content view is different. I have not yet found anything that works.
Have you tried onContentChanged() yet?
The documentation says
This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView).
Related
We know ViewTreeObserver is used to register listeners that can be notified of global changes in the view tree. There are two method defined in this class are
addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) and addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener listener). But the the purpose of these methods are not clearly understandable to me which described in the developer site. I tried to understand it and also searched in google but was unable to find clear picture about this topic. Any help is greatly handful for me.
OnPreDrawListener
Gets called just before onDraw() method gets invoked. At this point, all views in the tree have been measured and given a frame. Therefore you can properly manipulate view in this callback
OnGlobalLayoutListener
This listener gets called:
- when visibility state changes. In example when view has been drawn it becomes visible and this gets called.
- when you addView state of view tree changes
I have a background view not showed into main activity and I need to get the "invalidate" event for know when something into the view is changed.
The problem is the event "invalidate" is "emitted" by the view only if this view has been added to the main activity thorugh the "addView" call. I don't want to add this view to main UI since is managed in a separate thread but I have this problem of invalidate event not emitted and I don't know how to find a workaround. It seem the "addview" function unlock something that allow to generate invalidate call but still not found the reasons.
Someone know how to force a view to generate invalidate event also if not added to main activity?
I think you can't do this, because invalidate method is called by View framework, so this method would't be called if the view is not add to the window. But I think there's a trick: adding this view to the window and hide it below other view, so this view is invisible and the invalidate method will be called by system.
Why would you want to invalidate a view that is not there? Instead of finding a workaround I suggest to improve how your app works. Invalidate means that you want to redraw something that is on screen. I don't know your exact use case is but my guess is you want to do something that is not UI related in invalidate. You should not do that.
The problem:
I have a class, inherited from LinearLayout.
This class creates some buttons in the constructor and puts them into itself using addChild().
I've overloaded method onSizeCHanged and I want to add some childs in this method.
But changes have no effect until one of the other buttons clicked.
So, I need to press some of existing buttons and after that views which I've added appear.
(by the way, only buttons with ontouchlistener do it. buttons without listener cannot make new views appeared).
How to add views in onSizeChange method to do them appeared immediately?
ADDITION:
Methods forceLayout() and requestLayout() cannot work.
I have not extended LinearLayout myself but I read up on the class a bit and I think you need to override onLayout() and add your child views in it. Check the docs and see if it helps
http://developer.android.com/reference/android/widget/LinearLayout.html#onLayout(boolean,%20int,%20int,%20int,%20int)
The solution was: making a simple boolean flag, setting it "true" im onSizeChanged and calling "invalidate".
In method "onDraw" checking if flag is true and if it is so, calling requestLayout. Then setting flag to false.
What is the difference between onCreateView and onBindView methods in Preference?
In documentation it says that onBindView:
Binds the created View to the data for this Preference. This is a
good place to grab references to custom Views in the layout and set
properties on them.
Why is it such a good place to set properties on Views in my layout? Currently I am setting properties in onCreateView method and everything seems to work fine.
From my experience it looks like both methods are always called together. Maybe there are some situations when only onBindView is called?
onCreateView() is for creating the View hierarchy that will eventually contain the Preference UI. onBindView() is for binding actual data to that View hierarchy created in onCreateView().
The pattern separates the creation of the View hierarchy - which is cached - from the binding of data to that View hierarchy. In the case of Preference, onCreateView() is only called once, but onBindView() is called every time the UI needs to load the Preference View.
I am guessing that your current setup works because you never change the properties that you set on the Preference. It would be better to setup the properties of the View hierarchy in onBindView(), in case it ever needs to be dynamic.
(As an aside, this View creation vs. binding design pattern is also seen in CursorAdapters, where it only creates enough Views to display on screen, but is constantly binding these Views to new data.)
In my custom view I need to use the getParent method and set the visibility on some of it's child views depending on my custom view's state. The problem is that I want to instantiate the child views just once. Where is the best place to do this?
I'm not exactly sure what you want to do. But if all of this is occurring in the same Activity screen why don't you just assign ids (e.g. android:id="#+id/someId" to your elements in the layout.xml file.
This way you can reference any element programatically in your code by calling:
View someView = findViewById(R.id.someId);
I am unclear why you would need to call getParent. If you are trying to manipulate views in a different activity then I think you will need to use a Handler.