- Android — get children inside a View?
- 8 Answers 8
- How to access parent Activity View in Fragment
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged android android-fragments fragment or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- IView Parent Interface
- Definition
- Remarks
- Properties
- Methods
- Extension Methods
- How can I get Fragment from View?
- 3 Answers 3
- Getting View’s coordinates relative to the root layout
- 10 Answers 10
Android — get children inside a View?
Given a View how can I get the child views inside it?
So I have a custom View and debugger shows that under mChildren there are 7 other views. I need a way to access these views but it doesn’t seem like there is a public API to do this.
EDIT:
My custom view inherits from AdapterView
8 Answers 8
If you not only want to get all direct children but all children’s children and so on, you have to do it recursively:
To use the result you could do something like this:
For example, within an activity / view:
or if you have a reference to a view:
I’m just going to provide this answer as an alternative @IHeartAndroid’s recursive algorithm for discovering all child View s in a view hierarchy. Note that at the time of this writing, the recursive solution is flawed in that it will contains duplicates in its result.
For those who have trouble wrapping their head around recursion, here’s a non-recursive alternative. You get bonus points for realizing this is also a breadth-first search alternative to the depth-first approach of the recursive solution.
A couple of quick tests (nothing formal) suggest this alternative is also faster, although that has most likely to do with the number of new ArrayList instances the other answer creates. Also, results may vary based on how vertical/horizontal the view hierarchy is.
Источник
How to access parent Activity View in Fragment
I have an ActionBarActivity and fragment. I am using FragmentPagerAdapter that provides fragment to my app. My question How can I access parent Activity View in Fragment ??
5 Answers 5
Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout
In Kotlin it is very easy to access parent Activity View in Fragment
At first, create a view like this:
Then convert it to any view that you need like this:
Note that if you are using findViewById<>() from activity, it wont work if you call it from fragment. You need to assign the view to variable. Here is my case
if you are using kotlin then you can use this syntax
Not the answer you’re looking for? Browse other questions tagged android android-fragments fragment or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
IView Parent Interface
Definition
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Defines the responsibilities for a class that will be a parent of a View.
Remarks
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Properties
Gets the JNI value of the underlying Android object.
(Inherited from IJavaObject)
Tells if this view parent layout direction is resolved.
Indicates whether layout was requested on this view parent.
Tells if this view parent text alignment is resolved.
Tells if this view parent text direction is resolved.
Return this view parent layout direction.
Returns the parent if it exists, or null.
Gets the parent of a given View for accessibility.
Return this view parent text alignment.
Return this view parent text direction.
Methods
Change the z order of the child so it’s on top of all other children.
Tells if this view parent can resolve the layout direction.
Tells if this view parent can resolve the text alignment.
Tells if this view parent can resolve the text direction.
This method is called on the parent when a child’s drawable state has changed.
Called when a child view now has or no longer is tracking transient state.
Called when a child of this parent is giving up focus
Have the parent populate the specified context menu if it has anything to add (and then recurse on its parent).
Tells the parent that a new focusable view has become available.
Find the nearest view in the specified direction that wants to take focus
Compute the visible part of a rectangular region defined in terms of a child view’s coordinates.
All or part of a child is dirty and needs to be redrawn.
All or part of a child is dirty and needs to be redrawn.
Find the nearest keyboard navigation cluster in the specified direction.
Notifies a view parent that the accessibility state of one of its descendants has changed and that the structure of the subtree is different.
The target View has been invalidated, or has had a drawing property changed that requires the hierarchy to re-render.
Request a fling from a nested scroll.
React to a nested fling before the target view consumes it.
React to an accessibility action delegated by a target descendant view before the target processes it.
React to a nested scroll in progress before the target view consumes a portion of the scroll.
React to a nested scroll in progress.
React to the successful claiming of a nested scroll operation.
React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.
React to a nested scroll operation ending.
Tell view hierarchy that the global view attributes need to be re-evaluated.
Called when a child of this parent wants focus
Called when a child of this group wants a particular rectangle to be positioned onto the screen.
Called when a child does not want this parent and its ancestors to intercept touch events with ViewGroup#onInterceptTouchEvent(MotionEvent) .
Ask that a new dispatch of View#fitSystemWindows(Rect) View.fitSystemWindows(Rect) be performed.
Called when something has changed which has invalidated the layout of a child of this view parent.
Called by a child to request from its parent to send an AccessibilityEvent .
Called when a child wants the view hierarchy to gather and report transparent regions to the window compositor.
Shows the context menu for the specified view or its ancestors.
Shows the context menu for the specified view or its ancestors.
Start an action mode for the specified view with the default type ActionMode#TYPE_PRIMARY .
Start an action mode for the specified view with the default type ActionMode#TYPE_PRIMARY .
Extension Methods
Performs an Android runtime-checked type conversion.
Источник
How can I get Fragment from View?
I added some Fragment into a TableLayout and I want to manage them from my container Activity , so I used this:
but getChildAt(int) returns a View and a View could NOT cast to Fragment
3 Answers 3
I don’t understand why people are down-voting your question. Fragments can be very confusing at times, especially for beginners. To understand your problem, you must learn what is a Fragment and how they are used.
To start with, a View is something that has an existence on the screen. Examples include: TextView , EditText , Button , etc. They are placed inside «layouts» written in Xml or Java/Kotlin. These layouts are shown using an Activity .
Now, a Fragment is not a View . It does not have any existence on the screen at all. Instead, it’s a class that simply manages a «layout» — kinda similar to an Activity . If you need the View returned by your Fragment’s onCreateView() , you can directly use findViewById() within your Activity .
If you need a reference to your Fragment, there are two possible ways of doing this:
1) If you added the Fragment programmatically like this
2) If you added the Fragment inside an XML layout like this:
You can use this:
Basically, each Activity has a FragmentManager class that maintains all the active Fragments , and there are two ways of finding them: Using a unique TAG that you pass while showing a fragment, or passing the container view-ID where the fragment was added.
Источник
Getting View’s coordinates relative to the root layout
Can I get a View’s x and y position relative to the root layout of my Activity in Android?
10 Answers 10
The Android API already provides a method to achieve that. Try this:
This is one solution, though since APIs change over time and there may be other ways of doing it, make sure to check the other answers. One claims to be faster, and another claims to be easier.
Let me know if that works.
It should recursively just add the top and left positions from each parent container. You could also implement it with a Point if you wanted.
Please use view.getLocationOnScreen(int[] location); (see Javadocs). The answer is in the integer array (x = location[0] and y = location[1] ).
First I get the root layout then calculate the coordinates difference with the view.
You can also use the getLocationOnScreen() instead of getLocationInWindow() .
No need to calculate it manually.
Also note that for the centre coordinates, rather than something like:
You can just do:
;` to get location of your view correctly.
But there is a catch if you use it before layout has been inflated you will get wrong position.
Solution to this problem is adding ViewTreeObserver like this :-
Declare globally the array to store x y position of your view
and then add ViewTreeObserver on your parent layout to get callback for layout inflation and only then fetch position of view otherwise you will get wrong x y coordinates
and then use it like this
I wrote myself two utility methods that seem to work in most conditions, handling scroll, translation and scaling, but not rotation. I did this after trying to use offsetDescendantRectToMyCoords() in the framework, which had inconsistent accuracy. It worked in some cases but gave wrong results in others.
«point» is a float array with two elements (the x & y coordinates), «ancestor» is a viewgroup somewhere above the «descendant» in the tree hierarchy.
First a method that goes from descendant coordinates to ancestor:
Next the inverse, from ancestor to descendant:
Incase someone is still trying to figure this out. This is how you get the center X and Y of the view .
I just found the answer here
It says: It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.
Источник