Search code examples
androidandroid-layoutandroid-navigationnavigation-drawerandroid-navigationview

Can't put the actual image icon and can't attch the selector color in the Navigation View


I have a java class file and a XML file. In the XML file I have attached the icon resource and the title. I can't get the actual icon in the NavigationView. What I am getting is the grey icon which has not been declared in the resource-drawable folder.

I have to put the selector in the navigation Item which has been selected.

Here is my java code as well as the XML file

Mainactivity.java

public class MainActivity extends AppCompatActivity {
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        // Show menu icon

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        /**
         * Lets inflate the very first fragment
         * Here , we are inflating the NewsFragment as the first Fragment
         */

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();

//        mNavigationView.setBackgroundColor(Color.parseColor("#CFCFCF"));
        /**
         * Setup click events on the Navigation View Items.
         */

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();


                if (menuItem.getItemId() == R.id.nav_item_sent) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView, new SportsFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_inbox) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
                }

                if (menuItem.getItemId() == R.id.nav_item_sent){
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView, new VideosFragment()).commit();
                }

                if (menuItem.getItemId() == R.id.nav_item_draft) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView, new OpinionFragment()).commit();
                }

                if (menuItem.getItemId() == R.id.nav_item_sports) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView, new SportsFragment()).commit();
                }

                if (menuItem.getItemId() == R.id.nav_item_weather) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
                }

                return false;
            }

        });

        /**
         * Setup Drawer Toggle of the Toolbar
         */



        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
                R.string.app_name);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about_us:
                Intent intent = new Intent(MainActivity.this, AboutUs.class);
                startActivity(intent);
                return true;

            case R.id.action_terms_of_use:
                Intent intent_two = new Intent(MainActivity.this, TermsUse.class);
                startActivity(intent_two);
                return true;

            case R.id.action_privacy_policy:
                Intent intent_three = new Intent(MainActivity.this, PrivacyPolicy.class);
                startActivity(intent_three);
                return true;

            case R.id.action_contact_us:
                Intent intent_four = new Intent(MainActivity.this, ContactUs.class);
                startActivity(intent_four);

            case R.id.search:
                hidetext();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void hidetext() {

        Intent i = new Intent(MainActivity.this, SearchResultActivity.class);
        startActivity(i);
    }
}

drawermenu.xml

 <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">

        <group
            android:id="@+id/news"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_inbox"
                android:title="News"
                android:icon="@drawable/news"/>

        </group>

        <group
            android:id="@+id/opinion"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_draft"
                android:title="Opinion"
                android:icon="@drawable/opinion"/>
        </group>

        <group
            android:id="@+id/sports"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_sports"
                android:title="Sports"
                android:icon="@drawable/sports"/>
        </group>

        <group
            android:id="@+id/videos"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_sent"
                android:title="Video Gallery"
                android:icon="@drawable/videogallery"/>
        </group>

        <group
            android:id="@+id/weather"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_item_weather"
                android:title="Weather info"
                android:icon="@drawable/weather"/>
        </group>

        <group
            android:id="@+id/misc"
            android:checkableBehavior="single">

        </group>


    </menu>

activity_main.xml

<LinearLayout 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:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="SIKKIM EXPRESS" />

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <FrameLayout
            android:id="@+id/containerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></FrameLayout>


        <android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/shitstuff"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginTop="-24dp"
            app:itemTextColor="@color/black"
            app:itemBackground="@drawable/selected"
            app:menu="@menu/drawermenu" />


    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

Solution

  • By default navigation view use material design. To remove the grey color in navigation view icon's

    code

    mNavigationView.setItemIconTintList(null);