Refering to string through R.string - java

I have a simple button with text
If in android:text i refer to string it works as it suppose to.
<Button
android:id="#+id/true_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" /> // OK
<Button
android:id="#+id/true_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#id/R.string.true_button" /> // doesn`t output anything
However, if i try to refer to my string through R.string."name of string" nothing happens
Explain me please where i am missing smth...

There are a few things that you are doing wrong here. To reference a string in XML you should use
android:text="#string/string_name"
#string referring to the string.xml file and string_name being the name that you have declared.
This is what the line required in string.xml would look like
<string name="string_name">This is a string you are referencing!r</string>
Also I have never tried naming a resource with . separating words. This could cause an error but I am not 100% sure.
Edit:
michal.z is incredibly correct when he says that you cannot reference R. or android.R. resources from XML. you only use these when you are trying to reference a resource programatically.

In XML you use #+id/ or #id/ to specify the id which your View should have or to reference View previously declared in XML. You cannot reference R class in XML code because in fact R class is created on base of your resources defined in XML. So it is unreachable in XML.

Related

What is id used for? [duplicate]

This question already has answers here:
Difference between "#id/" and "#+id/" in Android
(12 answers)
Closed 7 years ago.
<TextView
android:id="#+id/button01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Sample"
/>
In this code,
what does
android:id="#+id/button01
mean?
And when do we use that id?
As said in Documentation
ID
Any View object may have an integer ID associated with it, to uniquely
identify the View within the tree. When the application is compiled,
this ID is referenced as an integer, but the ID is typically assigned
in the layout XML file as a string, in the id attribute. This is an
XML attribute common to all View objects (defined by the View class)
and you will use it very often. The syntax for an ID, inside an XML
tag is:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource. The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file). There are a number of other ID resources that
are offered by the Android framework.
Example in the same doc :
<Button android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_button_text"/>
Now you can uniquely identify this button with
Button myButton = (Button) findViewById(R.id.my_button);
<TextView
android:id="#+id/button01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Sample"
/>
This means that the TextView has a unique ID: button01 which can be used to access it in the application. To access it, we use:
TextView text = (TextView) findViewById(R.id.button01);
Operations can then be performed on this View, e.g.
text.setText("hello");
A unique resource ID defined in XML. Using the name you provide in the element, the Android developer tools create a unique integer in your project's R.java class, which you can use as an identifier for an application resources (for example, a View in your UI layout) or a unique integer for use in your application code (for example, as an ID for a dialog or a result code).
Ref:
http://developer.android.com/guide/topics/resources/more-resources.html#Id

imageview source in the java file and the xml file?

Looking at the code below;
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView1);
theImageView.setImageResource(R.drawable.dot);
Is the second line necessary if we established the source (android:src) of the image in the xml file? XML shown below;
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:src="#drawable/dot"
android:id="#+id/imageView1"/>
No. You either do it in XML or in Java code. You don't have to do it twice.
If you have already define ImageView and set image, then you don't even have to get it's reference in Java code (1st line), unless you want to change something.

#android:id is not recognized by eclipse

In my xml file I have below list view.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:divider="#color/blue"
android:dividerHeight="2dip"
android:background="#drawable/list_divider"
>
</ListView>
And in my java class I try to access the view as follows
ListView myListView = (ListView) findViewById(R.id.list);
I get a error for this saying "list cannot be resolved or is not a field". But if I change the id as android:id="#+id/list"
How can I access the view by using the id this way android:id="#android:id/list".
Also whats the basic difference between two ways of defining ID. Thanks in advance
Assuming your Activity extends ListActivity, you can access it with the convenience method
ListView myListView = getListView();
otherwise you would access it with android.R.id.list
Also whats the basic difference between two ways of defining ID
#android:id references built-in android resources
#+id is a user defined id and will add it to the R.java file to be accessed later with #id
See this post about more on that
You have to mention your id like
android:id="#+id/list"
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
Android resources id are referenced by #android:id/list in the xml
and android.R.id.list in the Java code.
User defined id are referenced by #+id/list in the xml and
R.id.list in the Java code. The + implies that the resource id value will be generated at compile time.
id define in resource files as <item type="id" name="list" /> will
be referenced as #id/list in the xml and R.id.list in the Java
code.

