androidandroid-studiosearchviewandroid-search

Android Studio SearchView UI extending at the right side of the screen


My SearchView in Android Studio preview looks like this:

https://i.sstatic.net/Khyy5.png

But in my physical device, it looks like this:

https://i.sstatic.net/PlrFX.jpg

How can I fix this? Let it not expand on the right side. I only have this code in my xml

<SearchView
    android:layout_width="343dp"
    android:layout_height="wrap_content"
    android:background="#fafafa"
    android:layout_marginTop="65dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp" />

Solution

  • Try not to hardcode as much as possible. So, remove the 343dp width value and replace it with match_parent.

    android:layout_width="match_parent"
    

    If the parent is a RelativeLayout then use this to centre the view:

    android:layout_centerHorizontal="true"
    

    And set an overall margin to position it away from the borders.