Android: Possible to get a custom R.id - android

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

Related

Xamarin custom attribute of reference type does not work

I'm trying to create a custom attribute for my control. Here's my attrs.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="ImageView">
<attr name="testcustom" format="reference"/>
</declare-styleable>
</resources>
In my test app at root, I add this namespace xmlns:TestCustom="http://schemas.android.com/apk/res-auto"
Later in my layout file I have an image view
<ImageView
android:src="#drawable/Icon"
TestCustom:testcustom="#drawable/Icon"/>
The first android:src property is fine, however TestCustom does not work.
The error given is "No resource found that matches the given name (at 'testcustom' with value '#drawable/icon)
So... what's going on here? Anyone have any ideas?
You need to specify your xmlns: to your specific package namespace. For example:
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"
Represents custom: for the package namespace of com.example.customviews.
You can read more about this here: https://developer.android.com/training/custom-views/create-view.html#customattr
Note: You may want to consider following the convention of camelCase with regards to your custom attributes, views, etc.
So to recap: The solution here was to use lower case name when referencing the resource via a custom attribute. Apparently Xamarin is doing some work in the background that normally allows resources that start with upper case to be OK, but it probably doesn't address this for custom attributes.
If you are using Xamarin, and custom attributes and you can't reference your resource, try it in lower case!!!
Or as Jon recommends, don't use upper-case stuff at all for resources.

Assign R.id to dynamically created Edit Text

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.

Do com.android.internal.R.attr and android.R.attr have the same set of values?

I need to use a built in value from Android resources. This value is stored com.android.internal.R.attr.listViewStyle. Being unable to get that from within my code, I tried to find the appropriate value I can use insted. Well, I've just found android.R.attr.listViewStyle.
Question 1: Are these values same?
Question 2: Where can I find the XML for com.android.internal.R.attr.listViewStyle? May be I have to create my own style instead that one. In order to find it out I should look at that file.
Sorry if these questions are silly. I'm new to Android development yet.
com.android.internal classes are internal to android, they are only accessible within frameworks.
I think com.android.internal.R.attr.listViewStyle and android.R.attr.listViewStyle are same.
If you want to create your own style you can check here . This contains two listViewStyle. They are used based on the device default theme(Light or dark).
If you want to use this style, then i think you dont need to specify anything in your code, this is default theme, so it is picked automatically, if no attributes are specified.
You can add listViewStyle in values/attr.xml with this code :
<attr name="listViewStyle" format="reference" />
Change com.android.internal.R.attr.listViewStyle in your code to R.attr.listViewStyle
I find it in this
Example for attr.xml
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< attr name="listViewStyle" format="reference" />
</resources>

Create a custom Drawable and use it in XML

I am busy creating a less complicated version of NinePatchDrawable that tiles the inner panels instead of stretching them.
I can implement the actual logic to do the rendering and such.
However, I can't find documentation about how to use this Drawable in the XML files. Is this possible? That is, can I have a <tileable-nine-patch>, or some such, tag I can use to define resources?
Unfortunately it is not possible to use custom drawables in XML files (for API 23 and below) due to potential security issues. See here for a Google Groups thread about this very topic with a response from Romain Guy, one of the Android developers.
You can still use them in your Java code though.
Update:
As LeoFarage points out in the comments, starting in API 24 you can now use custom drawables in XML files but only within your application package.
Starting Android API 24, custom Drawables classes can be used in XML only within your package.
Custom drawables classes may be used in XML in multiple ways:
Using the fully-qualified class name as the XML element name. For this method, the custom drawable class must be a public top-level class.
<com.yourapp.MyCustomDrawable
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffff0000"/>
Using drawable as the XML element name and specifying the fully-qualified class name from the class attribute. This method may be used for both public top-level classes and public static inner classes.
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
class="com.myapp.MyTopLevelClass$InnerCustomDrawable"
android:color="#ffff0000" />
Note: that is not supported by support library.
Too late, but since there is no clear answer here what I tried. As #Casabancais answer copied from Android docs.
Create a drawable xml named say custom_drawable.xml with this
<?xml version="1.0" encoding="utf-8"?>
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
class="com.my.package.path.MyCustomDrawable"
android:color="#ffff0000" />
com.mypackage.MyCustomDrawable.java should be your java drawable class
Then you can use it in lets say background tag for any xml like this
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="#drawable/custom_drawable"
android:layout_height="wrap_content">

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Categories

Resources