2

I've got a custom component that I'm instantiating in a loop, and they all need to have the same style applied. As it's a custom component (specifically a subclass of LinearLayout), I can't just set the style attribute in the XML, because the root tag of the component's layout XML is a merge (to save making the view hierarchy too crazy). I found this:

How can I create an AttributeSet from a style.xml?

Which describes my problem pretty much exactly. Only the answers are useless - my R.java is fresh, and the style is there. I think the problem is that getResources().getXml(R.style.my_style) doesn't actually look in the /res/values/styles.xml file?

I'm using Scala, so the syntax is a little...nicer. My component:

class MyLinearLayout(object : MyDatabaseObject, context : Context, attrs : AttributeSet = null) 
        extends LinearLayout(context, attrs) {
    // Shows and interacts with the data in object
}

In the Activity:

val componentStyle = Xml.asAttributeSet(getResources.getXml(R.style.my_style))
for (obj <- dbResults) viewGroup.addView(new MyLinearLayout(obj, this, componentStyle))

And the first line of that throws a Resources.NotFoundException. The style itself is in res/values/styles.xml, along with the rest of them, and gets applied to LinearLayouts in different activities, so that's not the problem.

Community
  • 1
  • 1
Isaac
  • 73
  • 1
  • 8
  • Share some code from your custom component. I think I have done something similar, but I'd like to see what your custom component looks like, and how you are instantiating it. I assume it isn't as simple as declaring a new view with reference to a style like so: Button newButton = new Button(context, attrs, R.style.BigPurpleButton); – Daniel Smith Aug 28 '12 at 19:35
  • Setting the third parameter on the constructor hasn't worked before, so I was hoping to use AttributeSet. If I'm meant to be using the resource ID, what the hell is the AttributeSet for? – Isaac Aug 28 '12 at 23:18
  • Before I take a stab at your particular situation, have you glanced at the following answer? Pretty awesome example of custom attributes, provided that is what you're looking for... http://stackoverflow.com/questions/4495511/how-to-pass-custom-component-parameters-in-java-and-xml/4495745#4495745 – Daniel Smith Aug 28 '12 at 23:33
  • I don't want to add any custom attributes, just apply an XML-defined style to the component so I can control all (or pretty much all) the appearance from the code. I want to replace: `this.setPadding(w,x,y,z); this.setBackgroundColor(...);` etc. in the constructor with a call to R.style.my_style in the activity that constructs the component. – Isaac Aug 29 '12 at 09:23

1 Answers1

2

Unfortunately, there is no way to set the style of a view programmatically with an R.style.your_style reference. There are a number of SO posts confirming this, but there is a workaround of sorts by creating an xml layout or drawable file with the style applied. I figure this could work in your situation because all of your components are being created in the same way with the same desired attributes (lets say its in drawable/custom_view:

<?xml version="1.0" encoding="utf-8"?>
<com.your.CustomView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="pick_your_width"
   android:layout_height="pick_your_height"
   style="@style/your_style" />

and then when you are adding the custom views use:

CustomView newView = (CustomView)getLayoutInflater().inflate(R.drawable.custom_view, null);

Sorry this isn't perfect for your situation, but it is the best solution I have found for applying one style to a custom view methodically without messing up your java. Based on a solution found here.

Community
  • 1
  • 1
Daniel Smith
  • 8,262
  • 3
  • 31
  • 56