hardcoded string "row three", should use #string resource - android

I'm a beginner android developer , I was trying to run this Linear Layout in eclipse :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
And, I noticed :
1) yellow line under android:text="Yellow"
2) yellow line under android:text="row four"
the Triangle warn says [I18N] Hardcoded string "Yellow", should use #string resource "
and same for the rest of the warnings.Any suggestion?

It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.xml file.
It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.
example:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
This layout XML applies a string to a View:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/yellow" />
Similarly colors should be stored in colors.xml and then referenced by using #color/color_name
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
</resources>

You must create them under
strings.xml
<string name="close">Close</string>
You must replace and reference like this
android:text="#string/close"/>
Do not use #strings even though the XML file says strings.xml or else it will not work.

It is not good practice to hard code strings into your layout files/ code. You should add them to a string resource file and then reference them from your layout.
This allows you to update every occurrence of the same word in all
layouts at the same time by just editing your strings.xml file.
It is also extremely useful for supporting multiple languages as a
separate strings.xml file can be used for each supported language
the actual point of having the #string system please read over the
localization documentation. It allows you to easily locate text in
your app and later have it translated.
Strings can be internationalized easily, allowing your application
to support multiple languages with a single application package file
(APK).
Benefits
Lets say you used same string in 10 different locations in the code.
What if you decide to alter it? Instead of searching for where all it
has been used in the project you just change it once and changes are
reflected everywhere in the project.
Strings don’t clutter up your application code, leaving it clear and
easy to maintain.

