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.
You can use it by using your package name along with the file name like
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.
Here I extended the LinearLayout