0

I'm trying display a Relative Layout based on a checkbox preference. Right now, no matter what state the checkbox is in, the layout is gone.

final RelativeLayout unreadCount = (RelativeLayout) findViewById(R.id.header_navigation);
            if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, false)){
                unreadCount.setVisibility(View.VISIBLE);
            }else{
                if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, true)){
                unreadCount.setVisibility(View.GONE);
            }
        }

<RelativeLayout
    android:id="@+id/header_navigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:padding="2dp">
    <Button
        android:id="@+id/previous_button" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hapticFeedbackEnabled="true"
        android:background="@drawable/btn_prev_arrow" />
    <TextView
        android:id="@+id/notification_count_text_view"
        android:layout_height="50dip"  
        android:layout_width="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/previous_button" 
        android:layout_toLeftOf="@+id/next_button"
        android:gravity="center"
        android:focusable="true" 
        android:clickable="true"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textColor="@color/white"
        android:text="0/0"/>
    <Button
        android:id="@+id/next_button" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:hapticFeedbackEnabled="true"
        android:background="@drawable/btn_next_arrow"/>
</RelativeLayout>
2
  • 1
    Have you put your code into debug to make sure it's making it into the if statement at all? Also, what's the visibility attribute set as in the XML? Commented Aug 24, 2011 at 22:56
  • @hooked82 My if statement working and I've tried adding a visibility attribute set to "visible" in my layout, but nothing changed. Commented Aug 24, 2011 at 23:18

1 Answer 1

2

Your code is flawed - no matter what the checkbox state is, the unreadCount.setVisibility(View.GONE); line will never be run. If the checkbox is checked, the beginning section of the if statement will be run. If it is not checked, the second nested if will skip over that line. You need to take out the second nested if:

final RelativeLayout unreadCount = (RelativeLayout) findViewById(R.id.header_navigation);

if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, false)){
   unreadCount.setVisibility(View.VISIBLE);
}else{
   unreadCount.setVisibility(View.GONE);
}

Aside from this snippet, there is probably a flaw in your layout that is causing the view not to be shown. Try testing it with a setVisibility(View.VISIBLE) line outside of any conditional statements to see if it shows, then I'd check your layout or post it here.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried adding "android:visibility="visible" under my Relative Layout bracket and nothing changed. If I remove View.GONE altogether the layout shows, so I'm not sure where my problem lies.
Is there anywhere else you set the visibility of your view, or is the code you posted different than the code you have? It shouldn't matter what you do to the View.GONE line because that line will never be run. If you have updated to use my code above, then it must mean that either the preference for UNREAD_COUNT_ENABLED_KEY is unchecked, UNREAD_COUNT_ENABLED_KEY is using the wrong key, or the preference is not a checkbox preference.
I figured it out. I hadn't added my relative layout as a property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.