Android Question [SOLVED] How to change width of AppCompat ACSearchView

Brandsum

Well-Known Member
Licensed User
Hi, @corwin42
I have an issue. ACSearchView width is not 100%. Here is my code:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    SearchBar.Initialize2("Search", SearchBar.THEME_DARK)
    SearchBar.QueryHint = "Search for stores"
    SearchBar.IconifiedByDefault = True
 
    Menu.Clear
    MenuItemHelper = Menu.Add(0,0,"Search",FontToBitmap(Chr(0xE8B6),True,35,Colors.White))
    MenuItemHelper.ShowAsAction = MenuItemHelper.SHOW_AS_ACTION_ALWAYS
    MenuItemHelper.SearchView = SearchBar
End Sub

For the output check screenshot attached.

6028914888491454213.png

How can I change the width?
 
Last edited:

Brandsum

Well-Known Member
Licensed User
Issue Solved.

android.support.v7.widget.searchview has a method for changing the width.
https://developer.android.com/reference/android/widget/SearchView.html#setMaxWidth(int)

So calling that method using Reflector solved my issue.

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    SearchBar.Initialize2("Search", SearchBar.THEME_DARK)
    SearchBar.QueryHint = "Search for stores"
    SearchBar.IconifiedByDefault = True
    '
    Dim r As Reflector
    r.Target = SearchBar
    r.Target = r.GetField("mSearchView")
    r.RunMethod4("setMaxWidth", Array As Object(Activity.Width), Array As String("java.lang.int"))
    
    Menu.Clear
    Menu.Add(0,0,"Search",FontToBitmap(Chr(0xE8B6),True,35,Colors.White)).ShowAsAction = MenuItemHelper.SHOW_AS_ACTION_ALWAYS
    Menu.GetItem(0).SearchView = SearchBar
End Sub

6144008216986780300.jpg
 
Upvote 0
Top