0

I have been trying to implement the navigation view using drawer layout.It works very well in phones above API 21.But it gets distorted in devices running android below API 21.Please refer the screenshots attached.See how the item gets distorted background.

Also, I have integrated the required images for hdpi,mdpi,xhdpi,xxhdpi and xxxhdpi densities.For API 21 and above I have created a drawable-v21 directory and added the Vector assests for the menu items.Can someone please help me out with this issue?

Here is the MainActivity in which the navigationView is implemented.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    titleText = (TextView) toolbar.findViewById(R.id.titleText);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame, Fragment1.newInstance("Fragment1"));
    fragmentTransaction.commit();

    navigationView.getMenu().getItem(0).setChecked(true);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (id == R.id.nav_home) {
        fragmentTransaction.replace(R.id.frame, Fragment1.newInstance("Fragment1"));
        fragmentTransaction.commit();
        titleText.setText("Frag1");
    } else if (id == R.id.nav_violations) {
        fragmentTransaction.replace(R.id.frame, Fragment2.newInstance("Fragment2"));
        fragmentTransaction.commit();
        titleText.setText("Frag2");
    } else if (id == R.id.nav_rem_drive_time) {
        fragmentTransaction.replace(R.id.frame, Fragment3.newInstance("Fragment3"));
        fragmentTransaction.commit();
        titleText.setText("Frag3");

    } else if (id == R.id.nav_downloads) {
        fragmentTransaction.replace(R.id.frame, Fragment4.newInstance("Fragment4"));
        fragmentTransaction.commit();
        titleText.setText("Frag4");

    } else if (id == R.id.nav_logout) {
        Toast.makeText(MainActivity.this, "Logout clicked", Toast.LENGTH_LONG).show();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Here is the xml code as well :

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<!--app:itemBackground="@drawable/drawer_item_selector"
app:itemIconTint="@drawable/drawer_item"
app:itemTextColor="@drawable/drawer_item"-->

Android solutions
  • 111
  • 1
  • 1
  • 7

0 Answers0