Android Question AppCompat SearchView

jcesar

Active Member
Licensed User
Longtime User
Hi

I'm trying insert a searchview on appcompat actionbar:

B4X:
Dim sv As ACSearchView

B4X:
sv.Initialize("")
sv.QueryHint = "Search"
ACToolBarLight1.AddView(sv,50%x,7%y,Gravity.NO_GRAVITY)

But a get this error:

ACToolBarLight1.AddView(sv,50%x,7%y,Gravity.NO_GR
javac 1.8.0_121
src\b4a\example\main.java:360: error: incompatible types: ACSearchViewWrapper cannot be converted to View
mostCurrent._actoolbarlight1.AddView((android.view.View)(mostCurrent._sv),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (50),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.PerYToCurrent((float) (7),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.Gravity.NO_GRAVITY);
 

DonManfred

Expert
Licensed User
Longtime User
you are not doing it right

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ScrollView1 As ScrollView
    Private ACSwitch1 As ACSwitch
    Private ACActionMenu1 As ACActionMenu
    Private ACSubmenu1 As ACSubMenu
    Private ACButton3 As ACButton
    Private Panel6 As Panel
    Private ACSpinner1 As ACSpinner
    Private ACSpinner2 As ACSpinner

    Private sv As ACSearchView
    Private si As ACMenuItem
End Sub

'Inline Java code to initialize the Menu
#If Java
    public boolean _onCreateOptionsMenu(android.view.Menu menu) {
        if (processBA.subExists("activity_createmenu")) {
            processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
            return true;
        }
        else
            return false;
    }
#End If

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("layout1")
    
    ScrollView1.Panel.LoadLayout("lay_scrollview")
    ScrollView1.Panel.Height = Panel6.Top + Panel6.Height + 8dip
    
    ACSpinner1.AddAll(Array As String ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
    ACSpinner2.AddAll(Array As String ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))

    'Ad some Action Menu items
    ACActionMenu1.Menu.Add(1, 1, "Menu1", Null)
    ACSubmenu1 = ACActionMenu1.Menu.AddSubMenu(1, 2, 2, "Submenu")
    ACSubmenu1.Add(101, 1, "Submenu Item 1", Null)
    ACSubmenu1.Add(102, 2, "Submenu Item 2", Null)
    ACActionMenu1.Menu.Add(3, 3, "MenuItem 2", Null)
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'This is the Sub called by the inline Java code to initialize the Menu
Sub Activity_CreateMenu(Menu As ACMenu)
    sv.Initialize2("Search", sv.THEME_DARK)
    sv.IconifiedByDefault = True

    'Clear the menu
    Menu.Clear
    
    'Add a menu item and assign the SearchView to it
    si = Menu.Add2(1, 1, "Search", Null)
    si.SearchView = sv
End Sub

'If the user submits a search then this event sub is called
Sub Search_QuerySubmitted (Query As String)

    sv.Iconfied = True
    si.ItemCollapsed = True
  
    Log("Search for '" & Query & "'")
End Sub

Sub Search_Closed
    Log("SearchView closed")
End Sub

Sub Search_QueryChanged (Query As String)
    Log("Query changed: " & Query)
End Sub
 

Attachments

  • appcompatsearchview.zip
    11 KB · Views: 429
Upvote 0

jcesar

Active Member
Licensed User
Longtime User
Thanks for your reply Manfred.

Unfortunately I could not run the example because i'm using a older version of b4a (8.30).
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Try this:
B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Dim MenuItemHelper As ACMenuItem
    Dim SearchBar As ACSearchView
    SearchBar.Initialize2("Search", SearchBar.THEME_LIGHT)
    SearchBar.QueryHint = "Search..."
    SearchBar.IconifiedByDefault = False
 
    Dim r As Reflector
    'search box text color, width, hint text color
    r.Target = SearchBar
    r.Target = r.GetField("mSearchView")
    r.RunMethod4("setMaxWidth", Array As Object(Activity.Width), Array As String("java.lang.int"))
    r.Target = r.RunMethod4("findViewById", Array As Object(r.GetStaticField("android.support.v7.appcompat" & ".R$" & "id", "search_src_text")), Array As String("java.lang.int"))
    r.RunMethod4("setHintTextColor", Array As Object(Colors.ARGB(100,10,170,146)), Array As String("java.lang.int"))
    r.RunMethod4("setTextColor", Array As Object(Colors.Red), Array As String("java.lang.int"))

   'Add search view to ActionBar
    Menu.Clear
    Menu.Add(0,0,"Search",Null).ShowAsAction = MenuItemHelper.SHOW_AS_ACTION_ALWAYS
    Menu.GetItem(0).SearchView = SearchBar

End Sub

#If Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
    if (processBA.subExists("activity_createmenu")) {
        processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
        return true;
    }
    else
        return false;
}
#End If
 
Upvote 0

jcesar

Active Member
Licensed User
Longtime User
you can copy the code from the example or just use the forumsearch. It is one of the Appcompat Examples here in the forum

I did this. But the layout files still missing.

I download a lot of AppCompat samples, but I did not find any with searchview.
 
Upvote 0
Top