0

I have a custom attribute in a subview class defined in XML file:

    <com.mysubView>
    android:id="@+id/board"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    cus:Count="0"
    />

I would like to be able to access it main_activity.java to set a value to Count like this:

findViewById(R.id.board).setCount(10);

Is it possible?

Note: attrs.xml is already populated with corresponding stylable.

Here is a related article on custom attributes Here is constructor to the sub-view class:

public MySubView (Context c, AttributeSet attrs) {
    super(c, attrs);
}

1 Answer 1

1

You can change the value of count. Of course you need a method with signature setCount(int), defined inside mysubView, and you will have to cast the return result of findViewById to mysubView

((mysubView)findViewById(R.id.board)).setCount(10);

this way you will able to change any specific property/behaviour you defined in mysubView

Sign up to request clarification or add additional context in comments.

6 Comments

Can you please elaborate? What would the method implementation would look like? which variable should I be accessing? for example: do I just get the int hand-off directly to the method with the signature? where do I assign it to?
did your write the View's subclass?
yeah. tested and working. I am adding the void setCount(int test) to it now. I dunno what to assign it to. Count is not available.
can you post the the constructor of your CustomView ?
public MySubView (Context c, AttributeSet attrs) { super(c, attrs); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.