0

Im willing to use a Button in the main layout to move into a new layout when I click it and I did read about it but when I tried myself something didn't work properly. It gives me one error on the setContentView (error: non-static method setContentView(int) cannot be referenced from a static context). Did I miss something or I'm doing it wrong ?

Since I already have a class for ChatController, I thought about just moving my bluetooth chat "terminal" into a new layout that I could access through a button to make things clean. Also that onClickListener is on my MainActivity

    //show bluetooth chat layout when click chat button
    btnChat.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            MainActivity.setContentView(R.layout.layout_chat);
        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Predule
  • 27
  • 7
  • Possible duplicate of ["Non-static method cannot be referenced from a static context" error](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – m0skit0 Feb 11 '19 at 17:48

1 Answers1

0

If you have ChatController class you can use intent to move to it.

Use this wayIntent intent = new Intent(MainActivity.this,ChatController.class); startActivity(intent);

  //show bluetooth chat layout when click chat button
btnChat.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        Intent intent = new Intent(MainActivity.this,ChatController.class);
        startActivity(intent);
    }
});
Ahmed Rashid
  • 29
  • 2
  • 11
  • But the ChatController class isn't "binded" with the new layout so...wouldn't I still have the same problem of how to change to the new layout ? – Predule Feb 11 '19 at 18:18
  • So are you mean you want to replace 'activity_main' to new layout? – Ahmed Rashid Feb 11 '19 at 18:38
  • Yes, in the activity_main I'll put some functions and the chat button. This button I want him when clicked to change to a new playout (layout_chat) where the chat features will be done – Predule Feb 11 '19 at 18:43
  • Sorry, But i know 2 ways to change layout. The 1st way it's using 'Intent' and the 2nd way it's using 'Fragment', I hope those way helps you. – Ahmed Rashid Feb 11 '19 at 19:06