What is id used for? [duplicate] - java

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

Related

Get Ids Dynamically using findViewById() in Android using Java

In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}

#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.

Refering to string through R.string

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.

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.

Equivalent of HTML 5 data attributes for android views

Is there a way I can store/retrieve arbitrary values from a view, similar to HTML5 data attributes?
This way, I can have a view call generic onClick() methods and the method can retrieve the related data.
e.g:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_france" />
...
I would like to be able to retrieve a value from the one that got clicked.
public void setCountry(View v){
//retrieve data somehow
}
You can use the View's tag property. It is intented for that purpose.
For example:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany"
android:tag="Germany" />
...
public void setCountry(View v) {
System.out.println(v.getTag());
}
Derive from ImageView, add a member variable "country" and accessor methods. Then you can reference your class in the layout file:
<com.foo.MyImageView ... />
Or use the tag property. It lets you attach an arbitrary object to a control, but there's no type safety, and no accessor methods with appropriate names. You can specify a string tag right there in the XML file.
Or use some kind of mapping from control ID to country.

Categories