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 3600676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:31:32+00:00 2026-05-18T20:31:32+00:00

When creating a custom component in android it is often asked how to create

  • 0

When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor.

It is often suggested that when creating a component in java that you simply use the default constructor, i.e.

new MyComponent(context);

rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I’ve tried to create an attrs object and it doesn’t seem either easy or at all possible (without an exceedingly complicated process), and by all accounts isn’t really required.

My question is then: What is the most efficient way of construction a custom component in java that passes or sets properties that would have otherwise been set by the attrs object when inflating a component 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-18T20:31:32+00:00Added an answer on May 18, 2026 at 8:31 pm

    (Full disclosure: This question is an offshoot of Creating custom view)

    You can create constructors beyond the three standard ones inherited from View that add the attributes you want…

    MyComponent(Context context, String foo)
    {
      super(context);
      // Do something with foo
    }
    

    …but I don’t recommend it. It’s better to follow the same convention as other components. This will make your component as flexible as possible and will prevent developers using your component from tearing their hair out because yours is inconsistent with everything else:

    1. Provide getters and setters for each of the attributes:

    public void setFoo(String new_foo) { ... }
    public String getFoo() { ... }
    

    2. Define the attributes in res/values/attrs.xml so they can be used in XML.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <declare-styleable name="MyComponent">
        <attr name="foo" format="string" />
      </declare-styleable>
    </resources>
    

    3. Provide the three standard constructors from View.

    If you need to pick anything out of the attributes in one of the constructors that takes an AttributeSet, you can do…

    TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyComponent);
    CharSequence foo_cs = arr.getString(R.styleable.MyComponent_foo);
    if (foo_cs != null) {
      // Do something with foo_cs.toString()
    }
    arr.recycle();  // Do this when done.
    

    With all that done, you can instantiate MyCompnent programmatically…

    MyComponent c = new MyComponent(context);
    c.setFoo("Bar");
    

    …or via XML:

    <!-- res/layout/MyActivity.xml -->
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:blrfl="http://schemas.android.com/apk/res-auto"
      ...etc...
    >
      <com.blrfl.MyComponent
       android:id="@+id/customid"
       android:layout_weight="1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_gravity="center"
       blrfl:foo="bar"
       blrfl:quux="bletch"
      />
    </LinearLayout>
    

    Additional Resource – https://developer.android.com/training/custom-views/create-view

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a custom Java Struts tag that is for building and formatting an
I am creating a custom component that is an image viewer for a given
In WPF, we are creating custom controls that inherit from button with completely drawn-from-scratch
I am creating a custom WPF control that let's say for simplicity sake has
I'm fairly new to BizTalk and creating a custom pipeline component. I have seen
I'm having problems with creating a Facelet Composition Control (= custom tag). That's the
Does anybody know of a method for creating custom Performance Counters using ordinary unmanaged
I am just learning about app.config in respect of creating custom sections. I have
I'm creating a custom drop down list with AJAX dropdownextender. Inside my drop panel
I'm creating some custom work items in TFS and the helptext field seems handy

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.