How to read android:layout_height from style resource in custom View - android

I am building a custom View that contains two standard Views. I have a default style for each contained View, and a custom attribute that lets the user specify a custom style for each contained View. I can get the default vs. custom styles just fine, and pass the right style id as the third parameter of each contained View's constructor. What I am having a hard time doing is generating a ViewGroup.LayoutParams for these contained Views, based on the android:layout_height and android:layout_width in the appropriate style.
It seems like I need to use the ViewGroup.LayoutParams(Context, AttributeSet) constructor, and the AttributeSet docs say that I should get an AttributeSet via
XmlPullParser parser = resources.getXml(myResouce);
AttributeSet attributes = Xml.asAttributeSet(parser);
... but that throws a Resources$NotFoundException with a warning from frameworks/base/libs/utils/ResourceTypes.cpp that Requesting resource %p failed because it is complex.
Hence, my questions, in decreasing order of specificity:
Is there a way to get an XmlPullParser that works with "complex" elements?
Is there some other way to get an AttributeSet that corresponds to a <style> element?
Is there some other way to construct a LayoutParameters that will pay attention to the layout_height and layout_width values in a given style?

static ViewGroup.LayoutParams layoutFromStyle(Context context,
int style) {
TypedArray t = context.getTheme().obtainStyledAttributes(
null,
new int[] { android.R.attr.layout_width,
android.R.attr.layout_height }, style, style);
try {
int w = t
.getLayoutDimension(0, ViewGroup.LayoutParams.WRAP_CONTENT);
int h = t
.getLayoutDimension(1, ViewGroup.LayoutParams.WRAP_CONTENT);
return new ViewGroup.LayoutParams(w, h);
} finally {
t.recycle();
}
}

Related

Re-using android's built in xml attributes for a custom view [duplicate]

I wrote a custom view that extends RelativeLayout. My view has text, so I want to use the standard android:text without the need to specify a <declare-styleable> and without using a custom namespace xmlns:xxx every time I use my custom view.
this is the xml where I use my custom view:
<my.app.StatusBar
android:id="#+id/statusBar"
android:text="this is the title"/>
How can I get the attribute value? I think I can get the android:text attribute with
TypedArray a = context.obtainStyledAttributes(attrs, ???);
but what is ??? in this case (without a styleable in attr.xml)?
use this:
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
int[] set = {
android.R.attr.background, // idx 0
android.R.attr.text // idx 1
};
TypedArray a = context.obtainStyledAttributes(attrs, set);
Drawable d = a.getDrawable(0);
CharSequence t = a.getText(1);
Log.d(TAG, "attrs " + d + " " + t);
a.recycle();
}
i hope you got an idea
EDIT
Another way to do it (with specifying a declare-styleable but not having to declare a custom namespace) is as follows:
attrs.xml:
<declare-styleable name="MyCustomView">
<attr name="android:text" />
</declare-styleable>
MyCustomView.java:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);
CharSequence t = a.getText(R.styleable.MyCustomView_android_text);
a.recycle();
This seems to be the generic Android way of extracting standard attributes from custom views.
Within the Android API, they use an internal R.styleable class to extract the standard attributes and don't seem to offer other alternatives of using R.styleable to extract standard attributes.
Original Post
To ensure that you get all the attributes from the standard component, you should use the following:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView);
CharSequence t = a.getText(R.styleable.TextView_text);
int color = a.getColor(R.styleable.TextView_textColor, context.getResources().getColor(android.R.color.darker_gray)); // or other default color
a.recycle();
If you want attributes from another standard component just create another TypedArray.
See http://developer.android.com/reference/android/R.styleable.html for details of available TypedArrays for standard components.

Android custom image view using built-in attributes

It's my very first question here, so please go easy on me ;)
I've built my custom View class extending ImageView.
public class CustomImageView extends ImageView {
// ...
}
I have created a set of custom parameters for it in the shape of a <declare-styleable> item in the attrs.xml file.
<declare-styleable name="CustomImageView">
<attr name="angle" format="integer"/>
</declare-styleable>
I've figured out how to access (i.e. read from within the class and set from within the layout) these values.
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, 0, 0);
try {
a.getInt(R.styleable.CustomImageView_angle, 0);
} finally {
a.recycle();
}
So far, so easy. All of the above are directly taken from the guide.
However, I could not figure out how to access the inherited attributes of the ImageView class. Specifically, I want to read what was set as the src attribute of the ImageView. I'm assuming I have to use a different value for the second parameter of the obtainStyledAttributes(...) call, but I don't know what to use there and this obviously does not work:
a = context.getTheme().obtainStyledAttributes(attrs, ImageView, 0, 0);
So, how do I access the built-in attributes of my super class?
How do I get the int value (drawable res id) that was set for the android:src attribute?
Thanks for your help!
How do I get the int value (drawable res id) that was set for the
android:src attribute?
Use getAttributeResourceValue to get id of drawable :
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
String android_schemas = "http://schemas.android.com/apk/res/android";
int srcId = attrs.getAttributeResourceValue(android_schemas, "src", -1);
}

How to require a view with specified id

