Search code examples
androidbottomnavigationview

Cannot resolve BottomNavigationView


Android Studio says it cannot resolve bottomnavigationview in my code. All BottomNavigationView text is red. Also, when I go to the xml layout page, instead of showing the bottom navigation view icons it shows 'android...BottomNavigationView' in a grey box. But when I run the app on my device, I see the icons. (Though it's a little squished)

I've tried rebuilding and cleaning the app code. I had this problem before, a few days ago, so I deleted the file and started over, and the bottom navigation problem was fine. But then whenever I quit android studio and reopen it, it gives me the issue.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_list:
                    mTextMessage.setText(R.string.title_list);
                    return true;
                case R.id.navigation_done:
                    mTextMessage.setText(R.string.title_done);
                    return true;
                case R.id.navigation_categories:
                    mTextMessage.setText(R.string.title_categories);
                    return true;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

I created this activity with the preset Bottom Navigation activity so I haven't tinkered with any code yet apart from changing the icons and icon names.


Solution

  • I hope this will work for you.

    In your app build.gradle file in dependencies tab add below line

    implementation 'com.android.support:design:26.1.0'
    

    Here 26.1.0 is library version instead of this add your library version and Sync Project.