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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:22:00+00:00 2026-06-12T01:22:00+00:00

I have several custom View s in which I have created custom styleable attributes

  • 0

I have several custom Views in which I have created custom styleable attributes that are declared in xml layout and read in during the view’s constructor. My question is, if I do not give explicit values to all of the custom attributes when defining my layout in xml, how can I use styles and themes to have a default value that will be passed to my View‘s constructor?

For example:

attrs.xml:

<declare-styleable name="MyCustomView">
    <attr name="customAttribute" format="float" />
</declare-styleable>

layout.xml (android: tags eliminated for simplicity):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.mypackage" >

    <-- Custom attribute defined, get 0.2 passed to constructor -->

    <com.mypackage.MyCustomView
        app:customAttribute="0.2" />

    <-- Custom attribute not defined, get a default (say 0.4) passed to constructor -->

    <com.mypackage.MyCustomView />

</LinearLayout>
  • 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-06-12T01:22:01+00:00Added an answer on June 12, 2026 at 1:22 am

    After doing more research, I realized that default values can be set in the constructor for the View itself.

    public class MyCustomView extends View {
    
        private float mCustomAttribute;
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            TypedArray array = context.obtainStyledAttributes(attrs,
                R.styleable.MyCustomView);
            mCustomAttribute = array.getFloat(R.styleable.MyCustomView_customAttribute,
                0.4f);
    
            array.recycle();
        }
    }
    

    The default value could also be loaded from an xml resource file, which could be varied based on screen size, screen orientation, SDK version, etc.

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

Sidebar

Related Questions

I created a ListView with a custom layout for each view. I have several
I have a custom view that contains several views in itself. I've made it
I have a custom view with several NSTextField controls for which I want to
I have several spinners that I have created a custom ArrayAdapter for so I
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I'm using a custom JLayeredPane. I have several Shapes which needed to be drawn
I have custom event that has several different subscribers who will all use the
I have a custom UITableViewCell which contains several UIButtons. Each button's frame position is
I have seen that in Cocoa I can create a custom view using drawing
I have an application where I have one custom view which is derived from

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.