View created from Java code is wrapped in another view. Why? - java

1) Why, when I create my custom compound view from Java code it is wrapped in another view which creates another level of view hierarchy? If I use the <include> tag in the XML to create the view, this does not happened. (see the attached image from hierarchy manager). Any reason for that?
2) The image shows (the part where the view is created by <include> tag) another interesting behavior – that it is easily possible to have the views with the same android:id on the same hierarchy level. When the findByViewId() method is used then, the first view reference is returned.

Any reason for that?
Because that's the way you wrote the code If you do not want that behavior, fix your code.
that it is easily possible to have the views with the same android:id on the same hierarchy level
Of course. You see this all the time with subclasses of AdapterView, such as ListView.

Related

What does findViewById(android.R.id.content) do in Snackbar?

I have seen in Snackbars being passed this findViewById(android.R.id.content) argument. We can't pass getContext() method as it demands a View parameter. I have seen on internet that programmers pass this argument inside Snackbar, what does it really mean?
Also, since it asks a View argument can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work? If yes, isn't it a little unexplanatory in code about what's really going on?
what does it really mean?
It asks the hosting activity to find a widget whose ID is android.R.id.content. All activities have one of these, set up by the framework Activity implementation and its associated Window. It represents the main content area of the activity.
isn't it a little unexplanatory in code about what's really going on?
You are certainly welcome to add comments to your code to explain your choice.
The Snackbar documentation explains the role of the View:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.
With that in mind...
can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work?
Perhaps. It depends a bit on the UI of your app. If there is a particular CoordinatorLayout that you want to use with the Snackbar, pass it (or a child) to make(). Otherwise, any widget should work.

Insert Android view at the top of the view hierarchy

I would like to know if it is possible to add a view to the top of the view hierarchy for my Android App. Currently, I have a class that receives information from an external service. I would like to display information received as a custom alert to the user. Is it possible to insert this custom view at the top of the global view hierarchy, so that it shows above all other views. I would prefer not to start a new activity, just add the view on top of what is currently being shown.
Yes. Accessing the root View is as easy as finding it.
In the Activity:
FrameLayout root = (FrameLayout) findViewById(android.R.id.content);
Since the root is a FrameLayout, you just have to add a View to it in order for it to be on top. However, note that the Z-order will change if you add other Views after this one is added. In which case, you'll have to use ViewGroup#bringChildToFront(View view) to bring it back to the top.
EDIT:
It is also possible to get the root view from another View by using View#getRootView(). This is not always reliable as the View may not be attached to the hierarchy (in which case the View itself is the root). Or it's parent may not be in the hierarchy (in which case the Parent itself will be the root).

How to create this kind of view in Android?

I want to use this kind of UI in my Contact Android App. The picture shown resizes itself when we scroll down much and the Name 'XYZ' finally become the title of the action bar. Also how can I put textview over imageview?
What you're looking for is CollapsingToolbarLayout.
The general approach to this kind of animations is to use top level CoordinatorLayout which contain two children: the first child is the "collapsible" content, and the second is "body". The general hierarchy looks like this:
CoordinatorLayout
|-AppBarLayout
|-CollapsingToolbarLayout
|-Toolbar
|-ScrollView
The second child must not be ScrollView - use any ViewGroup that suits your needs, and just add the following attribute to its description in xml:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
the above string resource should map to AppBarLayout.ScrollingViewBehavior (just use android.support.design.widget.AppBarLayout$ScrollingViewBehavior as this resource's value).
There is very good tutorial on this kind of animations on Codepath.

execute class from included layout

Within my layout I have set a Include Other Layout. this layout schould in his turn show a listview with items loaded from the web (my webserver)
the only problem is: the items don't load in because the class that holds the code to load the items isnt called on because im using a Include Other Layout meaning only the layout is called and not the functional code from any .java files (classes)
leaving me with a blank page...
is there a way to make a call upon the class containing the code for the layout i have included?
<include/> tag is only for including only the view into another layout. It can be very useful if you use a common view everywhere. A ProgressBar can be an example. You can include ProgressBar everywhere you want. But it is just a UI.
If you also want the functionality you should use Fragments. Actually Fragments are exactly for what you describe.
Here is the tutorial from Android Developers official website
http://developer.android.com/training/basics/fragments/index.html

Android - findViewById() and dynamically setting ID's

I have a poll of ID's (ids.xml), and I assign id's for views I create dynamically. Now my question is pretty simple - assume I create a new view and assign it an id with setId() in conjuction with R.id.uniqueId. Later on, can I access the view with findViewById(R.id.uniqueId)?
If so, what could be the reason it returns null?
Here is a toy example: UPDATED
LinearLayout l = new LinearLayout(this);
l.setId(R.id.mId);
setContentView(l); //i see on screen the views added to 'l'
l = (LinearLayout) findViewById(R.id.mId); //it returns null :(
How come it does not register\map the assgined ID to the view it was assigned?
Does your desired view have a parent view that you could use to call parent.findViewById on? That may help narrow your problem down.
One thing I noticed that's missing from your brief example: you need to make sure you're adding the new LinearLayout to the view hierarchy before you will be able to find it with findViewById:
findViewById(R.id.parent).addView(l)
You can also use the hierarchy viewer to take a look and see if everything's being set up properly.
encountered this many times, try cleaning your project by making a clean, Also check findViewById() returns null for custom component in layout XML, not for other components
From what you have given us it appears that the view is never added to the Activity's view. This is necessary as findViewById uses the activity's view as a parent to find a child view with that Id.
If you are creating the view you can always create it as a member variable and refer to it that way if you are going to be adding it to a view at a later time. Please note that the view must be added after the id has been set.

Categories

Resources