Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

android - How to change the SearchView icon position from left to right

I have the following layout which contains a SearchView and i want its icon to be on the right side instead of the default left. I looked up multiple posts on here and their respective answers but none worked. Any ideas on how to do it? Is there an xml attribute i can add or do i have to do it programatically?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presenter.CultureActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/culture_toolbar"
        android:layout_width="match_parent"
        android:layout_height="145dp"
        android:background="@drawable/toolbar_gradient"
        android:elevation="4dp"
        android:paddingTop="40dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@android:color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="20dp">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top"
                android:layout_marginTop="10dp"
                android:fontFamily="@font/montserrat_medium"
                android:text="@string/toolbar_title_placeholder"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.SearchView
                android:id="@+id/searchBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top"
                android:layout_marginTop="5dp"
                android:background="@drawable/searchbar_rounded_bg"
                app:defaultQueryHint="@string/search_hint"
                app:iconifiedByDefault="false"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/toolbar_title" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.appcompat.widget.Toolbar>

    <!-- Implement the main layout -->

    <include
        android:id="@+id/shadow"
        layout="@layout/navbar_shadow"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <include
        android:id="@+id/bottom_nav_bar"
        layout="@layout/bottom_navbar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Edit: Result of Sumit Shukla's answer implementation: result

Expected result: res

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <item
            android:id="@+id/search"
            android:icon="@drawable/ic_search_white"
            android:title="@string/search"
            app:actionViewClass="androidx.appcompat.widget.SearchView"
            app:showAsAction="always|collapseActionView" />
</menu

YourActivity.java

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);  
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
            @Override
             public boolean onCreateOptionsMenu(Menu menu) {
                    getMenuInflater().inflate(R.menu.home_menu, menu);
                    MenuItem searchItem = menu.findItem(R.id.search);
                    SearchView searchView = (SearchView) searchItem.getActionView();
                    searchView.setOnQueryTextListener(this);
                    return super.onCreateOptionsMenu(menu);
              }

        //Override below methods:

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...