3

In my main.xml layout, I have an <FrameLayout> element which is the fragment placeholder:

main.xml:

<FrameLayout
        android:id="@+id/fragment_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

I add Fragment programmatically to the above <FrameLayout> by:

fragmentTransaction.add(R.id.fragment_placeholder, fragment, null);

I can then use the replace() to change to other fragment:

fragmentTransaction.replace(R.id.fragment_placeholder, otherFragment, null);

At some point of my project, I need to get the current showing fragment, and disable everything on the view. I firstly successfully get the current showing fragment by :

Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder); 

Then, how can I disable the view of the fragment ? On the view, there could be buttons, is it possible to disable the whole view? If it is not possible, how can I add an overlay on the view?

I tried:

currentFragment.getView().setEnabled(false); 

But, it does not work, I can still click on buttons on the view.

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • 1
    check here http://stackoverflow.com/questions/5418510/disable-the-touch-events-for-all-the-views and here http://stackoverflow.com/questions/7068873/how-can-i-disable-all-views-inside-the-layout – Georgy Gobozov Aug 02 '12 at 10:51

2 Answers2

9

As per @Georgy's comment, here is a copy of the answer from Disable the touch events for all the views (credit to @peceps).


Here is a function for disabling all child views of some view group:

 /**
   * Enables/Disables all child views in a view group.
   * 
   * @param viewGroup the view group
   * @param enabled <code>true</code> to enable, <code>false</code> to disable
   * the views.
   */
  public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
      View view = viewGroup.getChildAt(i);
      view.setEnabled(enabled);
      if (view instanceof ViewGroup) {
        enableDisableViewGroup((ViewGroup) view, enabled);
      }
    }
  }

You can call this passing in your Fragment's view as retrieved by Fragment.getView(). Assuming that your fragment's view is a ViewGroup.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • Downside of this solution is that when a view is already disabled when disabling all views, it will be enabled when enabling all views. The function could return a (weak) list of all views that were disabled, so you can enable that list again, instead of enabling everything. – Marcel50506 Sep 09 '16 at 12:08
  • @Marcel absolutely agree it could be improved upon. But 4 years after posting, neither this, nor the original answer, have been improved on. If you'd like to provide an update along the lines you suggest, I'll be happy to vote it. – Richard Le Mesurier Sep 09 '16 at 14:27
1

Here's a Kotlin implementation with @Marcel's suggestion.

fun ViewGroup.enableDisableViewGroup(enabled: Boolean, affectedViews: MutableList<View>) {
    for (i in 0 until childCount) {
        val view = getChildAt(i)
        if (view.isEnabled != enabled) {
            view.isEnabled = enabled
            affectedViews.add(view)
        }

        (view as? ViewGroup)?.enableDisableViewGroup(enabled, affectedViews)
    }
}

fun MutableList<View>.restoreStateAndClear(enabled: Boolean) {
    forEach { view -> view.isEnabled = enabled }
    clear()
}
Eran Boudjnah
  • 1,228
  • 20
  • 22