This is probably a trivia question, but why are there package/class names in some people's XML layout files?
(please don't downvote this question if it is something trivial, i don't even know what this is called, so i couldn't look it up).
i was looking at a tutorial, and i saw something like this (in "sample.xml"):
<com.tutorials.foo
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- some buttons and views here -->
</com.tutorials.foo>
My questions are:
1) i'm assuming that "foo" is a custom view? say, like you want to extend TextView with your own version of TextView?
2) what is this pattern/technique even called?
3) what would be the advantages of using this method?
Thanks so much in advance!!!
Yes, <com.tutorials.foo .../> is a custom view.
Calling it will be as same as others.ex:
Foo foo=(Foo)findViewById(R.id.foo);
I assume you mean creating layout static(.xml) or dynamically with code. xml layout would be in advantage when you know that you will use this layout in the program and will not change its format. Of course you can add to it or edit it with code later on. It is also in advantage for readabilty.
Related
This question is quite basic. Let me go with an example.
I have two activities
activity_a.xml
ActivityA.java
activity_b.xml
ActivityB.java
Both the XML files contain only a TextView to display a simple text. As usual, the TextViews are going to be referenced in the corresponding .java files using their View id
My question is, if it is right to reference the TextView in both the XML files with same id? (like using the below code with exactly same id for activity_a.xml and activity_b.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I was practising this procedure without any problems. When trying to reach the corresponding xml code for the TextView using Ctrl + Click (on Windows), I am provided with two options (to display the TextView's xml code from activity_a.xml or from activity_b.xml).
Also, what is the recommended way to name a View in Android? This will be helpful, when your Android project contains multiple layout files.
Yes , if you have same name of view or view group in different layout then it tells from which layout file it belongs to, ask for select required layout file.
so for that you have to follow proper naming conventions to avoid this type of confusion
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
or you can give name like
activity_home_tvUserName if username textview from home activity
and
activity_profile_tvUserName if username textview from profile activity.
if it is right to reference the TextView in both the XML files with same id?
It is totally fine, the compiler will only look at the ID under a single view hierarchy.
e.g.: findViewById(R.id.textview) inside ActivityA.java will only search for textview ID inside activity_a.xml (assuming you have setContentView(R.layout.activity_a); beforehand.
what is the recommended way to name a View in Android?
In my opinion, you just need to be consistent in naming your view throughout the app. The main goal is to avoid misinterpretation and confusion.
Hope it helps!
They are in different activities, so you should have no problem using the same id. you could event declare them both as #+id/textview. However, why not just use the same XML file for both activities? No reason you can't.
You can also create an ids.xml file under the values folder and declare all your ids under it, so you don't have to declare them in your layouts, but this is not a very common approach.
if it is right to reference the TextView in both the XML files with same id?
My answer is Yes, It is right.
Whenever we set the setContentView(R.layout.activity_a), then it'll search for the given id within the above activity. Local attribute having the same id will take the more preference over the other attributes with the same id.
But having unique id's is Best practice.
I am developing an application where certain elements will repeat themselves a lot over the whole interface. Googling a bit i found out about the <include /> tag, which is working nicely for what i want.
I was just wondering if there is a way to export certain properties of the included layout: One of them has an image and a string that change according to the use case, and i'd like to set these in the XML file for each case, instead of having to write boilerplate code to set them in the code; Is there any way to do this? Or am i doomed to write that code?
We're all doomed. As explained in the article Creating Reusable Components, the only things that you can override are the layout_* attributes and the id. There's no way (sadly) to parameterize a layout like you describe.
The <include> tag is is useful for separating configuration-dependent parts of your layout from those parts that are invariant across devices. (E.g., you can <include layout="#layout/footer"> and have different footer.xml files for different configurations.)
I would like to begin by saying I don't have the best java knowledge because I'm still learning it in school, but I decided to go off on my own and attempt to create an android application.
Right now, I have my program generating two different random numbers. I'm trying to get these generated ints to be shown in the application. I want it to be done through XML formatting tho because of the control I have over what I want it to look like.
My issue is that I can't seem to figure out how to pass the values over to the XML file. I'm not sure if I should be using "android:text" and somehow integrating the name of the ints in that(Which i've attempted.) or what.
I've tried to google for an explanation of what can be done, but I think my lack of ability to really put my issue into words is holding me back.
You won't be using the XML file to set the Text. You use the xml file to set the look and feel, like this (note the android:id= line):
<TextView android:id="#+id/randomInt1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
<TextView android:id="#+id/randomInt1" />
android:layout_height="wrap_content" android:layout_width="wrap_content"/>
then in your java code in onCreate for the activity that will be showing the ints you will do something like this:
int random_int1=0;
int random_int2=100;
((TextView)this.findViewById(R.id.randomInt1)).setText(random_int1);
((TextView)this.findViewById(R.id.randomInt2)).setText(random_int2);
The way it works is you need to make a "link" between the java and the xml.
This can be done in java with the following line(quick example I pulled out of my code):
TextView nameText = (TextView) findViewById(R.id.nameTextView);
then this can be set using:
nameView.setText(PutStringHere);
This is a fundamental part of android development. It's the link between the java and the xml. You can edit other things though the java TextView.
I've started learning coding for the android, I know the basics of programming in general and thought that android would be fun, Which it has so far.
Now in my exercises in the book I have, It says to add more text to the application. The application is nothing at the moment but 1 string, And I have to add another string.
Now when I have added the string to the strings.xml file and then on the main.xml I type:
android:text="#string/AppName" />
AppName is the new string I made which in the strings.xml it looks like this:
This App is called Droid1
The weird thing is when I type in the main xml to referr to the string, It doesnt even get colour coded when I type the android:text part. The whole line stays as the black text colour. Im sure im not missing anything as the string is all colour coded and so is the last string that I referred to while following the examples in the book which is:
android:text="#string/hello" />
And this is what is confusing me. So please point out the obvious or not so obvious thing that I have done wrong. Any help at all will be appreciated
Android uses the XML format to define interfaces and such. The line
android:text="#string/AppName" />
is not valid XML, and the black syntax is Eclipse's way of showing this. This is probably because you have forgotten to put some lines above it. What you want to have is something like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AppName" />
This tells Android that you would like to put a TextView (or, a text label or whatever) with your string in it. Also note that the android:layout_width and android:layout_height attributes here are always required: without them, you will get an error and you can't build your application.
If you are not already familiar XML, I would highly recommend taking a look at an XML tutorial (for instance at Tizag or W3Schools) and learn the basics of XML, since understanding the XML language simplifies Android programming a lot.
If you want your text to have color, you can set android:textColor="######" in your xml either textview or whatever you used to display the string. ###### is your color code.
Let's say i have a layout with style:
<LinearLAyout ... >
<TextView ... style=someStyle />
<ImageView ... style=someImageStyle ... />
</LinearLayout>
the style will be defined in an xml in my project.
How can i override that style with an external xml ? (i'm asking because i've noticed the View does not have applyStyle\setStyle or anything of that sort (best bet, because style need to be parsed, compared against android:attr for validation and then applied on each item of the view).
I do wonder how am i suppose to make downlaodable themes for my app.
After some experience i can share my thoughts about it here in my own question...
So first remark is, even in your own styles, use Dimentions and colors and attributes as much as you can. this way if you ever want to programatically apply a style to a view you can use getResources.getDimen... , getRessources().getColor(), getResrouces.getDrawable() etc...
In addition you can take the code from the aapt or open an existing apk and take the compiled xml from there and then use the same code reading the xml (it's C code mind you!!!) from android source. i would not do it from the simple reason it's an overkill to apply a simple style to a view.
The time it takes to write the method is too short. and if you must do it in xml, you can create an xml file with just your view and then inflate it, you can't replace the style in run time though. for that you have to define your own mechanism to replace colors, sizes, backgrounds etc... and supply the images as well, which is not so easy, reading those images from the local storage, if i'm not mistaken is less productive then readin them from assets or drawable directories.