I want to write a custom view that requires some button with a speicific id to be present (to be able to be found by id), as in ListActivity android.R.id.list must be present, how do i do the same with a custom id of my own?
I plan on reusing this view in an Android lib for several use cases, but all of them must declare some specific view with specific id so that i can find it by id in the lib code and manipulate it for later use in using Applications...
Just do what the ListActivity does.
Check for the ID in your custom view and throw an exception if it does not exist in the layout.
Snippet from ListActivity Source:
#Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(com.android.internal.R.id.empty);
mList = (ListView)findViewById(com.android.internal.R.id.list);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
The best way to do this is in a flexible way is to use a custom attribute. It allows for any id to be the required id, but it also uses the dev tools to enforce that a valid id is used.
Declare that your custom view is style-able with a custom attribute in an attrs.xml file like this:
<resources>
<declare-styleable name="MyView">
<attr name="required_view_id" format="integer" />
</declare-styleable>
</resources>
You can then refer to the attribute from a layout file like below. Pay special attention to the header where the "app" namespace is defined. You can use any name you want for your custom attribute namespace, but you have to declare it to use any of your custom attributes when defining the views later. Note the custom attribute on MyView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="#+id/the_id_of_the_required_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.full.package.to.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:required_view_id="#id/the_id_of_the_required_view" />
</LinearLayout>
Now you need to make sure this view is present in your custom view class. You can required that your custom attribute is set by overriding certain constructors. You'll also need to actually verify the presence of the required view at some point. Here's a rough idea:
public class MyView extends View {
private int mRequiredId;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
checkForRequiredViewAttr(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
checkForRequiredViewAttr(context, attrs);
}
// Verify that the required id attribute was set
private void checkForRequiredViewAttr(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView, 0, 0);
mRequiredId = a.getResourceId(R.styleable.MyView_required_view_id, -1);
a.recycle();
if (mRequiredId == -1) {
throw new RuntimeException("No required view id attribute was set");
}
}
// This allows the custom view to be programmatically instantiated, so long as
// the required id is manually set before adding it to a layout
public void setRequiredId(int id) {
mRequiredId = id;
}
// Check for the existence of a view with the required id
#Override
protected void onAttachedToWindow() {
View root = getRootView();
View requiredView = root.findViewById(mRequiredId);
if (requiredView == null) {
throw new RuntimeException(
String.format("Cannot find view in layout with id of %s", mRequiredId));
}
super.onAttachedToWindow();
}
}
Using onAttachedToWindow to check for the required view may not be good enough for your purposes. It won't, for example, prevent the required view from being removed. Finding a view in a layout isn't a cheap operation, especially for complex layouts, so you shouldn't constantly check for it.

ImageView Constructor usage

There are several constructors available for defining an ImageView.
For Example
1) public ImageView (Context context)
2) public ImageView (Context context, AttributeSet attrs)
3) public ImageView (Context context, AttributeSet attrs, int defStyle)**
I am confused in using 2nd and 3rd type of constructor.
basically i don't know what to pass in place of AttributeSet.
Kindly provide a coding example.
These constructors are defined in the View documentation. Here is a description of the parameters from View(Context, AttributeSet, int):
Parameters
context The Context the view is running in, through which it can access the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyle The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may
either be an attribute resource, whose value will be retrieved from
the current theme, or an explicit style resource.
It's worth noting that you can pass null in place of an AttributeSet if you have no attributes to pass.
In terms of coding the AttributeSet, here's a bit of code I use for a custom TextView class I have:
public EKTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// ...
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LocalTextView);
determineAttrs(context, a);
}
// ...
}
private void determineAttrs(Context c, TypedArray a) {
String font = a.getString(R.styleable.fontName);
if (font != null)
mTypeface = Typeface.createFromAsset(c.getAssets(), "fonts/" + font);
mCaps = a.getBoolean(R.styleable.allCaps, false);
}
As you can see, once you get a TypedArray from the attributes, you can just use its various methods to collect each of the attributes. Other code you may want to review is that of View(Context, AttributeSet, int) or Resources.obtainStyledAttributes(AttributeSet, int[], int, int).
Ways of creating imageView, ImageView with Context
ImageView image= new ImageView(context);
Here when you want set the values like height, width gravity etc you need to set
image.set****();
based on the number of attributes you need to use no of setXXX() methods,.
2.Using Attribute set
you can define set of attributes like height, width etc in your res/values folder in separate xml file, pass the xml file to getXml()
XmlPullParser parser = resources.getXml(yourxmlfilewithattribues);
AttributeSet attributes = Xml.asAttributeSet(parser);
ImageView image=new ImageView(context,attributes);
Here you can also define your custom attributes in your xml . and you can access the by using the methods provided by AttributeSet class example
getAttributeFloatValue(int index, float defaultValue)
//Return the float value of attribute at 'index'

layout_width and layout_height

Is there any way I can get around having to add the layout_width and layout_height parameters to my custom views? I know there are a few built in android views that you don't have to supply those attributes for.
It's not a View's responsibility to decide whether or not it can/should provide these attributes. The parent ViewGroup dictates whether these attributes are mandatory or not. TableRow for instance makes them optional. Other layouts (LinearLayout, FrameLayout, etc.) require these params.
When would you want to not use the height and width parameters? I'm not sure but I think that would cause them to not even show up on the layout?
Look here for reference http://developer.android.com/guide/topics/ui/declaring-layout.html#layout-params
From the same Reference Dave has suggested.
All view groups include a width and height (layout_width and
layout_height), and each view is required to define them. Many
LayoutParams also include optional margins and borders.
So it looks like, you have to.
If reducing common and redundant attributes is what you want, then you should try styling.
Developer guide here.
The problem is that ViewGroup.LayoutParams.setBaseAttributes() uses the strict getLayoutDimension(int, String).
You need to extend whichever LayoutParams you need and override setBaseAttributes.
Inside you can either manually set width and height or use the more lenient getLayoutDimension(int, int). Finally, you'll have to override in your layout class that you are using your own LayoutParams.
#Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends FrameLayout.LayoutParams {
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
width = a.getLayoutDimension(widthAttr, WRAP_CONTENT);
height = a.getLayoutDimension(heightAttr, WRAP_CONTENT);
}
}

Categories

Resources