Android Question AppCompat SearchView

Discussion in 'Android Questions' started by jcesar, Dec 13, 2018.

  1. jcesar

    jcesar Active Member Licensed User

    Hi

    I'm trying insert a searchview on appcompat actionbar:

    Code:
    Dim sv As ACSearchView
    Code:
    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);
     
  2. DonManfred

    DonManfred Expert Licensed User

    you are not doing it right

    Code:
    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(11"Menu1"Null)
        ACSubmenu1 = ACActionMenu1.Menu.AddSubMenu(
    122"Submenu")
        ACSubmenu1.Add(
    1011"Submenu Item 1"Null)
        ACSubmenu1.Add(
    1022"Submenu Item 2"Null)
        ACActionMenu1.Menu.Add(
    33"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(11"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
     

    Attached Files:

    JMB, jcesar and Erel like this.
  3. jcesar

    jcesar Active Member Licensed User

    Thanks for your reply Manfred.

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

    DonManfred Expert Licensed 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
     
  5. Brandsum

    Brandsum Well-Known Member Licensed User

    Try this:
    Code:
    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
     
    jcesar likes this.
  6. jcesar

    jcesar Active Member Licensed User

    I did this. But the layout files still missing.

    I download a lot of AppCompat samples, but I did not find any with searchview.
     
  7. Brandsum

    Brandsum Well-Known Member Licensed User

    I've modified some code above. Please check that again.
     
    jcesar likes this.
  8. jcesar

    jcesar Active Member Licensed User

    Thanks to Brandsum and DonManfred. Problem solved.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice