Question

I'm trying to add a ViewSwitcher inside a Customized PopupWindow, the problem comes up when the showNext() method is executed It happens nothing, the same first view is showed.

This is the xml layout:

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyt_popup_arm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/popup_padding">

    <RelativeLayout
        android:id="@+id/lyt_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/popup_icon_contentdesc"
            android:layout_marginLeft="@dimen/popup_spinner_left_padding"
            android:layout_marginRight="@dimen/popup_spinner_right_padding"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/spinner"
            android:textColor="#FFFFFF"
            android:textSize="@dimen/popup_text_size" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/lyt_result" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/menu_annex"
            android:contentDescription="@string/popup_icon_contentdesc" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/icon" />

    </RelativeLayout>

</ViewSwitcher>

And the java Code is below:

public class PopupProgressArm extends PopupProgress {
    protected ViewSwitcher vSwitcher;
    protected View v1;
    protected View v2;

    public PopupProgressArm(Context context) {
        super(context);
    }

    public PopupProgressArm(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init() {
        baseView = LayoutInflater.from(context).inflate(R.layout.popup_arm, null);
        setContentView(baseView);
        initElements();
        vSwitcher = (ViewSwitcher) baseView.getRootView();
        v1 = vSwitcher.findViewById(R.id.lyt_progress);
        v1.setTag("V1");
        v2 = vSwitcher.findViewById(R.id.lyt_result);
        v2.setTag("V2");
    }

    public void setResult(String message, int icon) {
        Log.d("CheckerService", (String) vSwitcher.getCurrentView().getTag());
        Log.d("CheckerService", "" + !((String) vSwitcher.getCurrentView().getTag()).equals((String) v2.getTag()));

        if (!((String) vSwitcher.getCurrentView().getTag()).equals((String) v2.getTag())) {
            Log.d("CheckerService", "Setting the result");
            Button btn = (Button) v2.findViewById(R.id.btn);
            TextView txtMessage = (TextView) v2.findViewById(R.id.message);
            ImageView image = (ImageView) v2.findViewById(R.id.icon);

            txtMessage.setText(message); 
            image.setImageResource(icon);
            btn.setText(context.getResources().getString(R.string.accept));
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupProgressArm.this.dismiss();
                }
            });
            vSwitcher.showNext();
        }
    }
}

The loggers added in code are drawn in the logcat console then the execution is correct.

If anyone knows the solution It would be a nice to know it.

Thanks in advance!

Was it helpful?

Solution

Well after a long research, finally I found the problem what was happened.

It has nothing to do with the code mentioned in the question, the real problem was when I was trying to call the showNext() method of the ViewSwitcher from a new Runnable() inside an ScheduledExecutorService object, for that reason the code never was executed.

So the lesson here is all code we want to run the UI Thread can't be executed outside the same Thread.

Hope this helps!

Regards!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top