Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't apply defStyleAttr to PlayerView #9024

Closed
JuzTosS opened this issue Jun 7, 2021 · 5 comments
Closed

Can't apply defStyleAttr to PlayerView #9024

JuzTosS opened this issue Jun 7, 2021 · 5 comments
Assignees
Labels

Comments

@JuzTosS
Copy link

JuzTosS commented Jun 7, 2021

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlayerView, 0, 0);

defStyleAttr is not passed to obtainStyledAttributes and a default style can't be applied to PlayerView.

@claincly
Copy link
Contributor

claincly commented Jun 8, 2021

Hi! Please provide complete information as requested in the issue template. The issue template can
be found here.
If you're unable to share bug reports or
test content publicly, please send them to [email protected] using a subject in the format
"Issue #1234" ("#1234" is replaced with your issue number). Please also update this issue to
indicate you've done this.

@claincly claincly self-assigned this Jun 8, 2021
@JuzTosS
Copy link
Author

JuzTosS commented Jun 8, 2021

If you try to call the constructor with default style PlayerView(context, attrs, defStyleAttr) it won't be applied as it's not passed further when retrieving values.

I personally need it for the case to extend PlayerView and have a view that sets TextureView by default instead of SurfaceView. To solve this I can pass a style defStyleAttr with texture view as surface_type=2

So the fixed line will look like this:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlayerView, defStyleAttr, 0);

There's also one missed override with defStyleRes
public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {

That also should be used with obtainStyledAttributes, we again should pass defStyleRes instead of 0 to the last parameter.

So the fixed line will look like this:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlayerView, defStyleAttr, defStyleRes);

@claincly claincly assigned ojw28 and unassigned claincly Jun 8, 2021
@ojw28 ojw28 added the bug label Sep 6, 2021
@ojw28
Copy link
Contributor

ojw28 commented Sep 6, 2021

There's also one missed override with defStyleRes

It actually looks pretty awkward to support this until our minimum API version has increased to 21. Until then, it's not possible for us to do:

public PlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  this(context, attrs, defStyleAttr, 0);
}

public PlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
  // Other initialization
}

because the 4-arg constructor in the super class only exists on API level 21 and higher. I think the implication is that we'd have to either (a) duplicate a lot of initialization code in the 3-arg and 4-arg constructors, or (b) call an initialization method from both constructors and make all of the member variables that are initialized at construction time non-final. Both options seem not that great to me. We could alternatively call 3-arg super class method from our 4-arg method, but that's pretty confusing (and technically wrong, since FrameLayout does make use of the defStyleRes argument).

Do you need the 4-arg constructor, or were you just noting that for completeness? If not, I think we should just fix the problem where defStyleAttr is not being used, and stop there.

icbaker pushed a commit that referenced this issue Sep 7, 2021
#minor-release
#exofixit
Issue #9024

PiperOrigin-RevId: 395224661
@ojw28
Copy link
Contributor

ojw28 commented Sep 7, 2021

The change referenced above makes use of defStyleAttr. Closing on the assumption that the 4-arg constructor isn't important, but please let us know by replying here if it is.

@ojw28 ojw28 closed this as completed Sep 7, 2021
@JuzTosS
Copy link
Author

JuzTosS commented Sep 8, 2021

The case we faced is having your own inheritor of PlayerView with predefined parameters (surface type in our case).

We solved it with composition, but this has a drawback of adding additional layer to the hierarchy.
(OurPlayerClass extends FrameLayout and inflates xml with PlayerView in it)

christosts pushed a commit that referenced this issue Sep 21, 2021
@google google locked and limited conversation to collaborators Nov 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants