Show my custom layouts in the "tools palette" when editing layouts - android

Is it possible to show custom widgets and layouts that you've created in your projects as part of the tools palette in the layout editor?
There is a section called "Custom & Library Views" that is empty with a refresh button.

Custom Layouts Requirements
For a custom layout to appear in the tools palette it must conform to the following:
You must implement a constructor that takes a Context and Attributes as parameters.
You must define custom attributes using the <declare-styleable> tags in the XML.
Here is an example of the constructor.
class PieChart extends View {
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Here is an example of the attributes.
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
Further reading on Creating Custom Views.

Related

Custom visibility xml params

I want to create a new xml parameter for one of my custom views to handle the visibility of something within it. I can do it other ways of course, but I would like to do it in a way that I can use normal android params to change it.
something like:
<MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:my_custom_visibility="gone"/>
so I coulg just do
viewToBeHidden.visibility = a.getInt(R.styleable.my_custom_view_my_custom_visibility, View.Visible);
I tried because visibilities are integers but it doesn't let me put gone in the view declaration
<attr name="my_custom_visibility" format="integer" />
using
<attr name="my_custom_visibility" format="reference" />
results in a compilation error:
AAPT: error: 'gone' is incompatible with attribute my_custom_visibility (attr) reference [weak].
the other types available for the attribute don't seem to apply to this case.
is there a way to achieve this?
Based on #Pawel and #Sam comments, I created a dedicated enum attribute like this:
<attr name="my_custom_visibility" format="enum">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>
and use it like this in the view:
viewToBeHidden.visibility = attrs.getInt(R.styleable.my_custom_view_my_custom_visibility, View.VISIBLE);
so it would feel just as the "original" one

Pass AttributeSet as parameter in custom view constructor in AppWidget for android

I need to pass AttributeSet as a parameter in custom View constructor. Here is the attribute that I need to pass:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DonutChart">
<attr name="radiusDonut" format="dimension"/>
<attr name="donutTextSize" format="dimension"/>
</declare-styleable>
</resources>
I need to do this with code, in order that I will add my custom view to RemoteView in my AppWidget. Does anyone has any idea how can I achive this?
You should be able to use XmlPullParser to get the attributes from your XML resource and then pass those as follows, replacing res_id with the ID of the file above.
XmlResourceParser resourceParser = activity.getResources().getXml(R.xml.res_id);
AttributeSet attrs = Xml.asAttributeSet(resourceParser);
CustomView view = new CustomView(activity, attrs);

Can't use globally defined custom attribute

So I have defined a custom attribute:
<resources>
<attr name="filterColor" format="color"/>
</resources>
I did not declare it in an to reuse it in other custom views.
However, I am not able to use it like so
<SomeView
app:filterColor="#color/my_color"/>
Nor am I able to obtain it from AttributeSet
final TypedArray a = getContext().obtainStyledAttributes(attrs, new int[]{R.attr.filterColor});
It's driving me crazy - why are we allowed to declare global custom Attributes then if we can't use them?
If nothing else, your XML appears to be missing the <declare-styleable> element:
<resources>
<declare-styleable name="ColorMixer">
<attr name="color" format="color" />
</declare-styleable>
</resources>
(pulled from this library project)
The name in declare-styleable should be the class name of the view that will use the attribute (SomeView in your case).
This is covered in the documentation.

How to obtain styled attributes anywhere outside custom view?

Custom attributes are great, but all tutorials mention usage in custom views where you have AttributeSet parameter ready.
Contents of my attrs.xml:
<declare-styleable name="StyledDialogs">
<attr name="sdlDialogStyle" format="reference" />
</declare-styleable>
<declare-styleable name="DialogStyle">
...
</declare-styleable>
I'm struggling how to access those attributes in any class outside custom view.
After a few hours, I have figured out a way which works:
final TypedArray a = mContext.getTheme().obtainStyledAttributes(null, R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0);

Using the custom enum attributes values

In my application I'm using the following custom attribute for a custom view:
<attr name="direction">
<enum name="up" value="1" />
<enum name="down" value="2" />
</attr>
The thing is that in my custom view I must compare the current direction with the possible
ones. Is there a way to access the values for the up & down attributes?
I have stumbled upon this AttributeSet class . It should help you to retrieve data from compiled XML files.

Categories

Resources