I have wrote a class that manage my costume navigation view in this class we have an onItemClickListener that every class use this class must implement it to listen for item clicks. Navigation view loads without problem, and initializeComponents do its work without any exception but when i click an item just nothing heppens. what is my mistake in this code:

    public class NavigationMenuHelper extends ViewGroup implements 
    View.OnClickListener {

    //region Constants
    public static final int MENU_SETTINGS = R.id.btnSettings;
    public static final int MENU_SUPPORT = R.id.btnSupport;
    public static final int MENU_SANDUGH = R.id.btnSandugh;
    public static final int MENU_TRANSFER = R.id.btnTransfer;
    public static final int MENU_INS_PAYMENT = R.id.btnInsPayment;
    public static final int MENU_TUTORIAL = R.id.btnTutorial;
    public static final int MENU_LOST_CARD = R.id.btnLostCard;
    //endregion

    private Activity activity;
    private LayoutInflater inflater;
    private View container;
    private ViewGroup viewGroup;

    //region Components
    private ImageButton btnSettings;
    private ImageButton btnSupport;
    private ImageButton btnSandugh;
    private ImageButton btnTransfer;
    private ImageButton btnInsPayment;
    private ImageButton btnTutorial;
    private ImageButton btnLostCard;
    //endregion


    public NavigationMenuHelper(Context context) {
        super(context);
        activity = (Activity) context;
        if (!(activity instanceof OnMenuItemsClickListener))
            throw new ClassCastException(activity +
                    " must implement NavigationMenuHelper.OnMenuItemsClickListener.");
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        container = inflater.inflate(R.layout.navigation_drawer_layout, this,true);
        initializeComponents(container);
    }

    private void initializeComponents(View container) {
        // getting view objects
        btnSettings = (ImageButton) container.findViewById(R.id.btnSettings);
        btnSupport = (ImageButton) container.findViewById(R.id.btnSupport);
        btnSandugh = (ImageButton) container.findViewById(R.id.btnSandugh);
        btnTransfer = (ImageButton) container.findViewById(R.id.btnTransfer);
        btnInsPayment = (ImageButton) container.findViewById(R.id.btnInsPayment);
        btnTutorial = (ImageButton) container.findViewById(R.id.btnTutorial);
        btnLostCard = (ImageButton) container.findViewById(R.id.btnLostCard);
        // set onClickListener
        btnSettings.setOnClickListener(this);
        btnSupport.setOnClickListener(this);
        btnSandugh.setOnClickListener(this);
        btnTransfer.setOnClickListener(this);
        btnInsPayment.setOnClickListener(this);
        btnTutorial.setOnClickListener(this);
        btnLostCard.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        ((OnMenuItemsClickListener) activity).onClickMenuItems(v.getId());
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    public interface OnMenuItemsClickListener {
        void onClickMenuItems(int id);
    }
}

my Navigation layout addded to a DrawerLayout and this is my Navigation Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lcGray"
    android:layoutDirection="rtl"
    android:orientation="vertical"
    android:paddingRight="30dp"
    android:paddingLeft="35dp"
    android:paddingBottom="30dp">

    <LinearLayout 
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:orientation="vertical">

        <ImageButton android:id="@+id/btnSettings"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:background="@drawable/shape_circle"
            android:src="@drawable/ic_settings" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="تنظیمات"
            android:textColor="@color/milky"
            android:textStyle="bold" />
    </LinearLayout>
    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_weight="4"
        android:columnCount="3"
        android:rowCount="2"
        android:orientation="horizontal">



        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton android:id="@+id/btnSupport"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@drawable/shape_circle"
                android:padding="7dp"
                android:src="@drawable/ic_support" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="پشتیبانی"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton android:id="@+id/btnSandugh"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@drawable/shape_circle"
                android:padding="7dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="صندوق المهدی"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>
    </GridLayout>

    <View style="@style/HDivider" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="25dp"
        android:layout_weight="4"
        android:columnCount="3"
        android:orientation="horizontal"
        android:rowCount="2">

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton android:id="@+id/btnTransfer"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@null"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="انتقال وجه"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton android:id="@+id/btnInsPayment"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@null"
                android:src="@drawable/ic_installment" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="پرداخت قسط"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton android:id="@+id/btnTutorial"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@null"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="آموزش"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/btnLostCard"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@null"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="مفقودی کارت"
                android:textColor="@color/milky"
                android:textStyle="bold" />
        </LinearLayout>

    </GridLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@mipmap/ic_launcher"
        android:gravity="center|right"
        android:text="صندوق المهدی"
        android:textColor="@color/milky" />

</LinearLayout>

I used this class like this:

public class MainActivity extends AppCompatActivity implements 
    NavigationMenuHelper.OnMenuItemsClickListener {
        private NavigationMenuHelper navigationMenuHelper;
        private void initialization() {
                navigationMenuHelper = new NavigationMenuHelper(this);
            }
@Override
    public void onClickMenuItems(int id) {
        String str = "";
        switch (id){
            case NavigationMenuHelper.MENU_SETTINGS:
                str = "Settings";
                break;
            case NavigationMenuHelper.MENU_SUPPORT:
                str = "Support";
                break;
            case NavigationMenuHelper.MENU_SANDUGH:
                str = "Sandugh";
                break;
            case NavigationMenuHelper.MENU_TRANSFER:
                str = "Transfer";
                break;
            case NavigationMenuHelper.MENU_INS_PAYMENT:
                str = "InsPayment";
                break;
            case NavigationMenuHelper.MENU_TUTORIAL:
                str = "Tutorial";
                break;
            case NavigationMenuHelper.MENU_LOST_CARD:
                str = "LostCard";
                break;
        }
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
    }
upvote
  flag
Welcome to SO! Please provide a minimal working example to make it easier for others to reproduce your problem and to help you. – Murmel

1 Answers 11

I figured it out that Activity that create NavigationMenuHelper, already inflated navigation layout so I have to use activity findViweById method to initializing my objects. I set an Id for my navigation view main LinearLayout to check if activity have inflated layout correctly.

<LinearLayout android:id="@+id/navigationView"...

so this is my NavigationMenuHelper changes:

public NavigationMenuHelper(Context context) throws Exception {
    activity = (Activity) context;
    if(activity.findViewById(R.id.navigationView) == null) {
        Log.e(TAG,activity.getLocalClassName()+" does not contain navigationView");
        throw new Exception("Navigation view not found!");
    }
    if (!(activity instanceof OnMenuItemsClickListener))
        throw new ClassCastException(activity +
                " must implement NavigationMenuHelper.OnMenuItemsClickListener.");
    initializeComponents(activity);
}

private void initializeComponents(Activity activity) {
    // getting view objects
    btnSettings = (ImageButton) activity.findViewById(R.id.btnSettings);
    btnSupport = (ImageButton) activity.findViewById(R.id.btnSupport);
    btnSandugh = (ImageButton) activity.findViewById(R.id.btnSandugh);
    btnTransfer = (ImageButton) activity.findViewById(R.id.btnTransfer);
    btnInsPayment = (ImageButton) activity.findViewById(R.id.btnInsPayment);
    btnTutorial = (ImageButton) activity.findViewById(R.id.btnTutorial);
    btnLostCard = (ImageButton) activity.findViewById(R.id.btnLostCard);
    // set onClickListener
    btnSettings.setOnClickListener(this);
    btnSettings.setOnTouchListener(this);
    btnSupport.setOnClickListener(this);
    btnSandugh.setOnClickListener(this);
    btnTransfer.setOnClickListener(this);
    btnInsPayment.setOnClickListener(this);
    btnTutorial.setOnClickListener(this);
    btnLostCard.setOnClickListener(this);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.