Assign R.id to dynamically created Edit Text - android

I am new to this. wanted to know if there is a way to assign R.id to dynamically created edit text so i can move the data with in to SQLite db. tried few diff ways but fail. Any help would be appreciated. Thanks

In android id should be unique. Setting any arbitrary integer value can lead to duplicate ids. The correct way is to define id like below.
Create a new xml file named ids.xml inside res/values folder.
Add new item like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="button_group_cancel" />
</resources>
Now you can set the id to edittext as:
edittext.setId(R.id.button_group_cancel);

You can assign an id using .setId(int) before you add the EditText to the layout. However this does not place the id into the R.java file.
You can reference the EditText with findViewById(int) instead of using findViewById(R.id.editTextId), where int is the same int used in the set method.

Related

How can we set id as a number for UI Elements?

I am quite interested and confused both. Sometimes ago I was working on a project who has already implemented by some one. And after worked on that i found that he was using a unique number for find ID for its UI elements. Just like if we need to find id for TextView then we write,
TextView textview = (TextView)findViewById(R.id.text);
but in that code it was like,
TextView textview = (TextView)findViewById(012345678);
something like this.I have searched a lot in project also tried out to find that number but i couldn't. In R.java file i have also checked but there was also not. Its unique id its generated automatically as usual but i couldn't find a number in whole project.
So my question how can we use like this way? Is this possible??
TextView textview = (TextView)findViewById(012345678);
If this is possible then where we can define that id number and use it for UI elements.
Help will be appreciated!!
from folder gen you can find R.java there is a class named id like in image.
for example
public static final int action_bar=0x7f06001c;
so instead of R.id.action_bar i can use 0x7f06001c.
That is not directly possible as you might think. You can define your own custom ids which are not used in any xml files. I think that is what you want. To do this create a new xml files in res/values/ids.xml with this content:
<resources>
<item name="your_custom_id" type="id"/>
</resources>
Than you can use it as R.id.your_custom_id I think that is what you want.

Defining variable in XML file

I have an XML file with 5 textviews, all with the same textSize. Currently, I define the size like this:
android:textSize="30dp"
However, I want a quick way to apply something like:
android:textSize="text_size"
Where textSize would be a variable I define somewhere, and can use wherever I want in my XML file. I tried defining these variables in the strings.xml file in the Android project, and calling them like this:
android:textSize="#string/text_size"
Where it would be defined like this in strings.xml
<string name="text_size">30dp</string>
It worked fine in Ecplise itself, and showed just fine in the Graphic Layout. But when I tried running the app on my phone, it crashed.
So, is there a way to define variables in my XML layout files? Thanks.
Your idea of putting it in strings.xml is close, but those values need to be set in a dimensions XML file. The documentation is here.
Using your example, you might make a file called res/values/my_dimens.xml. Then put in there
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_text_size">30dp</dimen>
</resources>
Then to use it in your layout, just set
android:textSize="#dimen/my_text_size"

R.id.pager? Where is this defined?

The example for viewpager, here, contains the lines:
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
I don't understand how R.id.pager is defined. Am I supposed to create a viewpager in xml somewhere? But that wouldn't make sense because it's instantiating a viewpager in the previous line. If someone could clear this up for me I would be most grateful!!
Thank you!!
EDIT
Apparently changing the line to:
mViewPager.setId(1);
Makes it work :) :)
You can define the id directly in a resource file in the res/values directory. The name of the file is irrelevant, but it could look something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="pager" />
</resources>
IDs can also spring up automatically in layouts when you set an id for a layout element using an attribute like this:
android:id="#+id/pager"
The + in the attribute says to add the indicated id to R.id if it isn't already there.
It's better to use an XML-defined ID rather than hard-coding a value into your code, for the same reason that it is better to use symbolic constants (e.g., final static int FOO = 1;) rather than sprinkling integer literals everywhere.
See the docs on ID resources for more info.
It is defined in R.txt file under bin folder.
int id pager 0x7f05003c

Programmatically assign resource ids

I am creating views programmatically and later want to call 'addRule' and pass their id to this function.
Is it safe to assign ids programmatically?
How do I know that I am not overriding existing ids?
Is there a resource type '0' or can I presume that any resource id I create of the format 0xNNTTNNNN where 'TT' is the type = 0 is ok?
EDIT:
Use View.setId (int id)
Quote:
Sets the identifier for this view. The identifier does not have to be
unique in this view's hierarchy. The identifier should be a positive
number.
You can define some ids in an xml file, say, ids.xml in the res/values directory and then you can assign these ids either programmaically or by referring to them in other xml files (say, in your layout files). This way you can, for example, guarantee that a certain view will have the same known id in all the layouts where you want it to appear
You define an id as follows:
<resources>
<item type="id" name="myid" />
</resources>
And refer to it as usual "#id/myid"

Android: Possible to get a custom R.id

Is it possible to get android to give me a custom id?
so for example if I already have defined in xml:
R.id.some_layout
R.drawable.some_drawable
is there any function like this
R.custom_id("a_custom_id")
so I could then access as
R.id.a_custom_id
You can not dynamically create new IDs. Even if R was capable of doing so, you wouldn't be able to access it using R.id.a_custom_id. Java is not dynamic language, and cannot add fields at runtime.
There is, however, compile-time solution. In your res/values/ids.xml add:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="a_custom_id"/>
</resources>
And then you can reference R.id.a_custom_id in your code and "#id/a_custom_id" in xmls. Of course its still pre-defined id (as opposed to runtime-defined id).
You can create boolean, integer, dimension, color, and other array resources.
http://developer.android.com/guide/topics/resources/more-resources.html

Categories

Resources