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.
Related
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)
}
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
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.
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.
I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});