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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:18:56+00:00 2026-05-27T05:18:56+00:00

I’ve got a custom component containing two TextViews that have a custom size-setting method

  • 0

I’ve got a custom component containing two TextViews that have a custom size-setting method on (the two text views are proportional by about a 1:2 ratio). Being that this is a subclass of RelativeLayout, it doesn’t have a textSize attribute, but I’m wondering if it is possible to still set the android:textSize attribute in the XML instantiation for this component, and then grab the textSize attribute from the AttributeSet to use with my custom setSize() method in the constructor.

I’ve seen the technique to do this with custom attributes, but what if I want to grab an attribute that’s already in the android lexicon?

  • 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-27T05:18:57+00:00Added an answer on May 27, 2026 at 5:18 am

    yes it is possible;

    let’s assume your RelativeLayout declaration (in xml) has textSize defined with 14sp:

    android:textSize="14sp"
    

    In the constructor of your custom view (the one that takes in the AttributeSet), you can retrieve the attributes from Android’s namespace as such:

    String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
    

    The value of the xmlProvidedSize will be something like this “14.0sp” and maybe with little bit of String editing you can just extract the numbers.


    Another option to declare your own attribute set would be little lengthy, but it is also possible.

    So, you have your custom view and your TextViews declared something like this right:

    public class MyCustomView extends RelativeLayout{
    
        private TextView myTextView1;
        private TextView myTextView2;
    
    // rest of your class here
    

    great…

    Now you need to also make sure your custom view overrides the constructor that takes in the AttributeSet like this:

    public MyCustomView(Context context, AttributeSet attrs){   
        super(context, attrs);
        init(attrs, context);  //nice, clean method to instantiate your TextViews// 
    }
    

    ok, let’s see that init() method now:

    private void init(AttributeSet attrs, Context context){
        // do your other View related stuff here //
    
    
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyCustomView);
        int xmlProvidedText1Size = a.int(R.styleable.MyCustomView_text1Size);
        int xmlProvidedText2Size = a.int(R.styleable.MyCustomView_text2Size);
    
        myTextView1.setTextSize(xmlProvidedText1Size);
        myTextView2.setTextSize(xmlProvidedText2Size);
    
        // and other stuff here //
    }
    

    You’re probably wondering where does R.styleable.MyCustomView, R.styleable.MyCustomView_text1Size and R.styleable.MyCustomView_text2Size are coming from; allow me to elaborate on those.

    You have to declare the attribute name(s) in your attrs.xml file (under values directory) so that where ever you get to use your custom view, the values gathered from these attributes will be handed in your constructor.

    So let’s see how you declare the these custom attributes like you’ve asked:
    Here is my whole attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MyCustomView">
            <attr name="text1Size" format="integer"/>
            <attr name="text2Size" format="integer"/>
        </declare-styleable>
    </resources>
    

    Now you can set your TextViews’ size in your XML, but NOT without declaring the namespace in your Layout, here is how:

    <com.my.app.package.MyCustomView
        xmlns:josh="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_custom_view_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        josh:text1Size="15"
        josh:text2Size="30"     
        />
    

    Please pay attention how I declared the namespace to be “josh” as the first line in your CustomView’s attribute set.

    I hope this helps Josh,

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.