Categories
discuss

passing android:fontFamily to custom view in Android

How do you pass a font family inside the res/font folder, e.g. @font/roboto_medium, as an attribute to a custom view in Android in XML, and then read it inside the custom view into a Typeface object? This is necessary to do custom graphical rendering of the text.

For example:

<MyCustomView android:fontFamily="@font/roboto_medium"/>

Then inside MyCustomView.kt:

override fun onCreateView(...) {

  // parse android:fontFamily attribute into Typeface object
  val typeface: Typeface = ???  

}

None of the Typeface functions seems to support this. One of them accepts a custom font inside the assets folder, and another accepts a resource font integer directly embedded inside the Java/Kotlin code e.g. R.font.roboto_medium.

Answer

The answer of doohickey_maker really works.

In layout.xml you need to add android:fontFamily="@font/name"

In attrs.xml

<declare-styleable name="LoadingButton">
<attr name="android:fontFamily" />

In CustomView

   int fontFamilyId = typedArray.getResourceId(R.styleable.LoadingButton_android_fontFamily, 0);
   if (fontFamilyId > 0) {
       mButton.setTypeface(ResourcesCompat.getFont(getContext(), fontFamilyId));
   }
Categories
discuss

Jest – check that item does not contain object with some property

This is my response I need to check:

[
  { _id: 'dislike', count: 1 },
  { _id: 'hate', count: 1 },
  { _id: 'trivial', count: 1 }
]

Checking the existing values is easy:

expect(response.data).toContainEqual({ _id: 'trivial', count: 1 });
expect(response.data).toContainEqual({ _id: 'dislike', count: 1 });
expect(response.data).toContainEqual({ _id: 'hate', count: 1 });

But I need to check that the array does not contain object with { _id: ‘neutral’ }. Following test looked successful because the test passed:

expect(response.data).not.toContain({ _id: 'neutral', count: 0 });

But I wanted to ensure that it does what I expect so I added a test that shall fail. But it does not:

expect(response.data).not.toContain({ _id: 'hate', count: 1 });

Can you see? Both tests toContainEqual and not.toContain are valid. How can I ensure that the array does not hold object with a property _id: 'neutral'?

Answer

You could do it with Array.prototype.some()

expect(response.data.some(({_id}) => _id === "neutral")).toBe(false)

Array.prototype.some() returns a boolean true when the condition is first met, and false if not.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Categories
discuss

Materia-ui and typescript No overload matches this call

I’m converting a js component that uses Material-ui to typescript and I ran into a problem. This part was rendering a tile-like image in which the component prop was overridden plus additional to prop to make a proper clickable link.

The error I get is : TS2769: No overload matches this call.

Here is the code I’m using:

Importing statement:

import GridListTile from '@material-ui/core/GridListTile';
import { Link } from 'react-router-dom';

And in render function I have:

<GridListTile
  component={Link} <<-- here I get the error
  to={'/some-address'}
>
  // other components
</GridListTile>

I can silence the error by passing as any, but then it complains about to prop!

<GridListTile
  component={Link as any} <<-- won't complain
  to={'/some-address'} <<-- but now here I get the error
>
  // other components
</GridListTile>

I want to be able to use Material-ui components and be able to pass another component and the props I want. I came across some solutions which were hacks, what is the correct way to handle this situation?

Answer

It was a bug in material-ui that was fixed in this commit, and I’ve checked the latest version (v4.9.13) and it was okay.

So upgrading to the latest version will fix this issue.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..