I am trying to reference a control using XML.
To declare an attribute for an id link from MyTextView:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyTextView"> <attr name="valueTextViewId" format="reference" /> </declare-styleable> </resources>
fragment_example.xml - How to use a custom attribute:
<com.example.MyTextView android:id="@+id/foo" example:valueTextViewId="@id/bar" ... /> <com.example.MyTextView android:id="@+id/bar" />
MyFragment.java - Overlay controls
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyTextView class constructor - during inflation, do something with a link to a text image:
public TextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.MyTextView); int refId = a.getResourceId(R.styleable.MyTextView_valueTextViewId); // Updated to use context if (refId > -1 && context instanceof Activity) { Activity a = (Activity)context; View v = a.findViewById(refId); // THE PROBLEM: v is null if (v != null) { // In my case, I want to check if the "Value" textview // is empty. If so I will set "this" textColor to gray } } }
in this example, v always null . I assume that controls are not yet added during the Inflation layout. Another thing to note is that this is in Fragment , so this may be the reason that I cannot find a representation in the parent activity.
Is it possible to associate a control with another in this way?
source share