Search code examples
androidandroid-toolbarandroid-search

Issues in displaying search icon


I am trying to display a search icon on the custom toolbar!

Problem : I had set the title of the toolbar to "Clubs List" while designing the toolbar, Now when I create a new menu.xml file to display the search icon on the toolbar, though the search icon is displayed on the right end side of toolbar but with the toolbar title("clubs list") even my app name in black color gets displayed on the left side as shown in the fig below("cric..")

I want only "CLUBS LIST" and search icon on the toolbar, How do I achieve this?

Here's my Toolbar.xml file enter image description here

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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:id="@+id/toolbarC"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/MyStyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="60dp"
    android:fontFamily="serif-monospace"
    android:text="CLUBS LIST"
    android:textColor="#FFF"
    android:textSize="30sp"
    android:textStyle="bold" />


</android.support.v7.widget.Toolbar>

Here's my main xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:layout="http://schemas.android.com/apk/res-auto"
android:background="#3dcc24">

 <include
    layout="@layout/clubs_tool_bar" />

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </ListView>

</LinearLayout>

Here's my menu xml file

 <?xml version="1.0" encoding="utf-8"?>
 <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:title="search"
    android:icon="@drawable/ic_menu_search"
    app:showAsAction="collapseActionView|always"
    android:actionViewClass="android.widget.SearchView"
    android:theme = "@style/MyStyle" />
  </menu>   

Here's my Java file

public class Clubs_List extends AppCompatActivity {
Toolbar toolbarC;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cricket_clubs);
    toolbarC = (Toolbar) findViewById(R.id.toolbarC);
    setSupportActionBar(toolbarC);
 }
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu,menu);
    return true;
}
}

Here's my styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" 
    parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="MyStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name = "android:textSize">30sp</item>



   </style>
</resources>

Solution

  • May i know what theme have you applied to your activity in your manifest?

    you might be giving wrong theme to your Activity to hide title.

       <style name="MyTheme" parent="Theme.Design.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        </style>
    

    In your Manifest give MyTheme to your Activity.

        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/MyTheme">
    

    in your activity

       getSupportActionBar().setDisplayShowTitleEnabled(false);