Android: performClick() on a Button inside a ViewFlipper?

88 views Asked by At

I have an Activity method "finishCardUpdateAndClose()" that is called after a Fragment onDismiss(). In the method I am trying to simulate a click on a button on the Activity UI. The button is an element of a ViewFlipper. I try to set up a View for the ViewTreeObserver that reference's the button in the ViewFlipper using "getCurrentView()". This may be my problem.

No click happens. But the Toast before and the Toast after the ViewTreeObserver code are firing, so it appears as though the "finishCardUpdateAndClose()" is being called after the Fragment closes. The Toast within the ViewTreeObserver code in onGlobaLayout() is not firing. I also tried just using performClick() with the button and I also tried using an OnClickListener, with no luck.

What am I missing here with trying to performClick() on the button child of the ViewFlipper?

Activity code

private View saveButtonEdits;

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit); 

    viewFlipper = findViewById(R.id.viewFlipper1);
    *saveButtonEdits = viewFlipper.getCurrentView().findViewById(R.id.saveButtonRVEdits);*
}

@Override
public void finishCardUpdateAndClose() {

    Toast.makeText(AddorUpdateCardActivity.this,"finishCardUpdateAndClose run after Frag",Toast.LENGTH_LONG).show();

    ViewTreeObserver vto = saveButtonEdits.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {                
            saveButtonEdits.performClick();
            vto.removeOnGlobalLayoutListener(this);
            Toast.makeText(AddorUpdateCardActivity.this,"performClick",Toast.LENGTH_LONG).show();
        }
    });
    Toast.makeText(AddorUpdateCardActivity.this,"post-performClick!",Toast.LENGTH_LONG).show();
}

activity_edit.xml

...
<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="wrap_content"
    android:layout_height="44dp"
    android:layout_marginBottom="5dp"  >

    <include layout="@layout/cardview_nobuttons"
        android:id="@+id/cardviewNobuttons"  />

    <include layout="@layout/cardview_twobuttonsedits"
        android:id="@+id/cardviewTwobuttonsEdits" />
     ...
</ViewFlipper>

cardview_twobuttonsedits.xml

...    
<Button
    *android:id="@+id/saveButtonRVEdits"*
    android:text="Save"
    android:onClick="onClickSaveEdits"  />

Fragment

public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);        
    if(getActivity() !=null) {
        viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(getActivity().findViewById(R.id.cardviewTwobuttonsEdits)));
        mListener.finishCardUpdateAndClose();
    }
}
1

There are 1 answers

1
Hactieuho96 On

You should not using findViewById any more. Using databinding or viewbinding is faster and will solve your problem.