I want to load a layout XML file and ad the layout to the current content view.
So, if I get this layout over here:
and if I hit the hardware search button, then I want to display a search bar at the top of the screen, like this:
Based on this answer, I tried something like this:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.search_bar, null);
ViewGroup layout = (ViewGroup) findViewById(R.id.layout_main);
layout.addView(v);
}
The search bar is a layout file named search_bar.xml. R.layout.activity_main
is the main activity. R.id.layout_main
is the id of the RelativeLayout
which is the container in activity_main.
But I got an error inflating class.
How can I load a layout and add it to the currently loaded layout?