Confusion regarding android app development - android

I was following an android development course and the instructor created an layout file named activity_youtube.xml and also gave the id to the layout as activity_youtube, later in the .java file the code he wrote was
setContentView(R.layout.activity_youtube)
ConstraintLayout constraintLayout = findViewById(R.id.activity_youtube)
I am really confused between this naming convention.
What are we refering to when we call R.layout.activity_youtube and what are we refering to when we call R.id.activity_youtube

When you are calling R.layout.activity_youtube you are referring to the layout xml file, so to everything that's inside.
While when calling R.id.activity_youtube you are referring to a specific component inside that file (a layout, a Button, a textView... ) that has the id property assigned to that name.
I will admit that it might be a little misleading calling the layout. xml file and its layout component the same.

R is a public final class in Android which extends Object class. It is purely there as a mechanism to allow you to easily reference the contents of your res hierarchy from your code. As such, you cannot create sub-hierarchies.
R.id is a nested class file created in android programming its also auto-generated file. It is used to refer to a UI component. Let us suppose you have two buttons in your UI(R.laoyout.your_layout_name) you can distinguish them and decide which action to be performed on the click of each button by using R.id.button1 and R.id.button2. These buttons lie in the layout file which you have used in the activity using R.layout.
setContentView() sets the View to be displayed as the main content view. The method is overloaded, so you can either pass an object of View class (or it's a subclass, such as LinearLayout, TextView, etc.), or you can provide the XML layout resource by using R.layout.your_layoutname.xml.
Thus, R.layout. references any layout resource you have created, usually in /res/layout. So if you created an activity layout called activity_main.xml, you can then use the reference in R.layout.activity_main to access it.

Related

OnClickListener for MaterialCardView inside Compound View in Android - Kotlin

I created a compound view that consists of two MaterialCardViews. I was curious if there is any way I can access the onClickListener on each of them separately from another file (for example the main activity). This is because I would like to trigger different events for each of them.
As of now, I have created the compound layout within a <merge> tag in an XML file and created a CompoundViewName class file in Kotlin.
I'm able to access onClickListener for each MaterialCardView (using findViewById) within the class file, but I would like to access them from another file, for instance, the MainActivity.kt file where the CompoundView is being rendered.
It would be great if you could guide me in the right direction. Kindly, let me know if my question is not clear.

accessing layout.xml through java and assigning values to views in android

I am new to android programming am trying to learn it.
So basically there are two ways of creating whatever is going to be visible on the screen :-
We create views and view group objects inside layout.xml files. And then as the need be we access those views that are already existing
in the layout through our java programs by accessing their ids ( as
r.java….). So basically when we start a particular activity, we
set the content to be displayed corresponding to that activity using
setContentView method, to which we pass the layout.xml file, inside
which we have defined the different views and view groups to be
displayed on the screen.
The second way is we create these views dynamically though our java programs and then set them as the content on the screen using
setContentView again.
Now the above is my basic understanding. Please let me know if the above needs correction.
Now what I want to understand here is :-
Is there a way that using the first method itself, we can do the
vice-versa, as in say instead of fetching the the views through their
ids from the layout.xml files, can we already have a predefined
layout.xml file with different views having ids, and now through our
java programs can we just access those views though their ids and set
their values, something like say (in javascript) :-
document.getElementById(“someTextBoxId”).value= “some calculated
value from java code here”
Thanks.

listview reusable component - architecture - android

i have a complex view which contains 4 list views arranged as per the requirement. i have been been able to implement and get it working. but this is a sole activity and data needs to supplied internally (within the activity).
i want to define a way where in, this complex view is like a reusable component which is called by other activities that provide data for all 4 list views and then the view shows up in the screen.
could somebody please guide me as to how do i go about achieving this functionality.
You should define your listview structure in a layout file of its own. Then you can use whats called inflation, which lets you "inject" seperate layout files into your main layout in run-time. Take a look at:
http://developer.android.com/reference/android/view/LayoutInflater.html
Take note at the introductory notes. Android is already inflating an XML resource, namely the layout file you´ve defined in setContentView(), you can grab the current instance of the inflater and use it as you see fit, saving greatly on memory as opposed to instantiating it yourself.

How can I allow multiple activities to share the same inflated "included" view?

I see this question here, and it makes me wonder if what I'm asking isn't really possible:
How to share a view across various activities
Basically, I have a common footer view that I'm inflating (including) in all of my views. However, it uses the same repetitive code to do that. My thought was to create a parent activity class to do this, but it doesn't seem correct to have one activity render the view of another. So should I just create a utility class of some sort, or is there a better way?
You can include other layout XML files directly in another layout file. So whenever you set content to a layout file, along comes your footer for the ride.
If your footer needs code to drive it, just create a custom class for it along with the layout file. Then perhaps during instantiation you can drive the code that needs to execute.
This is a blog of how to do it.
include is very useful while reusing View components.
But remember, if any problem occurs while using include tag, wrap the included view by an arbitrary layout.
If I understand your question, another way of having multiple Activities use the same View instance is by doing something like creating your own Application class (it's seriously easy).
MyApplication extends Application ...
#Override
public void onCreate(), onConfigurationChanged(), onLowMemory(), onTerminate(), getIstance().
As there is only a single intance of "Application" that is statically available it makes it a good place to store and share various objects that need to be passed around.

Android Dynamic Content Design

I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);

Categories

Resources