I have a fragment embedded in a layout. This fragment has a very simple view (basically just a styled button with its pressed state being programmatically set).
When I start a new activity from the activity hosting the fragment, and press BACK to go back to the original activity the button is still disabled, but is now showing its unpressed state.
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
class="com.mypackage.NavigationDrawerFragment"
android:gravity="bottom"
android:padding="5dp" />>
</RelativeLayout>
fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp" >
<Button
android:id="@+id/nav_home"
style="@style/navigation_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/button_home"
android:text="HOME"
android:textColor="@drawable/button_text" />
</RelativeLayout>
In the fragment class, I simply do the following:
- Disable the button
- Set the pressed state of the button to true (so the downstate of the drawable is shown)
My fragment class:
public class NavigationDrawerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawer = inflater.inflate(R.layout.fragment_navigation_drawer,
container, false);
mButtonHome = (Button) mDrawer.findViewById(R.id.nav_home);
mButtonHome.setEnabled(false);
mButtonHome.setPressed(true);
}
}
Any idea why the enabled state of the button is preserved, but the pressed state is not?
As a work around for this, I am now only disabling the button and have added set_enabled=”false” states to my drawables and colors. This state is identical to the “pressed” state.
drawable/button_home.xml
drawable/button_text.xml
This basically has the same effect as calling setEnabled(false) and setPressed(true) on a button.