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

How to use a different Typeface? #17

Closed
4goodapp opened this issue Oct 20, 2016 · 19 comments
Closed

How to use a different Typeface? #17

4goodapp opened this issue Oct 20, 2016 · 19 comments

Comments

@4goodapp
Copy link

4goodapp commented Oct 20, 2016

I to change the default typeface/font for NumberPickerView?
I want use a custom font.

@Carbs0126
Copy link
Owner

sorry i have not add customizing font function, you can custom font in NumberPickerView by using code:

mPaintText.setTypeFace(...);

when mPaintText is initialized.

i will add this function later

@4goodapp
Copy link
Author

4goodapp commented Oct 22, 2016

Thanks, for fast answer.
I try to edit the file within Android Studio but I can't edit the file. It's locked :(
Any Idea about this?

@4goodapp
Copy link
Author

After some search, I manage to import the project as module and was able to edit the file but what I should put in mPaintText.setTypeFace(...); ?
I tried those but didn't work:

mPaintText.setTypeface(Typeface.create("2Toon", Typeface.BOLD)); mPaintText.Typeface.createFromAsset(getContext().getAssets(), "2Toon.ttf"); mPaintText.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/2Toon"));

@Carbs0126
Copy link
Owner

sorry for not replying in time, you can add a method

public void setContentTextTypeface(Typeface typeface){
        mPaintText.setTypeface(typeface);
    }

    public void setHintTextTypeface(Typeface typeface){
        mPaintHint.setTypeface(typeface);
    }

and invoke this method outside in oncreate method , if you want to change typeface dynamicly, using mNumberPickerView.invalidate( ) after using this method

@4goodapp
Copy link
Author

No Problem :)
I solved the problem by adding:
mPaintText.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/MyFont.ttf"));
It worked for me. I don't know if it's a good practice to do it in that way though.

@Carbs0126
Copy link
Owner

Carbs0126 commented Oct 28, 2016

if you have used MyFont.ttf, you can use a static value to refer to this MyFont.ttf, in this way you can avoid memory leak, because every time you use

Typeface.createFromAsset(context.getAssets(),"fonts/MyFont.ttf")

MyFont.ttf will be created again and added into memory.

@4goodapp
Copy link
Author

I don't understand what you mean by "...use a static value to refer to this MyFont.ttf".
How can I use a static value to refer to the custom font I want use?

@Carbs0126
Copy link
Owner

i mean you can use cache to refer to MyFont.ttf, in this way, if you use MyFont.ttf again, you can get MyFont.ttf from cache,
just like

public static Typeface cache = Typeface.createFromAsset(context.getAssets(),"fonts/MyFont.ttf")

mPaintText.setTypeface(cache);

this will avoid memory leak

@4goodapp
Copy link
Author

Ah okay, I got it now. I'll change it.
But compared to [your solution], what are the differences/advantages?(#17 (comment))

@Carbs0126
Copy link
Owner

if there are many numberpickerviews in you app( or other views which use MyFont.ttf), there will be many copy of MyFont.ttf, but in fact only one MyFont.ttf is enough

@4goodapp
Copy link
Author

Cool, I got it :) Thank you very much.

Off Topic:
Is their a way to display the selected numbers while scrolling?
I tried to put it in the the onScrollStateChange methode but it randomly crash my app.

Like in this gif:
http://i.imgur.com/d3ZMjss.gif

@Carbs0126
Copy link
Owner

i have not add this function, if you need this function now, you can add some code into

calculateFirstItemParameterByGlobalY()

and get the index of current selected item by mCurrDrawFirstItemIndex and mCurrDrawFirstItemY, and set this index value to another value such as 'currSelected', and use a 'prevSelected' to store the last selected one in scrolling, if currSelected != prevSelected, then number changed. i will add this function later :)

@Carbs0126
Copy link
Owner

i have added a method

    picker.setOnValueChangeListenerInScrolling(...);

you can get changed value while in scrolling

and i have also added two methods for changing typeface of text/hint in NumberPickerView,
just update dependence version of NumberPickerView to 1.1.1

@4goodapp
Copy link
Author

4goodapp commented Oct 28, 2016

Oh, Great,
I just try it , it's working but the scrolling is not smooth. It lags a lot.

`

@Override

public void onValueChangeInScrolling(NumberPickerView picker, int oldVal, int newVal) {
    myChosedNumber.setText(picker.getContentByCurrValue());
 }

`

Is there a better way to make it as smooth as in the gif

@Carbs0126
Copy link
Owner

this callback will be invoked once as number value changed for 'one item', and this callback runs in main thread , if you want to do some complicated work in this callback such as play sound, it may make scrolling lag. To make scrolling smoothly you can use handler to send message in this callback ,or make callback tasks run in a new thread, if it still lag, you can send your code to me ,and maybe i can refine this project according to your code

@4goodapp
Copy link
Author

Hi, I sent just you the project via email,
I've tried to put the code in a Runnable as well in a AsyncTask but it was still laggy.

I planned to add a scrolling sound but It's not much of a big deal if it may create (lag) problems...

If you need any info, let me know.

@4goodapp
Copy link
Author

4goodapp commented Oct 30, 2016

And by the way, I've try the project in that gif on my phone and it was working smoothly, just like in the gif.

I found that project from here, Just in case it may help.

@tinsukE
Copy link

tinsukE commented Feb 28, 2017

@Carbs0126 Can you update the version in the README file to 1.1.1?

I was trying to branch the repo to add the Typeface functionality, but saw that it was already implemented in this version (which is not refereed to anywhere).

@Carbs0126 Carbs0126 mentioned this issue Mar 13, 2017
@rohankandwal
Copy link

I have created a fork of the project which allows custom font loading from xml itself. I will be glad if you can take a look at it.
https://github.com/rohankandwal/NumberPickerView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants