Question

Basically I want the searachview to expand and collapse when there is a imagebutton behind which should disappear when icon is clicked and view should appear when searchview collapses.

I am using search view not in action bar .When i click on the search icon it expands only half the screen but it should expand complete width but should wrap_content when I close the searchview.

<SearchView
        android:id="@+id/searchView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">

I also tried :

android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();
params.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

But I am getting error here setLayoutParams it says "add to cast"

Was it helpful?

Solution

At first you're getting an exception, because if you look into search_view.xml located in

\Android\android-sdk\platforms\android-18\data\res\layout\

You'll find out that SearchView is basically simple LinearLayout merging several widgets together. That's why you are getting class cast exception. Instead of

android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();

use

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchview.getLayoutParams();

or just import correct class and then you can simply use this

import android.widget.LinearLayout.LayoutParams;

                      ...

LayoutParams params = (LayoutParams) searchview.getLayoutParams();

than you can use LayoutParams without any prefix.

But I don't think that setting LayoutParams to wrap content will help. I would wrap your SearchView and ImageButton to RelativeLayout, where you could specify view position relative to the other views. Then simply set SearchView width to fill parent, so it could fill remaining space.

OTHER TIPS

Have you tried the XML-attribute android:maxWidth like described here: http://developer.android.com/reference/android/widget/SearchView.html#attr_android:maxWidth

I am using the SearchView from actionbarsherlock and used the corresponding java-method to stretch the SearchView over the available width of the actionbar.

first screenshot

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search_b"
        android:orderInCategory="3"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />

    <item
        android:id="@+id/action_scan"
        android:icon="@drawable/scan_b"
        android:title="Scan"
        android:orderInCategory="2"
        app:showAsAction="ifRoom|collapseActionView" />

    <item
        android:id="@+id/action_add_cusomer"
        android:icon="@drawable/add_cust_b"
        android:title="Add Customer"
        android:orderInCategory="1"
        app:showAsAction="ifRoom|collapseActionView" />

</menu>

After Clicking on Search icon:

screenshot after clicking on search

Try this according to your needs but dont think to work on your own button, you can move default searchButton to right side with giving wrap_content width parameter and then when you click to search set width to match_parent then again when you close it set width to wrap_content

Use your searchView child of RelativeLayout and be sure about right LayoutParams(RelativeLayout.LayoutParams)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/counter_text_color" >

    <Button
        android:id="@+id/buttonLeftMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_drawer" />

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/actionbar_logo" />

    <TextView
        android:id="@+id/tw_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="YOUR "
        android:textSize="24sp" />

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/buttonRightMenu" />

    <Button
        android:id="@+id/buttonRightMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_action_settings" />

</RelativeLayout>

and at your class

searchView.setOnSearchClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.RIGHT_OF, R.id.buttonLeftMenu);
        params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
        searchView.setLayoutParams(params);

    }
});

searchView.setOnCloseListener(new OnCloseListener() {

    @Override
    public boolean onClose() {
        // TODO Auto-generated method stub
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
        searchView.setLayoutParams(params);

        return false;
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top