Java Question AttributeSet in library

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi,

i'm wondering how i could enter the AttributeSet in the constructor of a class.

B4X:
 public MySwitch(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        Resources res = getResources();
        mTextPaint.density = res.getDisplayMetrics().density;
        mTextPaint.setShadowLayer(0.5f, 1.0f, 1.0f, Color.BLACK);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MySwitch, defStyle, 0);

        mThumbDrawable = a.getDrawable(R.styleable.MySwitch_thumb);
        mTrackDrawable = a.getDrawable(R.styleable.MySwitch_track);
        mTextOn = a.getText(R.styleable.MySwitch_textOn);
        mTextOff = a.getText(R.styleable.MySwitch_textOff);
        mTextOutsideTrack = a.getBoolean(R.styleable.MySwitch_textOutsideTrack, false);
        mTextOnThumb = a.getBoolean(R.styleable.MySwitch_textOnThumb, false);
        mThumbTextPadding = a.getDimensionPixelSize( R.styleable.MySwitch_thumbTextPadding, 0);
        mTrackTextPadding = a.getDimensionPixelSize( R.styleable.MySwitch_trackTextPadding, 0);
        mSwitchMinWidth = a.getDimensionPixelSize( R.styleable.MySwitch_switchMinWidth, 0);
        mSwitchMinHeight = a.getDimensionPixelSize( R.styleable.MySwitch_switchMinHeight, 0);
        mSwitchPadding =  a.getDimensionPixelSize( R.styleable.MySwitch_switchPadding, 0);

        mTrackDrawable.getPadding(mTrackPaddingRect) ;
        //Log.d(TAG, "mTrackPaddingRect=" + mTrackPaddingRect);
        mThumbDrawable.getPadding(mThumbPaddingRect);
        //Log.d(TAG, "mThumbPaddingRect=" + mTrackPaddingRect);
        
        
        int appearance = a.getResourceId(R.styleable.MySwitch_switchTextAppearanceAttrib, 0);
        if (appearance != 0) {
            setSwitchTextAppearance(context, appearance);
        }
        a.recycle();

        ViewConfiguration config = ViewConfiguration.get(context);
        mTouchSlop = config.getScaledTouchSlop();
        mMinFlingVelocity = config.getScaledMinimumFlingVelocity();

        // Refresh display with current params
        refreshDrawableState();
        setChecked(isChecked());
        this.setClickable(true);
        //this.setOnClickListener(clickListener);
    }

For example, i need to give in the AttributeSet attrs to load my attributes, but I don't know how this can be achieved in Basic4Android.

More like this:
B4X:
 TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MySwitch, defStyle, 0);

        mThumbDrawable = a.getDrawable(R.styleable.MySwitch_thumb);
        mTrackDrawable = a.getDrawable(R.styleable.MySwitch_track);

Regards,
Tomas

EDIT: Ugh sorry, this should be in the developer questions thread...
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
I understand that.
I just cannot seem to find how i can pass the attributeset through the initialize metod

Verstuurd van mijn R800i met Tapatalk


HTMLParser
Author: XverhelstX
Version: 1
  • HTMLParser
    Events:
    • Parsed (List1 as list As )
    Fields:
    • content As Element
    • doc As Document
    • img As Elements
    • links As Elements
    • title As String
    Methods:
    • attr (imga As Elements, attr As String) As String
    • findIntegerInString (findInt As String) As String
      Finds an integer in a string.
      findInt:
      Return type: @return:
    • getElementById (Id As String)
      Find an element by ID, including or under this element.
      Id:
    • getElementsByAttribute (key As String)

      key:
    • getElementsByClass (className As String)

      className:
    • getElementsByClass2 (className As String)
      Find elements by class, including or under this element.
      Id:
    • getElementsByTag (Tag As String)
      Finds elements, including and recursively under this element, with the specified tag name.
      Tag:
    • initialize (EventName As String)
      Initializes the HTMLParserLibrary.
      EventName - Events subs prefix.
    • loadHTML (Dir As String, fileName As String, Encoding As String)
      Loads an internal html file.
      Dir:
      fileName:
      Encoding:
    • loadHTML2 (html As String)
      Grabs the html content of a string.
      URL:
    • loadURL (URL As String)
      Grabs the html content of a website and makes it available.
      URL:
    • parse (Attribute As String, TaskID As Int)

      Attribute:
    • parse2 (Attribute As String, Attribute2 As String, TaskID As Int)

      Attribute:
    • parse3 (Attribute As String, Attribute2 As String, Attribute3 As String, TaskID As Int)

      Attribute:
    • reverseString (reversingString As String) As String
      Reverse a String.
      reversingString:
      Return type: @return:
    • select (jsDoc As Document, Select As String)
    Permissions:
    • android.permission.WRITE_EXTERNAL_STORAGE
    Properties:
    • Text As String [read only]
 
Last edited:

tuhatinhvn

Active Member
Licensed User
Longtime User
Current i made a library with declare-styleable view but when initialize metod i can not pass attributeset, any one help us?
 
Top