Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create my custom properties on xml for Android?

Tags:

java

android

We have in our project a keyboard with "Key" elements, this Key elements have attributes such as android:codes="119", android:keyLabel="w" and so on.

My question is how can I include an custom attribute like a "android:alternativeKeyLabel" to do something else.

like image 941
Tebam Avatar asked Jan 08 '10 18:01

Tebam


People also ask

How do I create an XML file on my phone?

File newxmlfile = new File("/data/new. xml"); You could save it to the internal storage, or cache.

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

Does Android still use XML?

Android applications use XML to create layout files. Unlike HTML, XML is case-sensitive, requires each tag be closed, and preserves whitespace.

How is XML used in Android?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.


2 Answers

This link gives a superficial explanation: http://developer.android.com/guide/topics/ui/custom-components.html

Considering you have a CustomKeyboard that inherits from KeyboardView/View:

  1. Create your custom properties in res/values/attrs.xml file (create the file if it does not exist):
<?xml version="1.0" encoding="utf-8"?> <resources>    <declare-styleable name="custom_keyboard">         <attr name="alternative_key_label" format="string" />     </declare-styleable>  </resources> 
  1. Create a constructor in your custom component overriding default constructor that receives the attribute set because this one will be called when the layout is loaded.

    public CustomKeyboard(Context context, AttributeSet set) {     super(context, set);     TypedArray a = context.obtainStyledAttributes(set,R.styleable.custom_keyboard);     CharSequence s = a.getString(R.styleable.custom_keyboard_alternative_key_label);     if (s != null) {         this.setAlternativeKeyLabel(s.toString());     }     a.recycle(); } 
  2. In your layout file, add your custom component and the link to your resources.

 <Layout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res/your.package.ProjectName"     .../>     ...     <your.package.projectname.CustomKeyboard         android:id="@+id/my_keyboard"         ...         app:alternative_key_label="F">     </your.package.projectname.CustomKeyboard> </Layout> 
like image 147
JPMagalhaes Avatar answered Sep 26 '22 06:09

JPMagalhaes


For any other purpose, declaring a custom property in the XML file can be retrieve with attrs constructor parameter.

In my case I reuse a preference custom dialog, and set things like that:

<?xml version="1.0" encoding="utf-8"?> <!-- something here --> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >     <your.package.projectname.CustomView         foo="bar"     /> </PreferenceScreen> 

Then in my class contructor:

public CustomView(Context context, AttributeSet attrs) {     String bar = attrs.getAttributeValue(null, "foo");     Log.d("CustomView", "foo=" + bar); } 
like image 44
vono Avatar answered Sep 24 '22 06:09

vono