javaandroidandroid-studioswitch-statementsetcontentview

Android Studio Using Multiple Buttons with Switch


Okay so I would like to start out by saying that I currently only have about 16 hours into actually knowing java. So this may seem like a very easy question, but I can not find any answer on the internet. Like, anywhere. So here I am!

What I am trying to do is move from my MainActivity into a menu activity which can then jump to different screens. I have four activity_.xml files currently. activity_home.xml, activity_climbs.xml, activity_certifications.xml, activity_toggle_menu.xml.

The code starts with activity_home.xml. Once a "menu" button is pressed, the screen successfully switches into the activity_toggle_menu.xml. From there, I see three buttons, but none of them will work. I am including my code so that hopefully someone can point me in the right direction!

The switch that I am using in every example I could find used Toast to make something happen. I cannot for the life of me find anything for the setContentView function.

MainActivity.java:

package com.example.certtracker;                                   
                                                                   
import androidx.appcompat.app.AppCompatActivity;                   
                                                                   
import android.os.Bundle;                                          
import android.view.View;                                          
import android.widget.Button;                                      
                                                                   
public class MainActivity extends AppCompatActivity {              
                                                                   
    @Override                                                      
    protected void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState);                        
        setContentView(R.layout.activity_home);                    
                                                                   
                                                                   
       //Handles the Menu button on the home screen                
       Button menu = findViewById(R.id.menuButton);                
        menu.setOnClickListener(new View.OnClickListener() {       
            @Override                                              
            public void onClick(View view) {                       
                setContentView(R.layout.activity_toggle_menu);     
            }                                                      
        });                                                        
                                                                   
    }                                                              
}                                                                  

ToggleMenu.java

public class ToggleMenu extends AppCompatActivity implements View.OnClickListener {

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

    Button home = findViewById(R.id.homeScreen);
    Button cert = findViewById(R.id.cert_button);
    Button climb = findViewById(R.id.climb_button);

    home.setOnClickListener(this);
    cert.setOnClickListener(this);
    climb.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {

        case R.id.homeScreen:

            setContentView(R.layout.activity_home);

            break;
        case R.id.climb_button:

            setContentView(R.layout.activity_climbs);

            break;
        case R.id.cert_button:

            setContentView(R.layout.activity_certifications);

            break;

    }

   }
}

Solution

  • For changing views/designs - You need to implement the activities or fragments.

    You are new in Android app development using java, so my suggestion is that first you need to understand about activities.

    Please check this link for understanding about Activity in Android and check this link for Build Your First Android App in Java

    As ToggleMenu is activity in your code, then you can call intent to change the activity instead of calling setContentView() on click of menu button, like below code:

    //Handles the Menu button on the home screen                
           Button menu = findViewById(R.id.menuButton);                
            menu.setOnClickListener(new View.OnClickListener() {       
                @Override                                              
                public void onClick(View view) {   
                    startActivity(new Intent(MainActivity.this,ToggleMenu.class));     
                }                                                      
            });  
    

    For designs activity_home.xml, activity_climbs.xml, activity_certifications.xml, You need to create and call activities instead of calling setContentView().