Sending a generated value to XML for formatting - android

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.

Related

Creating id for Android Views

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.

Text in android app (beginner...)

My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.

Can I use a colon in the id attribute in android?

I need help with something. I found a code sample that does not fully understand. Can I use a colon in the id attribute?
<TextView
android:id="#+id/Customer:Name" />
Thanks in advance.
This is not very well documented on the official site, but there are certain types of punctuation that are allowed as part of the ID in a layout XML file, including semi-colons and periods. However, when you actually reference these ids in Java code, they are converted to underscores. Thus something like this:
<TextView
android:id="#+id/customer.name"/>
Is referenced in java code like this:
getView().findViewById(R.id.customer_name);
It's legal (not an error) to do so, but it isn't a good practice, because it makes searching for the ID more difficult.

Android package/class name in layout XML?

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.

Android with eclipse

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.

Categories

Resources