When we are coding some GUI in android, we specify in the layout some containers (view groups) and views. These entities have some attributes which allow us to configure their behaviour. Attributes can have different formats / types of values, like string, integer, color, enum, etc. We can use already specified attributes in our custom views or we can create new ones. We can specify custom attributes in the declare-styleable entity. The best place for custom attributes is the attr.xml file, inside the res/values directory. The declare-styleableentity contains the attr elements. The attr element has two XML properties – name and format. The name allows you to refer the attribute in code (R.attr.attr_name) Define Attributes: define attributes in the top <resources> element or inside of a <declare-styleable>element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a <declare-styleable> element it can be used outside of it and you cannot create another attribute with the same name of a different type. 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.

  • reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")
  • color
  • boolean
  • dimension
  • float
  • integer
  • string
  • fraction
  • enum - normally implicitly defined
  • flag - normally implicitly defined
You can set the format to multiple types by using |, e.g., format="reference|color". enum attributes can be defined as follows:
<attr name="my_enum_attr">
  <enum name="value1" value="1" />
  <enum name="value2" value="2" />
</attr>
flag attributes are similar except the values need to be defined so they can be bit ored together:
<attr name="my_flag_attr">
 <flag name="fuzzy" value="0x01" />
 <flag name="cold" value="0x02" />
</attr>
In addition to attributes there is the <declare-styleable> element. This allows you to define attributes a custom view can use. You do this by specifying an <attr> element, if it was previously defined you do not specify the format. If you wish to reuse an android attr, for example, android:gravity, then you can do that in the name, as follows. An example of a custom view <declare-styleable>:
<declare-styleable name="MyCustomView">
 <attr name="my_custom_attribute" />
 <attr name="android:gravity" />
</declare-styleable>
When defining your custom attributes in XML on your custom view you need to do a few things. First you must declare a namespace to find your attributes. You do this on the root layout element. Normally there is only xmlns:android="http://schemas.android.com/apk/res/android". You must now also add xmlns:whatever="http://schemas.android.com/apk/res-auto". Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:whatever="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <org.example.mypackage.MyCustomView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>

Access Attributes: Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyCustomView extends View {

 private View mValue;
 private ImageView mImage;

 public MyCustomView(Context context, AttributeSet attrs) {
 super(context, attrs);

 TypedArray a = context.obtainStyledAttributes(attrs,
 R.styleable.Options, 0, 0);
 String titleText = a.getString(R.styleable.Options_titleText);
 int valueColor = a.getColor(R.styleable.Options_valueColor,
 android.R.color.holo_blue_light);
 a.recycle();

 // more stuff
 }


}
Resources of this article:
http://stackoverflow.com/a/3441986/4803974 http://www.vogella.com/tutorials/AndroidCustomViews/article.html
Total Views 39, Views Today 1

Leave a Reply

Your email address will not be published. Required fields are marked *