CHarris
CHarris

Reputation: 2793

Android SearchView not positioning correctly

I downloaded some sample code here https://trinitytuts.com/get-contact-list-and-show-in-custom-listview-android/ which searches and lists contacts in my phone. The resulting app builds and loads well. I believe it uses the Android search widget as at the top of my mainactivity I have :

import android.widget.SearchView

(However I do not have a searachable.xml file which I have seen in some sources is a feature of this Search Widget)

According to Google, I believe the searchview widget can be positioned anywhere in the activity (I don't want it at the top of the activity, I want it at the bottom): http://developer.android.com/guide/topics/search/search-dialog.html#TheBasics

According to Google in the link above the the search dialogue always appears at the top of an activity, however I am lead to believe the widget can be positioned in different places.

But when I try to change the position of the Searchview in my activity, it doesn't display.

For example, this works fine in my activity_main xml, the searchview is at the top of the page:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <SearchView

        android:background="#5b74a8"
        android:queryHint="howdy"
        android:padding="8dp"
        android:id="@+id/searchView"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:layout_width="match_parent"
        android:singleLine="true"
        android:layout_gravity="right"
        android:layout_height="wrap_content"></SearchView>

    <ListView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contacts_list"></ListView>

</LinearLayout>

But in this case the SearchView isn't visible at all. Any ideas how I can place my search widget at the bottom?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <ListView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/contacts_list"></ListView>

    <SearchView

        android:background="#5b74a8"
        android:queryHint="howdy"
        android:padding="8dp"
        android:id="@+id/searchView"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:layout_width="match_parent"
        android:singleLine="true"
        android:layout_gravity="right"
        android:layout_height="wrap_content"></SearchView>

</LinearLayout>

Upvotes: 2

Views: 617

Answers (1)

Shyam
Shyam

Reputation: 36

Your list view is hiding SearchView..

You can do following to see its going at the end

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.codepath.mytestapp.MainActivity">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  tools:context=".MainActivity">


        <ListView

            android:id="@+id/contacts_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="right"
        android:background="#5b74a8"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:padding="8dp"
        android:queryHint="howdy"
        android:singleLine="true"
        />
</RelativeLayout>

Upvotes: 1

Related Questions