'How to change activity from an Expandable Menu button?

I've got a file called activity_demo.xml where I put an Expandable Menu with a button with the id "changeActivity". The Expandable Menu references the files:

  • DemoActivity.java / activity_demo.xml
  • DataModel.java
  • ItemAdapter.java
  • NestedAdapter.java
  • each_item.xml
  • nested_item.xml

I want to change activity_demo.xml to activity_demo2.xml from the "changeActivity" button that's inside the file nested_item.xml but I don't know how.

This is the code from DemoActivity.java

public class DemoActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private List<com.codingstuff.nestedexapandabler.DataModel> mList;
private ItemAdapter adapter;

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

    //Expandable Menu

    recyclerView = findViewById(R.id.main_recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mList = new ArrayList<>();

    //list1
    List<String> nestedList1 = new ArrayList<>();
    nestedList1.add("Subtitle");

    mList.add(new com.codingstuff.nestedexapandabler.DataModel(nestedList1 , "List title"));
    adapter = new ItemAdapter(mList);
    recyclerView.setAdapter(adapter);
}

And this is the code I want to use for the "changeActivity" button, but I don't know where to put it, since I've tried to use it inside DemoActivity.java but it doesn't work:

changeActivity backButton;
setContentView(R.layout.nested_item);
    changeActivity=(Button)findViewById(R.id.changeActivity);
    changeActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent packageContext;
            Intent i = new Intent(DemoActivity.this, DemoActivity2.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    });

I'm really sorry if I'm not being too clear about my problem. This is my first Android Studio project and I'm learning how to code it as I go. Let me know if there is any more info I can provide to help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source