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 questi
You can create custom attributes for your own classes that extend View or a subclass. The process is documented in the "Creating a View Class" section of the Android Dev Guide under the heading "Define Custom Attributes":
https://developer.android.com/training/custom-views/create-view#customattr
android:keyLabel is one of many XML attribute used by the Keyboard.Key class for each key on your keyboard. android:keyLabel is what you want to label the key with (like a "w" as in above). The attributes are pre-defined for the class. The "w" isn't but android:keyLabel is. If you could create android:alternativeKeyLabel what would you expect the class to do with it? I think maybe you should try to explain further what you are trying to accomplish.
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);
}
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:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_keyboard"> <attr name="alternative_key_label" format="string" /> </declare-styleable> </resources>
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();
}
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>