Dipak Dipak - 2 months ago 6
Android Question

SearchFunctionality to edittext in Android

I have Edittext with search functionality with listview and listview visibility is gone but when i am searching in that
edittextbox and select item from list and set to edittextbox there is space is created between another edittext and first edittext in android please help me.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusable="false"
android:id="@+id/dipak"
android:background="@color/colorforbuttonss"
android:focusableInTouchMode="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5e0d3a"
android:id="@+id/toolbar1"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<Button
android:background="@drawable/rounded_corners"
android:text=" "
android:layout_gravity="center"
android:layout_width="250dp"
android:textAlignment="center"
android:textSize="15dp"
android:layout_marginTop="40dp"
android:layout_marginBottom="15dp"
android:layout_height="wrap_content"
android:id="@+id/tables_item" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<EditText
android:id="@+id/selectitem"
android:layout_gravity="center"
android:layout_width="250dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@drawable/rounded_corners"
android:textColor="#000"
android:hint="Select Item"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:visibility="gone"
android:background="@color/colorforbuttons"
android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_gravity="center">
<EditText
android:id="@+id/quantitybox_itemorder"
android:layout_gravity="center" android:layout_width="250dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/rounded_corners"
android:textColor="#000" android:digits="0123456789."
android:padding="10dp"
android:hint="Quantity" />

<EditText
android:id="@+id/Description_item"
android:layout_gravity="center"
android:layout_width="250dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@drawable/rounded_corners"
android:textColor="#000"
android:hint="Description of item" />
<Button
android:background="@drawable/buttonshape"
android:text="Add Order"
android:layout_gravity="center"
android:layout_width="250dp"
android:textAlignment="center"
android:textSize="20dp"

android:layout_margin="10dp" android:layout_marginTop="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginLeft="-10dp"
android:textColor="@android:color/primary_text_dark"
android:layout_height="wrap_content"
android:id="@+id/add_order_item" />
<Button
android:background="@drawable/buttonshape"
android:text="Show Inserted"
android:layout_gravity="center"
android:layout_width="250dp"
android:textAlignment="center"
android:textSize="20dp"
android:layout_margin="10dp"
android:layout_marginTop="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginLeft="-10dp"
android:textColor="@android:color/primary_text_dark"
android:layout_height="wrap_content"
android:id="@+id/showitem" /> </LinearLayout> </LinearLayout>

Answer

You need to make changes in xml. Change your parent to Relative so that list should open on the edittext not below it. Because in case of linear layout with vertical orientation when you ll make listview visible then it ll occupy space vertically and definitely the edit text will move down. If you understood the point then its good else post your complete xml its not complete xml you have posted here. I ll make changes in it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dipak"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/darker_gray"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#5e0d3a"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <Button
        android:id="@+id/tables_item"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="40dp"
        android:text="abc"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/toolbar1"
        android:textAlignment="center"
        android:textSize="15dp" />

        <EditText
            android:id="@+id/selectitem"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:hint="Select Item"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tables_item"
            android:textAlignment="center"
            android:textColor="#000" />        

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:layout_below="@id/selectitem">

        <EditText
            android:id="@+id/quantitybox_itemorder"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:digits="0123456789."
            android:hint="Quantity"
            android:padding="10dp"
            android:textAlignment="center"
            android:textColor="#000" />

        <EditText
            android:id="@+id/Description_item"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:hint="Description of item"
            android:padding="10dp"
            android:textAlignment="center"
            android:textColor="#000" />

        <Button
            android:id="@+id/add_order_item"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_marginLeft="-10dp"
            android:layout_marginRight="-10dp"
            android:layout_marginTop="-10dp"
            android:text="Add Order"
            android:textAlignment="center"
            android:textColor="@android:color/primary_text_dark"
            android:textSize="20dp" />
        <Button
            android:id="@+id/showitem"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_marginLeft="-10dp"
            android:layout_marginRight="-10dp"
            android:layout_marginTop="-10dp"
            android:text="Show Inserted"
            android:textAlignment="center"
            android:textColor="@android:color/primary_text_dark"
            android:textSize="20dp" />
    </LinearLayout>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:visibility="gone"
        android:layout_below="@id/selectitem"/>
</RelativeLayout>

Have a look on this now when you will make your listview visible then the edittext will not moe down or there wil not be space below. After selecting item from listview make its visibility gone programmatically. If you feel any problem let me know.