Search code examples
androidandroid-widgetandroid-xmlandroid-library

Android mancj.MaterialSearchBar: Using reflection to override and change some API layout and widgets, font-family, boldness and padding


I'm using https://github.com/mancj/MaterialSearchBar to set a search bar with a menu burger. I can't change of API.

Here is what I obtained:

enter image description here

Questions

How to...:

  1. Reduce the padding-left of the text?

  2. Change the font-family of the latter?

  3. Finally, disable boldness?

NB: these questions are completed by some others in the next section, According to the documentation.

According to the documentation

mt_aBCD are the XML attributes that allow us to customize the search bar. However, the above ones don't exist. Their Java equivalent (setABCD neither).

By the way, note that there isn't a lot of Github activity for this API. I can't replace the latter with another one. What solution would you give me?

  1. Should I change something in this API (is it authorized and legal)? (how?)
  2. Could I override the style? etc. (how?)
  3. Other?

My implementation

I edited my build file, then I simply added the search bar in my XML layout:

    <com.mancj.materialsearchbar.MaterialSearchBar
    android:id="@+id/material_search_bar"
    style="@style/MaterialSearchBarLight"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="15dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="15dp"
    app:layout_constraintBottom_toBottomOf="@+id/toolbar"
    app:layout_constraintEnd_toEndOf="@+id/toolbar"
    app:layout_constraintStart_toStartOf="@+id/toolbar"
    app:layout_constraintTop_toTopOf="@+id/toolbar"
    app:mt_maxSuggestionsCount="10"
    app:mt_navIconEnabled="true"
    app:mt_placeholder="@string/search_placeholder" />

Solution

  • First moment:

    this library not support font-familly. This question exists in the github block "ISSUE"

    enter image description here


    Second moment:

    padding for you text not support like and "disable boldness"


    But you can solve your problems with reflection

    this is not the best solution but it works

        searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
        try {
            final Field placeHolder = searchBar.getClass().getDeclaredField("placeHolder");
            placeHolder.setAccessible(true);
            final TextView textView = (TextView) placeHolder.get(searchBar);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    
            // fix trouble number 1 paddingLeft
            textView.setPadding(0, 0, 0, 0);
            final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
            layoutParams.setMargins(0,0,0,0);
    
            //fix trouble number 2 font-family and number 3 Finally, disable boldness?
            Typeface typeface = Typeface.createFromAsset(getAssets(), getString(R.string.roboto_medium));
            //<string name="roboto_medium">fonts/Roboto-Medium.ttf</string>
            textView.setTypeface(typeface);
    
            textView.setText("Hello World!");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    

    enter image description here

    Burger menu have size 48dp it is not margins(DeclaredField = navIcon) enter image description here


    Last variant

    copy this library in you project and edited all you want .
    you can do it because this library used license MIT most importantly specify the original author