How to test if a view is in the top layer? - android

I have a View, I want to know if there is a way to test if the view is in the top layer or it has the biggest z-value.

There's no such property/value like z-index in android except the order in which you add your views in a FrameLayout. However, you may do the following to verify if the view itself is the parent or not:
if(v.getParent() == null){
//view is parent
}
else{
//view is not parent
}

Related

Android - Move a single instance of a View between Parent Views

For an important reason, i'm required to keep only a single instance of a View in the entire application.
Within the application there will be multiple parent views, (each displayed only once at at time). And I need to move the child view around - to the currently active parent.
I tried doing the following:
if (view_to_move_around != null) {
ViewGroup oldParent = (ViewGroup) view_to_move_around.getParent();
if (oldParent != null) {
oldParent.removeView(view_to_move_around);
}
} else {
// Initialise the View
}
newParent.addView(view_to_move_around)
However, that method didn't seem to work? Completely stuck at this point.
I guess my question will be "What do you mean it's not working?" Are you getting an exception? It is not being displayed properly? Does it execute without issue, but is not displaying? Does it display, but it's always initializing?
Are you manipulating the child view within each parent view, before you pass it on to the next view?
Few things to make sure right off the bat:
Layout Params. Are they all set correctly? For both the parent and the child?
Parent View. The code doesn't appear to be faulty from what I can see. So is the parent view being displayed correctly?
Visibility. Are both the parent and child View.VISIBLE?
EDIT
Sweet. Ok, when I'm debugging these things, I like to keep it simple at first. I would take the child view, set it's background color to purple (or a contrasting color from the parent). Then, for simplicity's sake, set it's layout params to match parent. Assuming the ParentView is a FrameLayout:
mChildView.setBackgroundColor(Color.CYAN);
mNewParentView.addView(mChildView,
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));
Does it fill up and take over the parent?
In the intermediate steps, does it no longer have a parent when it's been removed?
Log.d("LOGTAG", mChildView.getParent().toString());
Does the new parent show that the child has been added?
Log.d("LOGTAG", mNewParentView.getChildCount()): // before and after
Is it being shown 'behind' the other views within the Parent?
mParentView.bringChildToFront(mChildView);
What may be the problem is that you are using getParent() to store oldParent. Compare for example:
if (view_to_move_around != null) {
ViewGroup oldParent = (ViewGroup) findViewById(R.id.old_parent);
if (view_to_move_around.getParent() == oldParent) {
oldParent.removeView(view_to_move_around);
}
} else {
// Initialise the View
}
newParent.addView(view_to_move_around)
Does this sound like what you're looking for? Let me know if this works.

How to recursively iterate through ViewParent tree?

I have tree of views, looking something like this.
-- parent view top
-- parent view mid
-- parent view mid
-- parent view low
-- parent view low
-- child 1
-- child 2
-- child 3
I want to check from the child level the id of the top parent view to apply some logic there, based on some TouchEvents in child.
Do you know how can I design the recursive function which will allow me to iterate to the top?
I was able with the usage of getParent() method to get to desired Parent, but this solution is not flexible enough for me at this moment.
A recursive function from child to parent can be done in this way.
public void doSomething(View view) {
//
// do something here
//
if(view.getParent() != null)
{
doSomething(view.getParent());
}
}

Custom view loses background resource when not in view

So, I'm adding an ad from either AdMob or MMAds, OR I have the MMAds Interstitial ad come up, and on exit, OR I touch one of the list items, starting a new activity, then at the end of the activity, this happens:
http://i.imgur.com/Zq7vvkQ.png
The top item doesn't have a background resource being drawn, the bottom is similar to this. This happens sometimes to the ones in the middle (With the ads, they ALL go away).
I was wondering why this happens and how to prevent it... The most I could find on other questions is one on using the following code on the ListView:
android:cacheColorHint="#00000000"
Or
android:scrollingCache="false"
The background resource is being drawin in the custom ListAdapter's getView(int, View, ViewGroup) method.
Any help is appreciated,
Justin W.
To verify your problem try to run hierarchyviewer, analize your views tree and try to find potential solution. Have you used Hierarchy Viewer?
So, I had a bit of code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
item = inflater.inflate(R.layout.griddler_menu_choice_item, parent, false);
} else {
item = convertView;
}
return item;
}
Basically this was to keep it more efficient so it didn't have to keep creating Views as it's costly, but this somehow effected the end result... So now it works =).
Forgot to add in how to fix this... remove the else part and the if in general, just leave the line of code:
item = inflater.inflate(R.layout.griddler_menu_choice_item, parent, false);
That fixed it for me.

RelativeLayout check runtime if is removed or not

In an activity I am changing one of the ViewGroup content runtime: buttons action, other events.
At a specific case I need to check if a child is in this layout or not ( in this case the child is another RelativeLayout, which hold other views)
How can I check runtime, programmatically if child_1_RelativeLayout is there or is already removed from view tree, his parent is parentRelativeLayout
The getParent() is usefully? - not much explanation how to use it, thanks.
In case you have stored your views you can use getParent() to check if view is a direct child for other view. After removing view its parent field will be cleared. For example:
ViewGroup parent = ...;
View child = ...;
assert(child.getParent() == parent); // <-- true
parent.removeView(child);
assert(child.getParent() == parent); // <-- false
Not really sure if I understand your question correctly, but:
when you add the relative layout, be sure to first give it an id (either in your layout.xml file, or from code)
to check existence of the relative layout within the ViewGroup, use ViewGroup's findViewById() method (inherited from View) and pass it the id you've given to the relative layout
if it returns null, the relative layout is not there, otherwise findViewById() will find it
So in short, findViewById() is not only defined for an Activity, but you can call this on any view you would like to use as a starting point for your search.

Android HorizontalScrolling laggy

I have a HorzontalScrollView with a LinearLayout inside. During Runtime I can add more LinearLayouts to the LinearLayout.
Now I have the problem that the Scrollview only scrolls a little bit and not smooth with one finger slide!
Does anyone have a solution for this problem?
HorizontalScrollView doesn't use an adapter that manages the list's memory, therefore it can't handle heavy (images, custom views, etc) lists.
You can use this Horizontal ListView http://www.dev-smart.com/archives/34 but make sure you don't write the on list item click method inside the getView, it will make the list scroll slow. Other than that, that's a great resource for a smooth horizontal list view.
You can also explore the Android view pager, which is also supported on lower Android versions using the compatibility pack: http://developer.android.com/sdk/compatibility-library.html
Edit - something like that in the adapter that inflates the XML you want (the linearLayout) and then populates every view with the relevant data.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_friends_list_item, null);
}
ImageView status = (ImageView)convertView.findViewById(R.id.status);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
ImageView imageBorder = (ImageView)convertView.findViewById(R.id.image_border);
TextView title = (TextView)convertView.findViewById(R.id.title);
}
The problem was my parent Viewflow because it has stolen the swipe event! The HorizontalListView is too buggy for me! (Problems with size attributes)
However,thank you for your answer! ;)

Categories

Resources