-1

I have defined a custom widget (MyWidget)

  • res/layout/mywidget.xml

  • res/values/attrs.xml

code

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="my_widget">
        <attr name="maxValueFromTextId" format="reference"/>
    </declare-styleable>
</resources>
  • MyWidget.java

into I would like retrieve the reference object describe by maxValueFromTextId. This object to me is a TextView which are not into this widget

<RelativeLayout
  <TextView
   android:id="@+id/text1"
 />
  <Mywidget
    maxValueFromTextId="@id/text1"
  />
/>

So what to do in MyWidget.java to get TextView reference object ?

I read this useful article: Defining custom attrs

Thanks

Community
  • 1
  • 1
bioinfornatics
  • 1,749
  • 3
  • 17
  • 36
  • 1
    It says what yYou need to in the post you linked. Include the attributes in your xml by adding xmlns:myApp="http://schemas.android.com/apk/res-auto" to the first tag in your layout. Then you can add myApp:myattr to any tag in there. The important thing is the attrs.xml and that xmlns line in your layout – Gabe Sechan Jul 21 '14 at 21:56
  • Thanks Gabe for your answer. Like you said the given link is really interesting. But I so not see where it use a reference format to take back a View object maybe I miss it. – bioinfornatics Jul 21 '14 at 22:23
  • 1
    I think you're really confused and not describing what you want very well. – Gabe Sechan Jul 21 '14 at 22:26

1 Answers1

1

Define additional attributes for your custom Views

see more here

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ColorOptionsView">
        <attr name="titleText" format="string" localization="suggested" />
        <attr name="valueColor" format="color" />
    </declare-styleable>

</resources> 

To use these attributes in your layout file you have to declare them in the XML header. In the following listing this is done via the xmlns:custom part of the code. These attributes are also assigned to the view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
<!-- define new name space for your attributes -->
    xmlns:custom="http://schemas.android.com/apk/res/com.vogella.android.view.compoundview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
<!-- Assume that this is your new component. It uses your new attributes -->
        <com.vogella.android.view.compoundview.ColorOptionsView
            android:id="@+id/main_color_options"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            custom:titleText="Background color"
            custom:valueColor="@android:color/holo_green_light"
             />

</LinearLayout> 

In your Activity

public class MainActivity extends Activity {

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        this.setContentView( R.layout.activity_main );
        final ColorOptionsView colorOptionsView = ( ColorOptionsView ) this.findViewById( R.id.main_color_options);

    }

}
wbelarmino
  • 509
  • 3
  • 7
  • thanks you. Your wesite is really good. just how to get the textView object? I do not see into TypedArray a method for this. they are getSring, getColor and other but not getView – bioinfornatics Jul 21 '14 at 22:09
  • The "colorOptionsView" works like other view objects can be accessed by "findViewById" – wbelarmino Jul 21 '14 at 22:43