Ok

In order to test about "Custom Attributes" I've made a class which
extends Button

and I made attrs.xml in values folder

This is attrs.xml  code===========================
<?xml version="1.0" encoding="utf-8"?>
<resources>
        <declare-styleable name="MyView">
                <attr name = "textSet" format="string"/>
                <attr name = "textSetClicked" format="string"/>
    </declare-styleable>
</resources>
===========================================
This is man.xml which is going to use for layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        xmlns:app="http://schemas.android.com/apk/res/
com.android.demo.customattribute"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.android.demo.customattribute.MyView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:textSet="@string/t1"
    app:textSetClicked="@string/t2"
    android:id="@+id/myButton"/>
</LinearLayout>
===========================================

As you can see,I defined textSet and textSetClicked then In
strings.xml I defined "string value t1 and t2"

in the Constructor of View class  I made this code.
============================================
private void init(Context context,AttributeSet attrs){
                TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
                CharSequence s = a.getString(R.styleable.MyView_textSet);

                  if(s != null){
                        setText(s.toString());
                }

        }
============================================

So that when I run application I can see a button with Text
It works !! But, I want to change the text of button so I override
OnDraw() method

===========================================
protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Log.i("MESSAGE" , "On DraW");
                if(isPressed())
                {
                        this.changeText();
                }
        }
==========================================

changeText() method is like that

=========================================
TypedArray a = this.mContext.obtainStyledAttributes(mAttrs,
R.styleable.MyView);

                CharSequence s = a.getString(R.styleable.MyView_textSetClicked);

                if(s != null){
                        Log.i("MESSAGE" , "SET TEXT!!!!!!!!!!!!");
                        setText(s.toString());

=========================================

mContext and mAttrs have already got values cos I put in Constructor
//Constructor
public MyView(Context context, AttributeSet attrs) {
 mContext = context;
 mAttrs = attrs;


But I can't change text cos I can't get custom attributes  I don't
know why..


If it is not enough to understand what I wrote please ask me..



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to