android2014.06.26 13:42

The View Crawler

Layouts (and sub-layouts) in Android are tree hierarchies comprised of ViewGroups as composite elements and Views as leaf nodes. This tree can be crawled by visiting child views in your favorite order (breadth first or depth first). When crawling this tree, one can replace all the Typefaces on any TextViews, EditTexts, and Buttons that are encountered.

Here is a simple recursive implementation of a view crawler that will replace theTypeface for any appropriate view in the hierarchy:

public class FontChangeCrawler
{
    private Typeface typeface;

    public FontChangeCrawler(Typeface typeface)
    {
        this.typeface = typeface;
    }

    public FontChangeCrawler(AssetManager assets, String assetsFontFileName)
    {
        typeface = Typeface.createFromAsset(assets, assetsFontFileName);
    }

    public void replaceFonts(ViewGroup viewTree)
    {
        View child;
        for(int i = 0; i < viewTree.getChildCount(); ++i)
        {
            child = viewTree.getChildAt(i);
            if(child instanceof ViewGroup)
            {
                // recursive call
                replaceFonts((ViewGroup)child);
            }
            else if(child instanceof TextView)
            {
                // base case
                ((TextView) child).setTypeface(typeface);
            }
        }
    }
}

Replace Entire Activity's Typeface

To replace the default font in every view within an Activity's layout, simply use theFontChangeCrawler from above, like so:

@Override
public void setContentView(View view)
{
    super.setContentView(view);

    FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "font.otf");
    fontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content));
}

If you are not familiar with android.R.id.content, it is the official ID given to the rootView within an Activity's layout.

Consider placing the above logic in a BaseActivity class.

Replace Fragment's Typeface

You will need to apply the FontChangeCrawler to each Fragment as well. Consider placing this logic in a BaseFragment class.

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "font.otf");
    fontChanger.replaceFonts((ViewGroup) this.getView());
}

Handle Adapters, etc.

Replacing Activity fonts goes a long way, but most of us also have a plethora ofListViews. The list items in a ListView are built within an adapter, not within an Activity. Therefore, you will also need to use the FontChangeCrawler in your adapters:

...
if(convertView == null)
{
    convertView = inflater.inflate(R.layout.listitem, null);
    fontChanger.replaceFonts((ViewGroup)convertView);
}
...

What's Not Handled?

I will leave handling the ActionBar as an exercise for the reader. Also, consider how you might handle widgets whose typeface you don't want to change.

신고
Posted by 레드오이
android2014.06.26 11:24

HzGrapher

android hz graph library

Function

  • Line Graph Screenshot

  • Line Graph with Region Screenshot


https://github.com/handstudio/HzGrapher

신고
Posted by 레드오이
android2014.06.26 10:56
25 October
8

Pull To Refresh

By Chris Banes

This project aims to provide a reusable Pull to Refresh widget for Android. It was originally based on Johan Nilsson’s library (mainly for graphics, strings and animations), but these have been replaced since.

Features

  • Supports both Pulling Down from the top, and Pulling Up from the bottom (or even both).
  • Animated Scrolling for all devices.
  • Over Scroll supports for devices on Android v2.3+.
  • Currently works with ListViewExpandableListView & GridViewWebView and ScrollView!
  • Integrated End of List Listener for use of detecting when the user has scrolled to the bottom.
  • Maven Support.
  • Indicators to show the user when a Pull-to-Refresh is available.
  • Support for ListFragment!
  • Lots of Customisation options!

https://github.com/chrisbanes/Android-PullToRefresh

신고
Posted by 레드오이

티스토리 툴바