0

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:

Layout without search bar.

and if I hit the hardware search button, then I want to display a search bar at the top of the screen, like this:

Layout with search bar.

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?

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130

2 Answers2

1

I do not see an obvious problem with your code. As was mentioned in a comment, please post the log here.

May I suggest another approach? You could include the search bar (either in your main layout or using the include tag) and set it's visibility to GONE until you need to show it.

britzl
  • 10,132
  • 7
  • 41
  • 38
0

I did a little research and combined several hints to the solution.
First of all, I used LayoutInflater.from(Context) instead of the Context.LAYOUT_INFLATER_SERVICE (although that doesn't seem to be the problem). Second, I used the onSearchRequest() method.

This is the result:

/**
 * Whether the search bar is visible or not.
 */
private boolean searchState = false;

/**
 * The View loaded from the search_bar.xml layout.
 */
private View searchView;

/**
 * This method is overridden from the Activity class, enabling you to define events when the hardware search button is pressed.
 *
 * @return Returns true if search launched, and false if activity blocks it.
 */
public boolean onSearchRequested() {
    // Toggle the search state.
    this.searchState = !this.searchState;
    // Find the main layout
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.layout_main);
    // If the search button is pressed and the state has been toggled on:
    if (this.searchState) {
        LayoutInflater factory = LayoutInflater.from(this.activity);
        // Load the search_bar.xml layout file and save it to a class attribute for later use.
        this.searchView = factory.inflate(R.layout.search_bar, null);
        // Add the search_bar to the main layout (on position 0, so it will be at the top of the screen if the viewGroup is a vertically oriented LinearLayout).
        viewGroup.addView(this.searchView, 0);
    }
    // Else, if the search state is false, we assume that it was on and the search_bar was loaded. Now we remove the search_bar from the main view.
    else {
        viewGroup.removeView(this.searchView);
    }
    return false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130