Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:14:32+00:00 2026-05-14T15:14:32+00:00

How do I declare an Android UI element using XML?

  • 0

How do I declare an Android UI element using XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:14:32+00:00Added an answer on May 14, 2026 at 3:14 pm

    The Android Developer Guide has a section called Building Custom Components. Unfortunately, the discussion of XML attributes only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows:

    1. Declare attributes in values\attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MyCustomView">
            <attr name="android:text"/>
            <attr name="android:textColor"/>            
            <attr name="extraInformation" format="string" />
        </declare-styleable>
    </resources>
    

    Notice the use of an unqualified name in the declare-styleable tag. Non-standard android attributes like extraInformation need to have their type declared. Tags declared in the superclass will be available in subclasses without having to be redeclared.

    2. Create constructors

    Since there are two constructors that use an AttributeSet for initialisation, it is convenient to create a separate initialisation method for the constructors to call.

    private void init(AttributeSet attrs) { 
        TypedArray a=getContext().obtainStyledAttributes(
             attrs,
             R.styleable.MyCustomView);
    
        //Use a
        Log.i("test",a.getString(
             R.styleable.MyCustomView_android_text));
        Log.i("test",""+a.getColor(
             R.styleable.MyCustomView_android_textColor, Color.BLACK));
        Log.i("test",a.getString(
             R.styleable.MyCustomView_extraInformation));
    
        //Don't forget this
        a.recycle();
    }
    

    R.styleable.MyCustomView is an autogenerated int[] resource where each element is the ID of an attribute. Attributes are generated for each property in the XML by appending the attribute name to the element name. For example, R.styleable.MyCustomView_android_text contains the android_text attribute for MyCustomView. Attributes can then be retrieved from the TypedArray using various get functions. If the attribute is not defined in the defined in the XML, then null is returned. Except, of course, if the return type is a primitive, in which case the second argument is returned.

    If you don’t want to retrieve all of the attributes, it is possible to create this array manually.The ID for standard android attributes are included in android.R.attr, while attributes for this project are in R.attr.

    int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};
    

    Please note that you should not use anything in android.R.styleable, as per this thread it may change in the future. It is still in the documentation as being to view all these constants in the one place is useful.

    3. Use it in a layout files such as layout\main.xml

    Include the namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto" in the top level xml element. Namespaces provide a method to avoid the conflicts that sometimes occur when different schemas use the same element names (see this article for more info). The URL is simply a manner of uniquely identifying schemas – nothing actually needs to be hosted at that URL. If this doesn’t appear to be doing anything, it is because you don’t actually need to add the namespace prefix unless you need to resolve a conflict.

    <com.mycompany.projectname.MyCustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="Test text"
        android:textColor="#FFFFFF"
        app:extraInformation="My extra information"
    /> 
    

    Reference the custom view using the fully qualified name.

    Android LabelView Sample

    If you want a complete example, look at the android label view sample.

    LabelView.java

     TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView);
     CharSequences=a.getString(R.styleable.LabelView_text);
    

    attrs.xml

    <declare-styleable name="LabelView">
        <attr name="text"format="string"/>
        <attr name="textColor"format="color"/>
        <attr name="textSize"format="dimension"/>
    </declare-styleable>
    

    custom_view_1.xml

    <com.example.android.apis.view.LabelView
        android:background="@drawable/blue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        app:text="Blue" app:textSize="20dp"/>
    

    This is contained in a LinearLayout with a namespace attribute: xmlns:app="http://schemas.android.com/apk/res-auto"

    Links

    • StackOverflow Thread: Retrieving an XML attribute for custom control
    • How do I use obtainStyledAttributes with internal themes of Android
    • Defining custom attributes + list of supported attribute formats
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd say the problem is here: define('SECRET', "vJs;ly-W\XDkD_2'-M7S2/ZRRBobxt5"); // ^^--… May 15, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer If the path you're referencing is a directory, vim will… May 15, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer That code won't work at all. As you are setting… May 15, 2026 at 3:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.