4

I'm trying to change direction of SearchView in toolbar, and this is my try

layout.xml

android:layoutDirection="rtl"

menu.xml

<item android:id="@+id/action_search"
    android:title="@string/search_hint"
    android:icon="@mipmap/ic_search_icon"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

Java code:

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        getSupportActionBar().setCustomView(MenuItemCompat.getActionView(searchItem));

        mSearchView.setInputType(InputType.TYPE_CLASS_TEXT);
        mSearchView.setQueryHint(getString(R.string.search_hint));

        mSearchView.setGravity(Gravity.RIGHT);
        mSearchView.setTextDirection(View.TEXT_DIRECTION_RTL);    
        mSearchView.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);

        mSearchView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));

SearchView in actionBar Wrong position of closeButton

And this is the result, I success added the SearchView to toolbar and is RTL now. But the issue is position of "X" (closeButton) is wrong, the position must be at left.

a7md0
  • 429
  • 2
  • 10
  • 20
  • You can use custom layout for toolbar. [This may helpful to you](http://stackoverflow.com/questions/15518414/how-can-i-implement-custom-action-bar-with-custom-buttons-in-android) – Kaushali de Silva Oct 13 '16 at 07:21
  • @KaushalideSilva my issue is in SearchView not in the toolbar – a7md0 Oct 13 '16 at 09:27

4 Answers4

3

add this for change x direction:

    View xIcon = ((ViewGroup) mSearchView.getChildAt(0)).getChildAt(2);
    xIcon.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
1

To Support Rtl in your App. you have to set android:supportsRtl="true" in your manifest.xml.

And Most Important point keep in Mind

Change all of your app's "Left/Right" layout properties to Start/End.

In you case you are setting.

mSearchView.setGravity(Gravity.RIGHT);

so change it to

mSearchView.setGravity(Gravity.END);

and also change here this

mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));

to

 mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.END));
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • 1
    I've already add `android:supportsRtl="true"` to my manifest.xml, and I just replace `Gravity.RIGHT` to `Gravity.END`. But the same issue with x button position – a7md0 Oct 13 '16 at 07:49
1

You can use this method to move closeButton to right in RTL locales:

  private static void handleCloseButtonRtlIssue(SearchView searchView) {
        ((LinearLayout) ((LinearLayout) searchView.getChildAt(0)).getChildAt(2)).getChildAt(1).setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
    }
1

the problem is that when you change layoutDirection to rtl, toolbar itself will become rtl. but layoutDirection for other layouts inside toobar will not change. and editText with close button are in a nested layout.

so you have to find parent layout and change direction for that. if you see inside SearchView layout file. you'll find that exit button is inside a layout with search_plate id

so in onCreateOptionsMenu after setup SearchView do this

kotlin

mSearchView.findViewById<View>(R.id.search_plate)?.let {
    it.layoutDirection = View.LAYOUT_DIRECTION_RTL
}

java

((View) mSearchView.findViewById(R.id.search_plate))
    .setLayoutDirection(View.LAYOUT_DIRECTION_RTL)

Update

or you can use this kotlin extention for other rtl problem as well.

fun ViewGroup.rtl() {
    val stack = Stack<View>()
    stack.add(this)
    while (stack.isNotEmpty()) {
        stack.pop().apply {
            layoutDirection = View.LAYOUT_DIRECTION_RTL
            textDirection = View.TEXT_DIRECTION_RTL
            (this as? ViewGroup)?.children?.forEach { stack.add(it) }
        }
    }
}

use it like this

view.rtl() //and view become rtl :)
codegames
  • 1,651
  • 18
  • 16