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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:22+00:00 2026-05-26T18:19:22+00:00

In my application I want to create one Search widget which would be Reusable

  • 0

In my application I want to create one Search widget which would be Reusable as .java file , i need to reflect it into layout.xml file so that i can use it. Directly…..Here is my code having one edit box , two buttons.

public class SearchWidget extends ViewGroup{

    Context mContext;
    LinearLayout layout;
    EditText edit;
    Button searchButton;
    Button clear;

    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    public SearchWidget(Context context) {
        super(context);
        this.mContext=context;
        layout=new LinearLayout(context);
        edit=new EditText(context);
        searchButton=new Button(context);
        clear=new Button(context);
        params.setMargins(10, 10, 10, 10);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(params);
        edit.setMaxLines(1);
        edit.setWidth(100);
        layout.addView(searchButton);
        layout.addView(clear);
    }

}

Please Suggest me how to give reference of this java file to my layout.xml
Thanks in Advance.

  • 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-26T18:19:23+00:00Added an answer on May 26, 2026 at 6:19 pm

    You can use it by using your package name along with the file name like

    <com.myapp.SearchWidget android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
    </com.myapp.SearchWidget>
    

    When you use the xml to put your custom view, you need to use the constructor with 2 parameters.

    After checking your code, there are many mistakes such as you are finally not adding ‘layout’ to the search widget. So nothing will show up. Im not sure what your end result is. But this is what I think you are trying to do.

    public class SearchWidget extends LinearLayout {
        public SearchWidget(Context context, AttributeSet attrs) {
            super(context, attrs);      
                EditText edit = new EditText(context);
            LinearLayout.LayoutParams elp = new LinearLayout.LayoutParams(0,
                LayoutParams.WRAP_CONTENT, 1.0f);
            edit.setLayoutParams(elp);
    
            Button searchButton = new Button(context);
            searchButton.setLayoutParams(new ViewGroup.LayoutParams(
                LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
            searchButton.setText("Search");
    
            Button clearButton = new Button(context);
            clearButton.setLayoutParams(new ViewGroup.LayoutParams(
                LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
            clearButton.setText("Clear");
    
            addView(edit);
            addView(searchButton);
            addView(clearButton);       
        }
    }
    

    Here I extended the LinearLayout

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

Sidebar

Related Questions

I want to create application which would display spatial data on Map. I'm thinking
I want to create a Java Desktop Application and want to employ some mechanism
I want to create a Java application bundle for Mac without using Mac. According
I want to create an application which will have a client and server components.
I'm trying to create and Android application which requires a number of search criteria
I want to create my Rails application with MySQL, because I like it so
I want to create a c# application with multiple windows that are all transparent
I want to create a new business application using the Django framework. Any suggestions
I want to create windows application in c# 2008. The problem is I don't
I want to create a navigation application but can't found any icons/symbols that suitable

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.