Adding style for my custom widget in the themes.xml file

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Adding style for my custom widget in the themes.xml file

Postby silverburgh.meryl » Sat Feb 14, 2009 1:39 am

How to specify the style of my widget in themes.xml file?

In my themes.xml file, I have added:

<resources>
<style name="MyTheme">
<item name="buttonStyleMyWidget">@style/MyWidget</item>
</style>
</resources>

But that does not pick up the theme by default. I have to add this

<MyWidget style="@style/MyToggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

But if I just do this:

<MyWidget android:layout_width="wrap_content" android:layout_height="wrap_content"/>

I have added this in the manifest file:
<application android:label="@string/app_name" android:theme="@style/MyTheme">

My style does not get picked up.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Top

Re: Adding style for my custom widget in the themes.xml file

Postby MrSnowflake » Sun Feb 15, 2009 9:48 am

silverburgh.meryl wrote:I have added this in the manifest file:
<application android:label="@string/app_name" android:theme="@style/MyTheme">
i think your theme should derive from the default style, because if you only specify a style for your buttons, the rest of the app won't have a look. I think that's why Android ignores your style.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby silverburgh.meryl » Mon Feb 16, 2009 1:12 am

Thank you. Mr. Snowflake.

Okay, if my theme needs to be inherit from an default theme. But if I add the line '<item name="android:textColor">#808</item>', it does show the right color for the text. It is just that my custom widget does not show the right style.

<resources>
<style name="MyTheme">
<item name="android:textColor">#808</item>
<item name="buttonStyleMyWidget">@style/MyWidget</item>
</style>
</resources>

Can you please tell me how to define the default 'style' my widget ('buttonStyleMyWidget')?

My application shows what I want if I do this:
<com.mycompany.MyWidget android:id="@+id/my_widget" style="@style/MyWidget" />

But I want this to work (without putting 'style' attribute everytime I use my custom widget:
<com.mycompany.MyWidget android:id="@+id/my_widget" />

And I would like to know how to specified that in my theme.

I really appreciate if you can help me resolve this problem.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Postby pskink » Mon Feb 16, 2009 10:04 am

silverburgh.meryl wrote:
But I want this to work (without putting 'style' attribute everytime I use my custom widget:
<com.mycompany.MyWidget android:id="@+id/my_widget" />

And I would like to know how to specified that in my theme.

I really appreciate if you can help me resolve this problem.


you have to declare declare-styleable in your attrs.xml file and then in your widget ctor you will need to call obtainStyledAttributes()
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby pskink » Mon Feb 16, 2009 10:09 am

pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby silverburgh.meryl » Tue Feb 17, 2009 7:40 am

Thank you very much for your help.

I already have in my attrs.xml:

<declare-styleable name="MyWidget">
<attr name="textX" format="integer" min="0"/>

</declare-styleable>
</resources>

Is that sufficient? What else do I need to add?

But if I want 'this to work (without putting 'style' attribute everytime I use my custom widget:
<com.mycompany.MyWidget android:id="@+id/my_widget" /> to work,

why I need to call obtainStyledAttributes() in MyWidget constructor? Since if I don't want to specify any style attribute value in my layout xml, the obtainStyledAttributes() should return nothing, right?

And even if I need to call obtainStyledAttributes() in MyWidget constructor, what are the parameters that I should used?

Thanks in advance for any more pointers.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Top

Postby pskink » Tue Feb 17, 2009 10:58 am

silverburgh.meryl wrote:Thank you very much for your help.

I already have in my attrs.xml:

<declare-styleable name="MyWidget">
<attr name="textX" format="integer" min="0"/>

</declare-styleable>
</resources>

Is that sufficient? What else do I need to add?


ok, now in your MyWidget(Context context, AttributeSet attrs) constructor put the following:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyWidget);
int textX = a.getInteger(R.styleable.MyWidget_textX, -1);
if (textX == -1) {
// textX was not specified in layout xml
}
a.recycle();
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby silverburgh.meryl » Tue Feb 17, 2009 5:16 pm

Thank you pskink,

I get that part to work.

My problem is android did not use the drawables that I define unless I put this in my layout.xml file:

i.e. this will pick up my defined drawable:
<LinearLayout>
<com.mycompany.MyWidget style="@style/MyWidget" />
</LinearLayout>

this does not.

I want a way to do that so that I don't need to add '@style/MyWidget' everytime I uses com.mycompany.MyWidget

Thank you for any pointers.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Postby pskink » Tue Feb 17, 2009 6:12 pm

silverburgh.meryl wrote:
I want a way to do that so that I don't need to add '@style/MyWidget' everytime I uses com.mycompany.MyWidget


did you try to call super(context, attrs, R.style.MyWidget) in your widget construcor?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby silverburgh.meryl » Wed Feb 18, 2009 6:48 am

Thank you for your help.

Yes, I did

public MyWidget(Context context, AttributeSet attrSet) {
super(context, attrSet);
System.out.println ("133. calling (Context context, AttributeSet attrSet) n");

initToggleButton(context, attrSet, R.style.MyWidgetButton);
}

Strange thing is

this works:
<com.mycompany.MyWidget android:id="@+id/mywidget_button"
style="@style/MyWidget">

but this does not work:

<com.mycompany.MyWidget android:id="@+id/mywidget_button" >


And both cases call the same constructor. I have print statement, and I see the same constructor gets called in both cases.

Thank you for your help.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Postby pskink » Wed Feb 18, 2009 12:35 pm

silverburgh.meryl wrote:super(context, attrSet);



you need to call 3 params super()

see Button.java line ~65 How its done for std Button widget
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby silverburgh.meryl » Wed Feb 18, 2009 5:46 pm

Thank you for your help again.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Postby silverburgh.meryl » Wed Feb 18, 2009 5:58 pm

I did look at and follow that Button.java did. And follow to what they are doing there.

But My question now is why both

<com.mycompany.MyWidget android:id="@+id/mywidget_button"
style="@style/MyWidget">

<com.mycompany.MyWidget android:id="@+id/mywidget_button" >

call the same constructors:
public MyToggleWidget(Context context, AttributeSet attrSet) {
super(context, attrStet, R.attr.buttonStyleMyWidget);
}

Now, for both cases, they look the same. that is not what I want.
I would like that it has a 'default' style when I don't put 'style' attribute in the layout xml.
But then it should apply the style when there is one specified in the 'style' attribute.

Thank you in advance for more help.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Postby pskink » Wed Feb 18, 2009 8:18 pm

silverburgh.meryl wrote:Now, for both cases, they look the same.


you defined buttonStyleMyWidget in your own theme, right?

try to set this theme in your manifest.xml
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby silverburgh.meryl » Wed Feb 18, 2009 9:25 pm

Got it.

THANK YOU very much.
I really appreciate your help as I has been struggling all week on this.
silverburgh.meryl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sun Feb 08, 2009 11:17 pm

Top
Next

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 9 guests