Java Question How do get string/value from a "styleable"?

DonManfred

Expert
Licensed User
Longtime User
for some things i got it translated from
B4X:
mProgressDrawable = (GradientDrawable) getDrawable(R.drawable.rect_progress).mutate();
to
B4X:
        int resID = getResources().getIdentifier("rect_progress", "drawable", mpkg);
        mProgressDrawable = (GradientDrawable) getDrawable(resID).mutate();
to be used in a wrapper...

But actually i want to translate this from using R to values set in the wrapper
B4X:
 private void initAttributes(Context context, AttributeSet attributeSet) {
TypedArray attr = getTypedArray(context, attributeSet, R.styleable.ProcessButton);

The styles.xml will be in the res-folder of the b4a project and will have content
<resources>
<declare-styleable name="ProcessButton">
<attr name="pb_textProgress" format="string" />
<attr name="pb_textComplete" format="string" />
<attr name="pb_textError" format="string" />
<attr name="pb_colorProgress" format="color" />
<attr name="pb_colorComplete" format="color" />
<attr name="pb_colorError" format="color" />
</declare-styleable>

<declare-styleable name="FlatButton">
<attr name="pb_colorPressed" format="color" />
<attr name="pb_colorNormal" format="color" />
<attr name="pb_cornerRadius" format="dimension" />
</declare-styleable>
</resources>

What do i need to do in java to load such an xml in the library (Eclipse; no res folder exists. Even R is not available.)? Or how do i create a
B4X:
TypedArray attr
by code?
 

DonManfred

Expert
Licensed User
Longtime User
In fact this is the code from the java-source i want to wrap
B4X:
private void initAttributes(Context context, AttributeSet attributeSet) {
TypedArray attr = getTypedArray(context, attributeSet, R.styleable.ProcessButton);
if (attr == null) {
return;
}
try {
mLoadingText = attr.getString(R.styleable.ProcessButton_pb_textProgress);
[...many more lines like this...]

I want to have a pendant to set the needed attr-values by code to realize attr will have the right content when using
B4X:
attr.getString(R.styleable.ProcessButton_pb_textProgress)
The problem is that R.styleable is not available in the wrapper class and i dont know how i can avoid using R in this case
 

warwound

Expert
Licensed User
Longtime User
Try a syntax like this:

B4X:
int resID = getResources().getIdentifier("ProcessButton", "styleable", mpkg);
TypedArray attr = getTypedArray(context, attributeSet, resId);
 

DonManfred

Expert
Licensed User
Longtime User
Try a syntax like this:
Does not work :(. I already tried that.
When i use
B4X:
int resID = getResources().getIdentifier("ProcessButton", "styleable", mpkg)
then the line
B4X:
TypedArray attr = getTypedArray(context, attributeSet, resStyleID);
will result in

typearray0036.png



If i then change the int to int[] then the first line give errors again cause it only returns a int not a set of ints...

That´s why i asked how to create this by code to avoid this
 

tchart

Well-Known Member
Licensed User
Longtime User
BTW warwounds suggestion works. Add this code to your R.java file;

B4X:
  private static final int[] getResourceDeclareStyleableIntArray(String name)
{
    try
    {
        //use reflection to access the resource class
        Field[] fields2 = Class.forName(BA.packageName + ".R$styleable" ).getFields();

        //browse all fields
        for ( Field f : fields2 )
        {
            //pick matching field
            if ( f.getName().equals( name ) )
            {
                //return as int array
                int[] ret = (int[])f.get( null );
                return ret;
            }
        }
    }
    catch ( Throwable t )
    {
    }

    return null;
}

And you can then access the StyleableIntArray this way;

public static int[] MDRootLayout = getResourceDeclareStyleableIntArray("MDRootLayout");
 
Top