How can I create my custom properties on xml for 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.
Asked by: Brad398 | Posted: 24-01-2022
Answer 1
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:
- 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>
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.
Answered by: Lyndon723 | Posted: 25-02-2022<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>
Answer 2
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);
}
Answered by: Vanessa327 | Posted: 25-02-2022
Answer 3
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
Answered by: Haris804 | Posted: 25-02-2022Answer 4
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.
Answered by: Lydia817 | Posted: 25-02-2022Similar questions
android - Project has no target set. Edit the project properties to set one
When I import a new existing project into Eclipse, I get the following error message when attempting to execute as an Android project:
Project has no target set. Edit the
project properties to set one
Any help is appreciated.
android - How do I iterate through the id properties of R.java class?
I've 16 textViews and need to set something like this done:
for (int i=1; i<6; i++)
{
int $RidInt = R.id.s;
tv[i] = (TextView)findViewById($RidInt);
tv[i].setTypeface(face);
tv[i].setClickable(true);
tv[i].setOnClickListener(clickListener);
}
my R.java file is :
public final class R {
public static final class attr {
}
public static final...
linux - Android Plugin in Eclipse 3.5 / 3.6 on Ubuntu 64bit got problems with properties
I got a huge problem with the Android Development Tools ADT running in Eclipse Galileo (3.5.1) on Ubuntu 9.10, 64bit.
[Edit: as well on Ubuntu 10.10 and Eclipse Helios up to the latest 3.6.2!]
On this platform, I do not manage to see any property edit dialogs for layout properties. E.g. the one where you can select a string resource ID for text fields, or a drawable ID for image fields or backg...
android - Project has no target set. Edit the project properties to set one
When I import a new existing project into Eclipse, I get the following error message when attempting to execute as an Android project:
Project has no target set. Edit the
project properties to set one
Any help is appreciated.
android - Setting TextView properties in a ListView - Strange Result
I have an activity with a ListView which is populated through a custom ArrayAdapter. There is an array of objects which have a Boolean property called 'isRead'. Based on the value of this property, I want to set the typeface of one of the TextViews in the row to either 'normal' or 'bold'. I also set the color of the text to either 'gray' or 'black'
When the Activity initially starts up, everything works as expec...
android - How do I iterate through the id properties of R.java class?
I've 16 textViews and need to set something like this done:
for (int i=1; i<6; i++)
{
int $RidInt = R.id.s;
tv[i] = (TextView)findViewById($RidInt);
tv[i].setTypeface(face);
tv[i].setClickable(true);
tv[i].setOnClickListener(clickListener);
}
my R.java file is :
public final class R {
public static final class attr {
}
public static final...
android - AVD properties for Motorola Flipout?
I'm looking to create AVDs to test my apps on, and i'm trying to figure out the correct settings for a square screen such as the new Motorola Flip Out. I don't see a wikipedia article for it, or many specs...
On a related note, what about tablets? Actually.. Why aren't there a set of AVD instances with params from popular devices.. or at least a site that lists them for each of the 60+ devices.
Thanks in a...
Android Bundle in properties
(I'm Coming from a C# background writing iPhone apps with MonoTouch)
I have a simple app that needs to save button color. I have this code in a Settings class that is a singleton.
Is this the way it 'should' be done? I usually like having a single Setting instance that is accessed around the app in my C# iPhone apps.
private static Bundle bundle = new Bundle();
public static int getButton...
android - Changing View properties after onCreate()
First time poster, new to Android, and I seem to have hit a roadblock on this problem:
I'm creating a dynamic layout, consisting of several identical "composite" objects (these are basically "floating" LinearLayouts, each containing an icon (ImageView) and a caption (TextView)). The (x,y) coordinates of each LinearLayout are subject to change, based on user actions, and require precise placement (ie: can't use Rel...
Android: Edit project properties - change app name
I have a project which I created a few days ago as a test project and now after I've done some very nice stuff with it I need to change some basic information.
I am using Eclipse IDE and I was able to change some things, such as class names. The problem is I can't figure out where to change the project name.
For the installed application on my android emulator, the text below the app icon is showing t...
android - Error "Failed to load properties file for project 'MJAndroid'" in eclipse
I have Downloaded an example "MJAndorid" from the O'reilly.
But after importing the project into workspace, m getting the below error log:
Failed to load properties file for project 'MJAndroid'
I am not able to apply the build target, Google API version 3 , in this case.
Also, by clicking on Android in the properties window of the project, I'm geting "The currently displayed page contains ...
java - Android, avoid typing the object name for each of its properties in a method?
I have an object instance called myClass and i need to set the value of about 30 variables in the object, and I need to do it about 30 times (in 30 methods) in my class.
myClass.vOne
myClass.vTwo
...
myClass.vThirty
Is there some way I can save myself from having to type "myClass." 900 times?
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android