Nested findViewById

I have this problem accessing TextView inside Relative Layout inside LinearLayout
Let's say I have these views in the file row.xml
<LinearLayout>
<TextView android:id="#+id/title" />
<RelativeLayout android:id="#+id/a_parent">
<TextView android:id="#+id/a">
</RelativeLayout>
<RelativeLayout android:id="#+id/b_parent">
<TextView android:id="#+id/b">
</RelativeLayout>
</LinearLayout>
Now I want to change the value of TextViews from an activity
LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout temp=(LinearLayout) li.inflate(R.layout.row, null);
((TextView)temp.findViewById(R.id.title)).setText("This is title");
RelativeLayout rl=(RelativeLayout)temp.findViewById(R.id.a_parent);
((TextView)rl.findViewById(R.id.a)).setText("an this is the content of a");
I can successfully set the TextView of id 'title', but an error appears when approaching the last line. It says that the error was caused by android.content.res.Resources$NotFoundException: String resource ID #0x1
Can anyone tell me what is wrong, how to fix it, and why does the code not work as I expected?
Thanks
Your Exception is a ResourcesNotFoundException for a String. Are you setting the value of the TextView using getString? And if so, are you sure that this string exists in your strings.xml file? If not, did you maybe reference one of your views with R.string.name instead of R.id.name? It looks like you modified your code for posting, it may be easier to help if you posted exactly what you're doing in the original, as well as the full stack trace.
Sometimes problems with Resources occur because the R file isn't up to date with new code. I would also try Rebuilding/Cleaning your project and see if that helps.
Usually this error comes when you are trying to set the integer value in Textview
like below :
textview.setText(1);
In this case the android will think that the integer value as string resource id and checks for
the string the strings.xml and throws Resources$NotFoundException.
Please check your code. If not post your necessary code and logcat output.
LinearLayout temp=(LinearLayout) li.inflate(R.layout.row....
in Your XML file, LinearLayout has not "android:id" property is declared.

Difference between android:text="#string/hello" and normal right click--> Text view component--> EditText

While I am working with help of tutorial I found this in textview android:text-"#string/hello" it shows some error.
Then I went through graphical view and right click the component and enter the text. Then that error removes and notifies me that
**Hardcoded string hello should use String resources**
In android, "#string/" refer to the string.xml in Project>res>values> location in your package explorer.
String.xml contains a xml file which refer to a string with an id.
Eg:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World</string>
<string name="app_name">My app</string>
</resources>
Here name="hello" is the id & "Hello World" is its value. When #string/hello is used, the value will be shown.
Similar is the case with "#drawable/". It will refer to the images used. and many more.
You can set text in text view programmatically. Eg:
TextView tv = (TextView)findViewById(R.id.text1); //text1 is the id u provide in xml file
tv.setText("Hello World");
I hope it helped you.
#string/hello simply notice Android to load the String in a XML file located into the /values directory (strings.xml).
hello is the ID of the String specified by name="hello" into that XML.
Android Studio is very unstable. Previously, it auto generated the corresponding statement in the string.xml . Tutorials using previous versions get beginners stuck because of that.
How string.xml works
Android Studio expects you to write all texts in one place, in the string.xml file. In all other files, you just put abbreviations. e.g.
android:text="#string/Hello_world"
means that find the abbreviation Hello_world in the string.xml file and replace it by the intended text.
In string.xml, the corresponding statement which should be written is:
<string name="Hello_world">Hello world!</string>
and that should do the job!
It is possible to just write android:text="Hello world!" (This is what hardcoding is). If you intend to be an android dev, then you would not maximize efficiency.
Why? string.xml
Having all texts in one file eases many things. Like translation if needed and easy change. However, you should choose meaningful abbreviations!

Categories