You can go to Design mode and select "Fix" at the bottom of the warning. Then a pop up will appear (seems like it's going to register the new string) and voila, the error is fixed.

A good practice is write text inside String.xml
example:
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
and inside layout:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/yellow" />

Apart from the multiple language special case, what is wrong with the global find & replace approach as used in almost every other environment?
The 'one place' argument seems spurious. Changing a string of 'yellow' would not affect another of e.g: 'yellow paint'!

Related

how to create an android layout containing tabs, and split line?

I want to create a layout like this for android cellphone app
I have tried to use http://www.droiddraw.org/
but didn't find the tabs or split line widges.
This is the best I managed to generate:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Spinner
android:id="#+id/widget41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:layout_x="17dp"
android:layout_y="47dp" />
<Button
android:id="#+id/widget45"
android:layout_width="58dp"
android:layout_height="31dp"
android:text="Search"
android:layout_x="144dp"
android:layout_y="64dp" />
<Button
android:id="#+id/widget46"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="place request"
android:layout_x="212dp"
android:layout_y="62dp" />
</AbsoluteLayout>
Does someone knows a better UI Editor?
can you help adding the tabs, splitting line?
also, I want to create a gridview with fixed size (for simplicity)
how can I create one?
You must use xml for design the UI.
Android
And look this for tabs
tabs
tabs

RelativeLayout textviews overlapping

I have a rather simple ListView row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
When the text of "#+id/tournament_name"is long - it overlaps with the one from "#+id/tournament_winner" and I don't understand why.
I tried using android:singleLine="false"to no avail. I also tried using android:inputType="textMultiLine"as the docu says android:singleLine="false" is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ? so no good here as well.
I also tried using android:ellipsize="end" but this doesn't work. I assume it is because the text in the left TextView ("#+id/tournament_name") is NOT long enough to fill up the full width of the ListView (which code is not sowing here).
I was sure that if I use android:layout_width="wrap_content"the two TextView fields shouldn't overlap.
Here is an example (see the second line):
Any further ideas how this could be fixed?
android:layout_toLeftOf="#id/tournament_winner" in First TextView.
Also set android:maxLines="1" and Fix width for tournament winner because when it gets long tournament name cant see...
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/tournament_winner"
android:maxLines="1"
android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="WINER"
android:textSize="25dp" />
</RelativeLayout>
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="#+id/tournament_winner" but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="#+id/tournament_winner"and not android:layout_toLeftOf="#id/tournament_winner" - pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="#id/tournament_winner": Error: No resource found that matches the given name... so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tournament_winner"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
You can use single text view in place of two and simply display both strings in one text view !!

When changing language my custom buttons cuts off some of the text

I've made my own custom button styles and they work splendid with some languages on my phones.
I've made strings.xml for DK and UK. However when I chose for instance france as language on my phone my buttons gets cut off in the bottom.
Can't seem to figure out why it does that.
Any ideas? I've tried on a HTC Hero, HTC Desire and a GarminAsus A10, all does the same.
EDIT layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/bg"
android:padding="0dip">
<LinearLayout android:id="#+id/linearLayout1"
android:orientation="vertical" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginLeft="40dip" android:layout_marginRight="40dip">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/hintUsername"
android:text="#string/hintUsernameText" style="#style/CodeFont"></TextView>
<EditText android:id="#+id/unameEditText"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:inputType="textPersonName" android:hint="#string/usernameHint">
<requestFocus></requestFocus>
</EditText>
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hintPasswordText"
style="#style/CodeFont"></TextView>
<EditText android:id="#+id/pwEditText" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:inputType="textPassword"
android:hint="#string/pwHint"></EditText>
<CheckBox android:layout_height="wrap_content" android:id="#+id/rememberLoginCheckbox"
android:text="#string/rememberLoginCheckbox" android:layout_width="fill_parent"
android:gravity="center" style="#style/CodeFont"></CheckBox>
<Button android:id="#+id/loginButton" android:text="#string/logonButton"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#drawable/btn_custom" android:textSize="18dp"></Button>
<TextView android:id="#+id/errorTextView" android:text=""
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:textColor="#FF0000"></TextView>
</LinearLayout>
</LinearLayout>
Background for custom button:
I guess there is a possibility that my ninepatch is wrongly created, but it looks right when chosing danish or english language on my phones.
Ok, so I've solved this myself.
After going through my styles.xml file I found out that I had fixated the height of the buttons by 25dip. This looked right when my phones was using Danish or English language, but when changing it to any other language the button text was cut off, as shown on the picture in my previous post.
So the answer is to remove the height inside the styles.xml file and just set the height to wrap_content.
I don't know if this is a bug in the Android api, but I am going to report it and see what happens. It's really odd that it doesn't happen for those two languages but for every other language - I even tried to add a values folder for another language and added the same files from the default values folder that was used, but it didn't help.
So much for following tutorials blindly on the interwebs :P

What does open quote expected for "android:id" in XML?

'<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/droid_background" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="#string/hello"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<GridView
android:id=”#+id/videoGrdVw”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:numColumns=”auto_fit”
android:verticalSpacing=”5dip”
android:horizontalSpacing=”5dip”
android:columnWidth=”80dip”
android:stretchMode=”columnWidth”
android:gravity=”center”/>
</RelativeLayout>
You've got fancy quotes mixed in with regular plain old quotes - replace the ”s with "s and it should work.
It looks like there's a strange problem with the quotes that you've posted. Some of them are of a different type than the others - look at the ones in the GridView. I would try changing them to be the same as the quotes up above and see if that helps.

Internal link to TextView in Android?

I find myself stuck in something I think would be really easy to solve. My app contains a lot of TextViews and ImageViews, and one TextView contains the content listed. I want each element of the displayed content internally linked to the respective TextView. Probably the TextView isn't the right element to use, but I have trouble finding the right element.
The internal link should work exactly like internal links in html-documents... is this possible to achieve in Android?
strings.xml:
<string name="c3">3. Contents</string>
<string name="c4">1. Abstract
\n3. Contents
\n4. List of Abbreviations
\n5. Introduction
\n6. Materials & Methods
\n6.1 Literature Selection
\n6.2 Method
\n6.3 What is Android?
\n6.3.1 Dalvik Virtual Machine and Android Applications
\n6.3.2 Android Structure, Java and XML
\n6.3.3 Android Versions
\n6.4 The Development Environment
\n6.4.1 Android SDK
\n6.4.2 Eclipse IDE
\n6.4.3 Android Virtual Device
\n6.4.4 Secure Digital Card
\n6.5 “Hello World!” as Test of IDE
\n6.6 Test Application
\n6.7 Developing Strategy
\n7. Result
\n7.1 Facebook Connect
\n7.1.1 Facebook Application
\n7.1.2 Facebook Connect for Android
\n7.2 Networking
\n7.3 Data Synchronization
\n8. Discussion
\n9. Conclusion
\n10. References
\n10.1 Internet References
\n10.2 Lecture References\n\n
</string>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/smoke"
android:scrollbars="vertical"
>
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none"
>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="4px"
android:paddingRight="4px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/a"
android:textColor="#color/black"
android:gravity="center_horizontal"
android:textSize="16dp"
android:paddingBottom="2dp"
android:paddingTop="20dp"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/b"
android:gravity="center_horizontal"
android:textColor="#color/black"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/c1"
android:textColor="#color/black"
android:textSize="20dp"
android:paddingBottom="2dp"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/c2"
android:textColor="#color/black"
android:scrollbars="vertical"
/>
If I were you, I'd convert your content into HTML with internal links and use a WebView to display it. I have no idea how you would pull off what you are trying to do any other way. Moreover, that's the typical approach used for ebooks, which appears to be what you are trying to create.

Categories

Resources