0

I try to make smth like this:

введите сюда описание изображения

To obtain this result I use this code:

I have my NavigationView

<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_second"
    app:menu="@menu/activity_third_drawer" />

I have menu XML file:

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">

    <item
        android:id="@+id/nav_inbox"
        android:icon="@drawable/ic_mail_outline_white"
        android:title="Inbox"
        app:actionLayout="@layout/badge" />
</group>

<item android:title="@string/about_app">
    <menu>
        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_close_24dp"
            android:title="@string/about1" />
    </menu>
</item>
</menu>

And I have my badge XML file:

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

<TextView
    android:id="@+id/textMenuItemCount"
    android:layout_width="wrap_content"
    android:text="rrrr"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:textColor="@android:color/holo_blue_light"
    android:textStyle="bold"
    android:textSize="16sp"
    android:alpha="0.6"/>
</RelativeLayout>

Then I try to set 2 to badge in my MainActivity:

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

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    MenuItem item = navigationView.getMenu().findItem(R.id.nav_inbox);
    MenuItemCompat.setActionView(item, R.layout.badge);
    RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
    TextView tv = (TextView) notifCount.findViewById(R.id.textMenuItemCount);
    tv.setText("2");
}

And eventually I get nothing.

enter image description here

I have tried to debug and it show me all is ok.

enter image description here

Please help me. What am I doing wrong?

codewitharefin
  • 1,398
  • 15
  • 24
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • verify visibility please. – Sohail Zahid Jul 11 '16 at 10:59
  • @SohailZahid how i can do it? – Sirop4ik Jul 11 '16 at 11:11
  • after `setText("asdasd");` also set `setVisibility(0);` – Sohail Zahid Jul 11 '16 at 11:12
  • Possible duplicate of [How to create a custom navigation drawer in android](http://stackoverflow.com/questions/21796209/how-to-create-a-custom-navigation-drawer-in-android) – Kushan Jul 11 '16 at 11:56
  • You can probably make a Fragment which looks similar to the navigation view, add a views to make a header and a list view for contents.Set this as your navigation views layout and you can be good to go. – Kushan Jul 11 '16 at 11:58
  • @SohailZahid no it doesn't help me(( – Sirop4ik Jul 11 '16 at 12:02
  • @Kushan i'm going to do this if won't get result with regular way – Sirop4ik Jul 11 '16 at 12:04
  • @AlekseyTimoshchenko why didt you use prebuilt library for this.there are ton libs outside there. – Sohail Zahid Jul 11 '16 at 12:04
  • @AlekseyTimoshchenko I am telling you from personal experience. If you need customizations, Do Not Waste time on using NavigationView. Almost always, it ends up being a time waste. Follow the links i have given. They give two different approaches to handle this issue. I personally find using a fragment much less restricting than customizing every adapter on earth to make the navigationview work :) – Kushan Jul 11 '16 at 12:13

3 Answers3

0

Easiest option for you would be to provide a Fragment with the custom layout as the second child for your DrawerLayout instead of your NavigationView. It will basically look and work like a NavigationView but instead can give you a lot more customizations.

Follow Slidenerd Tutorial for this approach. Watch all videos to get total understanding :) :

https://www.youtube.com/watch?v=zWpEh9k8i7Q

If you still want to stick to using a NavigationView, check out the Duplicate Question which is basically what you have asked. It gives another approach.

How to create a custom navigation drawer in android

Community
  • 1
  • 1
Kushan
  • 5,855
  • 3
  • 31
  • 45
0

Try using something like this.

  TextView tv = (TextView) navigationView.getMenu().findItem(R.id.nav_inbox).getActionView().findViewById((R.id.textMenuItemCount);
Tanveer Bulsari
  • 213
  • 1
  • 10
0

Do not find and manipulate views in onCreate , onCreate is called before onCreateView . Instead you should reference all your view's from onResume witch is called after the layout is properly inflated. Or you sould have the view setup before you passe it to onCreate .

Mihai
  • 420
  • 6
  • 9
  • Acctually i have tryed invoke this method after i opened `NavigationDrawer`. It means that `view` 100% had created, then i opened it and click button to set value and nothing – Sirop4ik Jul 14 '16 at 11:19