0

First of all, I would like to apologise if my question is very basic - I don't know which keywords are useful to find the answer.

I know how to programme two "types" of application - ones which are run by .xml files (for instance, a converter from Celsius to Fahrenheit, some options menu and so on), and ones which are run with a class which extends View (for applications with graphical components). My question is, how to combine them? More precisely, how to add a View component into an XML file? Practical example: drawing a circle in an options menu, which moves once touched.

Thank you in advance.

2 Answers 2

1

I don't know about drawing a circle in options menu, but in general you can use your custom View classes in layout xmls like this;

<com.testing.MyCustomView
  id="@+id/my_view"
  ... />

Making them very much alike compared to using TextViews, ImageViews etc.

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

2 Comments

Thank you very much, I am going to try this approach.
Well, I tried it on a simple extension of View and it works perfectly well. Tomorrow (too tired now...) I am going to introduce some information from the sensors - but I don't suppose it is going to cause any problems, since the sensors are dealt in the main Activity class (or maybe there is another way? I may be interested in that). Thank you again and have a nice week!
1

To use custom view in XML you will need to

  • Code your view in such a way that it accepts AttributeSet. Example:

    public ActivityTitleView(Context context, AttributeSet attrs)

  • Define styleable attributes. They go into res/values/attr.xml

<resources>
    <declare-styleable name="ActivityTitleView">   
        <attr name="text" format="string"/>
        <attr name="helpContext" format="string"/>   
    </declare-styleable>
</resources
  • Include your view in XML with your own namespace. Own namespace is important if you want your attributes:

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yournamespace="http://schemas.android.com/apk/res/com.yourpackage" >

<com.yourpackage.ActivityTitleView yournamespace:text="Bla Bla"  
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>
  • Extract attributes in code
     TypedArray array = context.obtainStyledAttributes(attrs,
          R.styleable.ActivityTitleView, 0, 0);
     String text = array.getString(R.styleable.ActivityTitleView_text);
     helpContext = array.getString(R.styleable.ActivityTitleView_helpContext);

Sorry StackOverflow does not seem to format my snippets well. Feel free to edit formatting.

2 Comments

Thank you very much for your answer. Unfortunately I think I don't know enough about programming in Android to understand everything you said, so for now I will take the other suggested approach, but when I have the time and effort - I will definitely look into it. Thanks!
Actually it's the same approach. I can see how it can be cryptic - I've been in your shoes couple of weeks ago. If you realize that you also want to customize your view (i.e. specify circle radius in xml), then you may want to get back and reread. Regards.

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.