Search code examples
javaandroidandroid-imagebuttonandroid-navigationview

How to set up navigation view with condition?


I have two navigation view for both side in android. As shown in the link image below, left side for user who signup as a parents meanwhile the right side for the user who signup as a tuition provider. For example, if User A sign up as a parents, so he/she can only open the left side navigation only. What coding should I write to make sure that he/she is the user for each navigation view. Can someone help or teach me please?

EXAMPLE OF MY INTERFACE

enter image description here

Im trying this coding:-

userCategory = (RadioGroup) findViewById(R.id.user_radio_group_btn);
    Integer checkedID = userCategory.getCheckedRadioButtonId();
    radioButton = (RadioButton) findViewById(checkedID);
    String selection = radioButton.getText().toString();

    if (selection.equals("Parents"))
    {
        menuRight.setVisibility(View.GONE);
        tuitionProvider.setVisibility(View.GONE);
    }

    if (selection.equals("Tuition Provider"))
    {
        menuLeft.setVisibility(View.GONE);
        parents.setVisibility(View.GONE);
    }

    else
    {
        Intent RegistrationIntent = new Intent(home.this, RegistrationActivity.class);
        startActivity(RegistrationIntent);
        Toast.makeText(this, "Please register first.", Toast.LENGTH_SHORT).show();
    }

XML CODING:-

 <RadioGroup
    android:id="@+id/user_radio_group_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/parents_radio_button"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="50dp"
        android:layout_marginTop="175dp"
        android:background="@drawable/roundedbutton"
        android:text="Parents" />

    <RadioButton
        android:id="@+id/tuition_provider_radio_button"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="35dp"
        android:layout_marginTop="175dp"
        android:background="@drawable/roundedbutton"
        android:text="Tuition Provider" />
</RadioGroup>

ERROR I GOT

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at com.addaj.mobilerecommendationsystem.home.onCreate(home.java:59)


Solution

  • Solution:

    Instead of:

    Integer checkedID = userCategory.getCheckedRadioButtonId();
    

    here, userCategoryis a RadioButton. Use this:

    Integer checkedID = (enter here your RadioGroupId).getCheckedRadioButtonId();
    

    Try if it works.