2

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" />
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70

2 Answers2

3

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

Community
  • 1
  • 1
serg3z
  • 1,852
  • 12
  • 28
  • Tahnk you very much for help! I have 2 questions: a) How could I use my own `R.font.` families instead of `Typeface.SANS_SERIF`? and b) The padding set with `textView.setPadding(0, 0, 0, 0);` doesn't delete the space between the burger menu and the `TextView`: maybe it's the burger menu that owns some `padding-right`: how could I be sure of that and how could I find the correspondig `DeclaredField` to set its `padding-right` to 0? – JarsOfJam-Scheduler Oct 10 '18 at 20:18
  • 1
    @JarsOfJam-Scheduler i fixed my answer check it out. – serg3z Oct 10 '18 at 20:44
  • I edited the title of my question to be more pertinent and indexable by search engines. – JarsOfJam-Scheduler Oct 11 '18 at 19:46
  • Do you know if it's possible to replace ` Typeface typeface = Typeface.createFromAsset(Objects.requireNonNull(getActivity()).getAssets(), "fonts/newscycle_regular.ttf");` with something directly using `R.font.`? – JarsOfJam-Scheduler Oct 11 '18 at 19:52
  • 1
    NB: `Typeface typeface = ResourcesCompat.getFont(Objects.requireNonNull(getContext()), R.font.XYZ);` is an alternative to your call `Typeface typeface = Typeface.createFromAsset(Objects.requireNonNull(getActivity()).getAssets(), "fonts/newscycle_regular.ttf");` :-). Cf. https://stackoverflow.com/questions/48688026/create-typeface-from-font-resource-id – JarsOfJam-Scheduler Oct 11 '18 at 20:01
  • 1
    yes you can or [so](https://stackoverflow.com/questions/2562408/set-specific-font-in-a-styles-xml/52447866#52447866) – serg3z Oct 11 '18 at 20:12
  • I tested your code with a longer text, and it is cut very sooner than being close to the magnifying glass icon. Is it a problem of width of the text view? I tried to set it, but didn't seem to work. Do you have any idea thus? Is it the icon? – JarsOfJam-Scheduler Oct 11 '18 at 21:31
  • 1
    I think yes, the last picture in my answer shows areas of all fields and icons. – serg3z Oct 11 '18 at 21:48
  • 1
    It wasn't a problem of width. The issue was due to a too long sentence, badly cut by this API :-). Thank you again! – JarsOfJam-Scheduler Oct 12 '18 at 20:44
1

I solved same thing with this:

Typeface typeface = ResourcesCompat.getFont(Objects.requireNonNull(getContext()), R.font.XXX);
placeHolder = findViewById(R.id.mt_placeholder);
placeHolder.setTypeface(typeface);