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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:07:34+00:00 2026-05-28T06:07:34+00:00

I have extended a RelativeLayout with all its constructor.And I have created a layout

  • 0

I have extended a RelativeLayout with all its constructor.And I have created a layout in xml that I am inflating in one of constructor of my class.

My Code for MyClass

 public class MyClass extends RelativeLayout
 {
        LayoutInflater inflater = null;
        EditText edit_text;
        Button btn_clear;

     public MyClass(Context context, AttributeSet attrs, int defStyle)
     {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
     }
        public MyClass(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            // TODO Auto-generated constructor stub

            inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.my_class_layout, this, true);
            edit_text = (EditText) findViewById(R.id.clearable_edit);
            btn_clear = (Button) findViewById(R.id.clearable_button_clear);
            btn_clear.setVisibility(RelativeLayout.INVISIBLE);
        }

        public MyClass(Context context)
        {
            super(context);
            // TODO Auto-generated constructor stub
        }
    }

My class layout which I inflate

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/clearable_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />
    <Button
        android:id="@+id/clearable_button_clear"
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:layout_alignParentRight="true"
        android:background="@drawable/image_clear" 
        android:layout_centerVertical="true"
        android:layout_marginRight="5dip"/>
  </RelativeLayout>

And I use MyClass in my activity’s layout like this

<com.and.MyClass
    android:id="@+id/my_class"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

This works fine.
but when I inflate my class layout in constructor MyClass(Context context) then it doesn’t work.

I am not getting what is the problem. I hope you got my question.

  • 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-28T06:07:34+00:00Added an answer on May 28, 2026 at 6:07 am

    This is OK.
    When we use a View in xml

    public MyClass(Context context, AttributeSet attrs)
    

    this constructor will be called.

    Because all the attributes from XML are passed to this constructor using AttributeSet, so that these values will have effect on View‘s layout and other fields.

    However if you want to use your View in Java also, you should also inflate in all View‘s constructor (Which is recommended approach).

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

Sidebar

Related Questions

I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have extended a class that is part of a large library. I instantiate
I have an Entity class that I have extended using the partial class syntax
I have class CalloutView extended from RelativeLayout . Currently has no any methods, it
I have extended my entities to implement specific interfaces for its type. I am
I have extended the ASP.NET MVC Html Helper to include my own ValidationImage that
I have extended a server control (not a user control) and put the code
Hello I have an extended stored procedure that sends an error message. srv_sendmsg(pSrvProc, SRV_MSG_ERROR,
I have extended my class with a dialog in which I have set content
I have a view in the UI that is technically an extended ImageView inside

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.