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

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.

Related

view and viewgroup android

I know java basics and I am learning Android development now. I have learnt that there are two central android classes for the ui, View and ViewGroup. I want you to correct me wherever I am wrong.
ViewGroup is a subclass of View and holds View objects together. TextView, EditText, etc are subclasses of View. LinearLayout, Gridview, etc are subclasses of ViewGroup.
I want to relate this to the OOP concept of java but I don't seem to get all this. Where are the objects? How come a subclass is a container?
I'll give you a basic overview here. I'm also pretty new to Android, so the correct-me-if-I'm-wrong thing goes for me too. :)
A View is basically a unit of UI; like, say, a box of 24x24 pixels (yes, THAT basic). Now this box can be used for anything, because it is the top-most entity in the hierarchy. We can define it more precisely by specifying what we want it to hold, this is where TextView, ImageView, WebView, etc. come in. So this box 24x24 may hold text, an image or a web-page content, respectively.
A ViewGroup can belong in the 'etc.' above. Just as for showing text, the 24x24 box can be used to display 4 boxes 12x12. If so, the View can be classified as a ViewGroup.
Further, when we know what type of arrangement we require the box to hold, in this case, we can further classify it as any of LinearLayout, RelativeLayout, GridLayout, FrameLayout, etc.
In such a hierarchy, an entity can have a child object of any kind, even an instance of its own class.
You may even say that View is the 'Object' of UI.
I hope I have helped you.
Comments/edits welcome. :)

How does LayoutInflater work?

I am new to Android development, and I am curious about the phrase "Inflate a View", which is being thrown around in the Android development community.
I know it has to do with calling the Inflate method of the LayoutInflater class but I'm still yet to grasp the concept behind it. Does this basically mean returning a view by passing its ID just like JavaScript's document.getElementById() sort of thing or is there more to it? Any form of enlightenment will be appreciated.
It basically is the term used for instantiating an xml Layout file into an object to be used in the java code. You can do this by calling setContentView(R.layout.file name minus xml extension) or by using LayoutInflater. Only after inflating can you call its child Views. This is important to know because I have seen many questions on SO in which people have an app that crashes and it turns out that they are trying to reference a view like
EditText et = (EditText) findViewById(R.id.someID);
and they haven't called setContentView() to obtain the Layout which holds that EditText which results in a NPE since the EditText doesn't actually exist until the Layout is inflated.
I'm sure you have seen the docs on it but Here they are anyway
While the specifics are surely more complicated than what I'm going to say, I think the easiest way to think of inflating views is as the process of going from the xml layout file to the actual View objects. The inflation methods, which are peppered around a number of different classes, typically all take an id argument which you would set as R.layout.xxx.

Why do i have to cast a Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly

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.

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

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.

Categories

Resources