Results 1 to 1 of 1
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why don't AppCompat v21 SearchView styles don't work in my app?

    I am trying to follow the "SearchView Widget" section in this blogpost to style my SearchView. I have added searchIcon, queryBackground and submitBackground values in my res/values-v21/styles.xml but the styles don't seem to apply:

    Why don't AppCompat v21 SearchView styles don't work in my app?-3.png

    Following is the image which was given to the searchIcon as its value:

    Why don't AppCompat v21 SearchView styles don't work in my app?-ic_account_circle_black_24dp.png

    I am posting the code of the SSCCE and also attaching the zipped eclipse project, in case somebody will like to try it.

    SSCCE

    res/values-v21/styles.xml

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="AppTheme" parent="AppBaseTheme">
            <!-- <item name="android:colorPrimary">@color/primaryColor</item>
            <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
            <item name="android:colorAccent">@color/accentColor</item>-->
    
            <item name="searchViewStyle">@style/CustomSearchViewStyle</item>
        </style>
    
        <style name="CustomSearchViewStyle" parent="Widget.AppCompat.SearchView">
            <!-- Search button icon -->
            <item name="searchIcon">@drawable/ic_account_circle_black_24dp</item>
            <!-- Background for the search query section (e.g. EditText) -->
            <item name="queryBackground">@color/searchViewQueryBackground</item> <!-- Lime 100 -->
            <!-- Background for the actions section (e.g. voice, submit) -->
            <item name="submitBackground">@color/searchViewSubmitBackground</item> <!-- Brown 100 -->
        </style>
    </resources>
    res/values/styles.xml

    Java Code:
    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
        <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
    
            <item name="colorPrimary">@color/primaryColor</item>
            <item name="colorPrimaryDark">@color/primaryColorDark</item>
            <item name="colorAccent">@color/accentColor</item>
        </style>
    
    
        <style name="AppTheme" parent="AppBaseTheme">
        </style>
    
    
        <style name="CustomToolbarTheme" parent="AppBaseTheme">
            <item name="android:textColorPrimary">#FFFFFF</item>
            <item name="android:textColorSecondary">#FFA400</item>
        </style>
    
        <style name="CustomToolbarPopupTheme" parent="AppBaseTheme">
            <item name="android:background">#1976D2</item>
        </style>
    
    </resources>
    res/values/colors.xml

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="primaryColor">#2196F3</color> <!-- Blue 500 -->
        <color name="primaryColorDark">#1976D2</color> <!-- Blue 700 -->
        <color name="accentColor">#C51162</color> <!-- Maroon Dark  -->
    
        <color name="searchViewQueryBackground">#F0F4C3</color><!-- Lime 100 -->
        <color name="searchViewSubmitBackground">#D7CCC8</color><!-- Brown 100 -->
    </resources>
    activity_main.xml

    Java Code:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="practice_projects.material_design_google_now_like_searchbox_four.MainActivity" >
    
        <include android:id="@+id/mainActivity_toolBar"
            layout="@layout/app_bar" />
    
        <TextView
            android:layout_below="@id/mainActivity_toolBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </RelativeLayout>
    app_bar.xml

    Java Code:
    <?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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/CustomToolbarTheme"
        app:popupTheme="@style/CustomToolbarPopupTheme" >
    
        <include android:id="@+id/appBar_searchBox"
            layout="@layout/search_box" />
    
    </android.support.v7.widget.Toolbar>
    search_box.xml

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/searchHint"
        app:iconifiedByDefault="false" >
    
    
    </android.support.v7.widget.SearchView>
    MainActivity.java

    Java Code:
    public class MainActivity extends AppCompatActivity {
        private Toolbar toolbar;
    
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.mainActivity_toolBar);
    
            setSupportActionBar(toolbar);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(R.color.primaryColorDark)); 
            }
    
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources() 
                    .getColor(R.color.primaryColor)));
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            return super.onOptionsItemSelected(item);
        }
    }
    Attached Files Attached Files

Similar Threads

  1. Replies: 0
    Last Post: 08-07-2015, 12:19 PM
  2. Styles of Programming...
    By CuppaCoffee in forum Forum Lobby
    Replies: 10
    Last Post: 06-14-2012, 10:57 PM
  3. JTextPane Styles Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:48 PM
  4. Java2D Line Styles
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:05 PM
  5. JOptionPane - message styles
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •