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

  • Home
  • SEARCH
  • 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 9254537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:29:41+00:00 2026-06-18T11:29:41+00:00

I’ve implemented a custom view with hosts two subviews which are identified by an

  • 0

I’ve implemented a custom view with hosts two subviews which are identified by an id in the xml. When using two of this custom view in the same layout I run into the problem that it is random which custom view is chosen.

How can I write a custom view with different view ids that can be multiply used in the same layout?

Here is the xml of the custom view:

<?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" >

<EditText
    android:id="@+id/clearable_edit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:paddingRight="35dip" />

<Button
    android:id="@+id/clearable_button_clear"
    android:layout_width="30dip"
    android:layout_height="30dip"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dip"
    android:background="@drawable/clear_button" />

</RelativeLayout>

The id (android:id=”@+id/clearable_edit”) of the EditText is the problem here.

Usage of custom view:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <com.custom.package.ClearableEditText
                android:id="@+id/arr_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 >
            </com.custom.package.ClearableEditText>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <com.custom.package.ClearableEditText
                android:id="@+id/dep_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 >
            </com.custom.package.ClearableEditText>
        </LinearLayout>

In this example the views of type “ClearableEditText” share the same id of their EditText subview.

Here is the code for ClearableEditText:

public class ClearableEditText extends RelativeLayout {

private LayoutInflater inflater = null;
private EditText edit_text;
private Button btn_clear;

public ClearableEditText(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    initViews();
}

public ClearableEditText(Context context, AttributeSet attrs){
    super(context, attrs);
    initViews();

}

public ClearableEditText(Context context){
    super(context);
    initViews();
}

private void initViews(){
    inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.clearable_edittext, this, true);
    edit_text = (EditText) view.findViewById(R.id.clearable_edit);
    btn_clear = (Button) findViewById(R.id.clearable_button_clear);
    btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
}
  • 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-18T11:29:42+00:00Added an answer on June 18, 2026 at 11:29 am

    I’ve found a solution.

    I’ve added a method to ClearableEditText where you can set the id of the underlying EditText from outside the object and set it with a new id.

    Here is a code sample:

    //inside ClearableEditText
    public void setEditId(int id){
        edit_text.setId(id);
    }
    
    //somewhere else
    
    departureLocation = (ClearableEditText)view.findViewById(R.id.dep_location);
    departureLocation.setEditId(R.id.clearable1);
    arrivalLocation = (ClearableEditText)view.findViewById(R.id.arr_location);         
    arrivalLocation.setEditId(R.id.clearable2);
    

    The Ids are created with a “ids.xml” in the values folder, which causes eclipse/ADT to create a placeholder id for the entered items

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <!-- This file is used to create unique ids for custom views, which will be used more 
       than once in the same layout file. Using unique ids prevents the custom view from         getting 
       the wrong state. -->
            <item name="clearable1" type="id"></item>
            <item name="clearable2" type="id"></item>
    </resources>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am writing an app for my school newspaper, which is run completely online
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
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.