Home » Java » java – Android Studio use setOnTouchListener after changing layout-Exceptionshub

java – Android Studio use setOnTouchListener after changing layout-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I like to swipe right, left to switch the layout of my Activity. But after switching the layout once the setOnTouchListener stop working. What is my fault?

My Activity:

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    static Fragment fragment;
private int pagenumber;
private ConstraintLayout cl;
    private View factory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.instruction_1);


        cl = findViewById(R.id.layout);
        pagenumber= 0;


        cl.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

            public void onSwipeRight() {
                if (pagenumber != 0)
                    pagenumber = pagenumber - 1;
                if(pagenumber==0){
                    setContentView(R.layout.instruction_1);
                    factory = getLayoutInflater().inflate(R.layout.instruction_1, null);

                    cl = factory.findViewById(R.id.layout);
                }
              else if(pagenumber==1){
                    setContentView(R.layout.instruction_2);
                    factory = getLayoutInflater().inflate(R.layout.instruction_2, null);
                    cl = factory.findViewById(R.id.layout);
              }

            }
            public void onSwipeLeft() {
                if(pagenumber<2) {
                    pagenumber = pagenumber + 1;


                     if (pagenumber==1){
                         setContentView(R.layout.instruction_2);
                         factory = getLayoutInflater().inflate(R.layout.instruction_2, null);
                         cl = factory.findViewById(R.id.layout);
                    }
                    else if (pagenumber==2){
                         setContentView(R.layout.instruction_3);
                         factory = getLayoutInflater().inflate(R.layout.instruction_3, null);
                         cl = factory.findViewById(R.id.layout);
                    }
                }

            }


        });

    }
}
How to&Answers:

Why the app is not working after first swipe is bcz when you swipe once you are replacing the layout file with another when you still have reference to old Constrained layout which is no more now.

You are trying to implement the behavior of Fragments

You should not be replacing the layout file completely, instead create a Layout inside the xml file and replace it Or just use ViewPager.

I think you are looking for something like this.
Try it

 public class MainActivity extends AppCompatActivity {

static Fragment fragment;
private int pagenumber;
private ConstraintLayout cl;
private View factory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instruction_1);


    cl = findViewById(R.id.layout);
    pagenumber = 0;


    cl.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

        public void onSwipeRight() {
            if (pagenumber == 0) {
                setContentView(R.layout.instruction_1);
                pagenumber = 1;
            } else if (pagenumber == 1) {
                setContentView(R.layout.instruction_2);
                pagenumber = 2;
            } else if (pagenumber == 2) {
                setContentView(R.layout.instruction_3);
                pagenumber = 3;
            }
        }

        public void onSwipeLeft() {
           if (pagenumber == 1) {
                setContentView(R.layout.instruction_0);
                pagenumber = 0;
            } else if (pagenumber == 2) {
                setContentView(R.layout.instruction_1);
                pagenumber = 1;
            }else if (pagenumber == 3) {
                setContentView(R.layout.instruction_2);
                pagenumber = 2;
            }

        }
    });
}
}