Android with eclipse - android

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.

Related

Can I give an additional label for a TextView that's only announced by TalkBack, not shown visually?

Say I have a list of items, and one of the views in each item is a message, in a simple TextView, that is populated in code (dynamic data from a backend service).
<TextView
android:id="#+id/message"
style="#style/DefaultText"
/>
Visually it doesn't need a label; the list item layout is pretty clear. But listening to how TalkBack reads the screen, I think it would be beneficial to have a "label" or description for it. So that TalkBack would read something like:
Message: [the actual dynamic message content]
My question is: is it possible to add a label/description for a TextView that, firstly, does not replace the TextView content, but is read along with it, and secondly, only affects TalkBack (not the visual presentation)?
What I tried:
contentDescription for the TextView. Doesn't work: if set, the actual content is not announced, only the contentDescription. Hmm, maybe if I'd set this in code with the description prepended to the actual content... but is there no easier way?
Separate TextView with labelFor pointing to #+id/message. The problem is that it's also shown visually and screws up the design. If I make it not visible, one way or other, it seems TalkBack won't read it either.
Alright, I found two solutions! As mentioned in the question, I'd rather have avoided doing it in code, but it seems for optimal results that's the way to go.
Use android:hint in XML
The most simple way is to set android:hint="Message" for the TextView. TalkBack reads it after the actual text:
[the actual dynamic message content] Message
For my needs, with the message being optional, this has some drawbacks: the hint is announced even if the TextView has no actual content (I'd prefer to omit it then), and in that case it's also shown visually (not what I want). Also, I'd prefer TalkBack to read the hint before the actual text.
Set contentDescription programmatically
Not as simple, but not complicated either, and you have complete control over what TalkBack says.
In my case, preferring to omit the description if message is missing and having it read before the message content, I'd do something like this in the code that populates the list items:
textView.setText(message);
if (!TextUtils.isEmpty(message)) {
textView.setContentDescription(
context.getString(R.string.message_desc, message));
}
With resource:
<string name="message_desc">Message: %s</string>
You can use this namespace it not visually show on real screen
xmlns:tools="http://schemas.android.com/tools"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="your text"/>
In your XML file, set the importance to "no". You can also do this at run time using the View.setImportantForAccessibility() API.
android:importantForAccessibility="no"
Programmatically you can say:
view.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Example click here
click here for more info

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.

XML tags in Android- Is there a reference

I'm doing an Android XML GUI layout:
<EditTextPreference
android:key="PREF_LT_500"
android:title="Step 1"
android:summary="aaaaa"
android:inputType="number"
android:defaultValue="0"
android:dialogTitle="yyy" />
1)I want the inputType to be a decimal. I'm struggling to find the XML value that can provide this. inputType="number" only permits the digits, not the ".". What must the inputType value be to force the input to be a decimal number?
2)In general, I find it time wasting and difficult to try and figure out the XML tags and permitted values. I'm wading through the Android references, and sometimes the tags are listed, but often not. Is there a reference for the XML GUI schema?
3)I'm using Eclipse, and if I use the XML editor, it seems that some of the values are not resolving. It doesnt show the possible values for "inputType". Maybe my Eclipse is missing something?
Thanks
I found the answer to part 1 of my question.
inputType="numberDecimal".

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.

Sending a generated value to XML for formatting

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.

Categories

Resources