Wrox Programmer Forums
|
BOOK: Beginning Android 4 Application Development
This is the forum to discuss the Wrox book Beginning Android 4 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-9954-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2012, 05:51 AM
Registered User
 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question Help understanding declare-styleable

Hi,
I'm reading the 5th chapter of your book and I'm stuck where you use the declare-styleable XML attribute for the Gallery background... I'm not able to understand how it works and also the java code related to the styleable makes me quite confuse.. I've tried to find some info on the official docs but I could not find anything detailed about styleables. The code is the following:
Code:
<?xml version=”1.0” encoding=”utf-8”?> 
<resources>
    <declare-styleable name=”Gallery1”>
        <attr name=”android:galleryItemBackground” />
    </declare-styleable>
</resources>
Code:
public class GalleryActivity extends Activity {
[...]
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this)); 
        [...]
    }

    [...]

    public class ImageAdapter extends BaseAdapter {
        [...]
        int itemBackground;

        public ImageAdapter(Context c) {
            context = c;
            //---setting the style---
            TypedArray a = obtainStyledAttributes(
            R.styleable.Gallery1); 
            itemBackground = a.getResourceId(
                        R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            [...]
            imageView.setBackgroundResource(itemBackground);
            return imageView; 
        }
    }
}
Can you help me to understand why we need to use a styleable and how it works? Thank you very much!





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to declare variables rehana Beginning PHP 11 April 30th, 2013 04:59 AM
Must declare the scalar variable stapes ASP.NET 2.0 Basics 1 August 15th, 2008 10:03 AM
Namespace - how to declare? JillB Excel VBA 0 January 25th, 2008 07:11 PM
declare My ajkumar Visual Basic 2005 Basics 0 April 18th, 2007 05:24 AM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 10:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.