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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:53:50+00:00 2026-06-01T11:53:50+00:00

I have added an easy compound control which loads its layout from a XML

  • 0

I have added an easy compound control which loads its layout from a XML layout and implements own XML attributes.

But this only works while this control is in the main package (com.myapp). When I try to move it into a subpackage like com.myapp.controls things begin to fail.

This is the attrs.xml to define the control custom attributes:

<resources>

    <declare-styleable name="CH2">
        <attr name="textText" format="string" />
    </declare-styleable>

    <declare-styleable name="CH3">
        <attr name="textFex" format="string" />
    </declare-styleable>

</resources>

This is the control CH2.java which ia in subspackage and does not work:

package com.myapp.controls;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CH2 extends LinearLayout {

    private TextView textView;

    public CH2(Context context) throws Exception {
        super(context);
        init(context, null);
    }

    public CH2(Context context, AttributeSet attrs) throws Exception {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) throws Exception {

        this.setOrientation(LinearLayout.VERTICAL);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);

        View v = findViewById(com.myapp.R.id.chText);
        this.textView = (TextView)v;

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH2);

            String text = a.getString(com.myapp.R.styleable.CH2_textText);
            this.textView.setText(text != null ? text : "<NULL!>");

            a.recycle();        
        }
    }
}

This is CH3.java which works:

package com.myapp;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CH3 extends LinearLayout {

    private TextView textView;

    public CH3(Context context) throws Exception {
        super(context);
        init(context, null);
    }

    public CH3(Context context, AttributeSet attrs) throws Exception {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) throws Exception {

        this.setOrientation(LinearLayout.VERTICAL);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);

        View v = findViewById(com.myapp.R.id.chText);
        this.textView = (TextView)v;

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH3);

            String text = a.getString(com.myapp.R.styleable.CH3_textFex);
            this.textView.setText(text != null ? text : "<NULL!>");

            a.recycle();        
        }
    }
}

Both sources use the same category_header.xml. The layout is loaded without problem. I can use both like this:

<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    myns:textText="Test 1" >
</com.myapp.controls.CH2>

<com.myapp.CH3
    android:id="@+id/cH2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    myns:textFex="Test 2"
    >
</com.myapp.CH3>

How can I make CH2 work? Or is it impossible to use another package?

Sidequest: Can I make both controls to use the same XML attribute textText or will they always have to use different attributes? Or can the declareable-style name be different from the components name?


Another approach with a new declare-styleable:

<declare-styleable name="controls.CH2">
    <attr name="textText" format="string" />
</declare-styleable>

But it does not seem to be usable. XML view always says Unexpected text found in layout file:, regardless how I use CH2:

xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
xmlns:mynsc="http://schemas.android.com/apk/res/com.myapp.controls"
...
<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    mynsc:textText="TestTestTest" 
    >        
</com.myapp.controls.CH2>

or

xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
...
<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    myns.controls:textText="TestTestTest" 
    >        
</com.myapp.controls.CH2>
  • 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-01T11:53:52+00:00Added an answer on June 1, 2026 at 11:53 am

    I just went back to my own attribute files and there’s no reference anywhere to the very deep sub-package where my layout lives… as a side note, you can have multiple elements use the same attribute if you do this in your attrs file:

    <resources>
        <attr name="textText" format="string"/>
    
        <declare-styleable name="CH2">
            <attr name="textText" />
        </declare-styleable>
    
        <declare-styleable name="CH3">
            <attr name="textText" />
        </declare-styleable>
    
    </resources>
    

    My study of the android source code shows a few examples where they do exactly what you’re describing, where they have a single custom attribute file pointing to view classes in different packages. The only glaring difference I saw was that the Views were declared in layout like this:

    <view
      class="com.myapp.controls.CH2"
      android:id="@+id/cH1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
      myns:textText="Test 1" >
    </view>
    
    <view
      class="com.myapp.CH3"
      android:id="@+id/cH2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      myns:textFex="Test 2"
    >
    </view>
    

    I honestly couldn’t tell you why that would make a difference… but every time they need custom attributes that is the format used

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

Sidebar

Related Questions

Does anyone have a quick and easy method for removing dynamically added buttons from
I have added a controller to my project named UserManager (automatically generated from the
I have added meta tag description in my page, but it is not displaying
I have added jQuery to my webiste from google. The awful part is the
I have added some code which compiles cleanly and have just received this Windows
Kind of a strange question, but here is my scenario: I have accidentally added
I have a small problem that would seem to be easy to solve, but
Hopefully an easy one, I have created a Custom Repeater control that extends System.Web.UI.WebControls.Repeater.
Hopefully an easy question: I have added some docs to the index, where every
I have added plain text file to the repository and git decided that it

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.