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

Discussion in 'Libraries developers questions' started by DonManfred, Feb 8, 2015.

  1. DonManfred

    DonManfred Expert Licensed User

    for some things i got it translated from
    Code:
    mProgressDrawable = (GradientDrawable) getDrawable(R.drawable.rect_progress).mutate();
    to
    Code:
    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
    Code:
    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
    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
    Code:
    TypedArray attr
    by code?
     
  2. DonManfred

    DonManfred Expert Licensed User

    In fact this is the code from the java-source i want to wrap
    Code:
    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
    Code:
    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
     
  3. DonManfred

    DonManfred Expert Licensed User

    I need this actually to get progress at the wrapper

    Result will be a wrapper realizing this (to give a preview)

    [​IMG]

    or

    [​IMG]
     
  4. warwound

    warwound Expert Licensed User

    Try a syntax like this:

    Code:
    int resID = getResources().getIdentifier("ProcessButton""styleable", mpkg);
    TypedArray attr = getTypedArray(context, attributeSet, resId);
     
    DonManfred likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    You will need to create an Android library project if you want to use resource files and then use #AdditionalRes to reference the library resource files.

    If you can implement it without resource files then it will be simpler to use.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Loading it dynamically as warwound wrote should work. It is a good solution.
     
  7. DonManfred

    DonManfred Expert Licensed User

    Does not work :(. I already tried that.
    When i use
    Code:
    int resID = getResources().getIdentifier("ProcessButton""styleable", mpkg)
    then the line
    Code:
    TypedArray attr = getTypedArray(context, attributeSet, resStyleID);
    will result in

    [​IMG]


    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
     
  8. warwound

    warwound Expert Licensed User

  9. tchart

    tchart Active Member Licensed User

    BTW warwounds suggestion works. Add this code to your R.java file;

    Code:
    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");
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice