Changing the Search Back icon?

I am using AppCompat action bar and I want to change the reverse search icon I am using in the action bar. I have searched but I could not find a solution.

I tried to set the icon in style:

<item name="homeAsUpIndicator">@drawable/myBackButton</item>

but I want to set another programmatically when the user selects the search type. Any ideas on how I can accomplish this?

+3


source to share


3 answers


To customize the "up" SearchView icon, you can use the style attribute:

collapseIcon="@drawable/ic_discard_icon"

      

in toolbar layout:



<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:collapseIcon="@drawable/ic_discard_icon"/>

      

Or you can move this attribute to the toolbar style. As you wish.

+3


source


You can change the background of the icon on the back as shown below.



Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initToolbar();
}

private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.tbMaster);
    setSupportActionBar(toolbar);
    if (toolbar != null) {
        toolbar.setBackgroundColor(getResources().getColor(R.color.base_color));
        getSupportActionBar().setTitle("Title");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.image);
    }
}

      

0


source


Try this piece of code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    // get the parent view of home (app icon) imageview
    ViewGroup home = (ViewGroup) findViewById(android.R.id.home).getParent();
    // get the first child (up imageview)
    ( (ImageView) home.getChildAt(0) )
        // change the icon according to your needs
        .setImageDrawable(getResources().getDrawable(R.drawable.custom_icon_up));
} else {
    // get the up imageview directly with R.id.up
    ( (ImageView) findViewById(R.id.up) )
        .setImageDrawable(getResources().getDrawable(R.drawable.custom_icon_up));
} 

      

0


source







All Articles