How to optimize custom view?

So I defined an custom view based on LinearLayout:

    public class AlphabetButton extends LinearLayout{

        private Button alphabetButton;
        private ImageView usersMark;
        public AlphabetButton(Context context) {
        private void load(Context context){

            LayoutInflater.from(context).inflate(R.layout.alphabet_button, this, true);

            alphabetButton = (Button)findViewById(;
            usersMark = (ImageView)findViewById(;


            final Typeface chalkFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");


since app creates about 30 pcs of this view it has a noticeable lag (on weak CPU smartphones it takes about 2-3s) . I also noticed a log output (not mine) like:

05-01 16:47:22.224: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.234: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.254: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.264: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.334: D/szipinf(10569): Initializing inflate state

and there are about 30 same lines. So I assume that load(Context context) method and especially inflating process is the resource of lags. But how to avoid it? How to optimize this view to instantiate it faster?


I don't think you can inflate a View and then cache it for future use (i.e. the second time you enter the constructor), because you will get errors like "view already has a parent". However, I don't know if the same holds for the Typeface you inflate.. maybe that one IS reusable.

Furthermore: I don't know if your R.layout.alphabet_button contains a lot if images? If that is the case you may want to get rid of a few of those, in favor of xml-generated graphics, like using xml shapes/gradients etc. In my experience, those load a LOT faster. If you cannot get rid of your images, you may at least be able to load those once and then copy them in-memory instead of inflating them each time (so don't put them in your layout.xml, but set them on your ImageView afterwards with methods like setImageBitmap() or setImageDrawable().

As always with these kind of caching tricks: be sure to check your app for memory leaks ;-)

Need Your Help

UIWebView shouldStartLoadWithRequest not called in iOS 4.3

javascript ios cocoa-touch uiwebview uiwebviewdelegate

UIWebView's shouldStartLoadWithRequest is fired by javascript with

How to pass back actionlink values - mvc3 c#-4.0

I have following code in HomeController; it gets latest date and shows songs according to that date.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.