javaandroidandroid-layout

Multiple buttons in single page navigating each button to different page in android studio


Please solve this problem in android studio, I am new to app development. I want to create 3 buttons in a single page and navigate each button to each different page. I need code for java i.e "Mainactivity.java" I have declared 3 button id's I have set everything in app manifest. I am able to navigate only single button at once but how can I arrange all three buttons for navigation?

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

    Button buttonWRGL = (Button)findViewById(R.id.buttonWRGL);
    Button buttonHNK = (Button)findViewById(R.id.buttonHNK);
    Button buttonKZP = (Button)findViewById(R.id.buttonKZP);

    buttonWRGL.setOnClickListener(this);
    buttonHNK.setOnClickListener(this);
    buttonKZP.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.buttonWRGL:

            break;
        case R.id.buttonHNK:
            break;
        case R.id.buttonKZP:
            break;
    }
}

}


Solution

  • Your question seems unclear, but I suppose that you are asking how to load another layout/activity/fragment by clicking on a button.

    Well, it depends on which of the three actions you want to do:

    1. in order to load another layout, you need to inflate the new layout in your view; in order to do that you need to clear the actual layout and inflate the new one. Here's some sample code:

      //you may change it to whichever layout you used LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);

      //remove previous view ll.removeAllViews();

      //set the new view setContentView(R.layout.new_layout);

    2. in case you want to start a new activity, you need to use a Intent and load it. Sample code:

      //create the new intent; it will refer to the new activity Intent intent = new Intent(this, NewActivity.class);

      //pass any data to the new activity; cancel this line if you don't need it intent.putExtra(extra_title, extra)

      //start the new activity startActivity(intent);

    3. in case you want to change fragment, you need to perform a transaction. Sample code:

      //create the new fragment Fragment newFragment = new MyFragment();

      //start a fragment transaction FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

      //replace the old fragment with the new transaction.replace(R.id.frame, newFragment).commit();

    Hope this helps; if not, try to edit your question in order to clarify what you mean.


    EDIT:

    You should add a new OnClickListener to each button, but I would do it differently than you are doing right now. I would do something like this sample code:

    buttonWRGL.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(this, NewActivity1.class);
            startActivity(intent);
        }
    });
    

    For each button. In this code, I directly attach a specific OnClickListener to the button; it will contain the intent that you need. You can copy this in every button, even 10k buttons, you just need to change the activity name inside the intent declaration with the activity that you want to launch.