How to open new android XML page with button in Android?

10,089

Solution 1

Add below code in your main actiivty

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener (new View.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(getApplicationContext(),NewActivity.class);
        startActivity(i);
    }
});

Create a new activity name NewActivity.java in its onCreate method add the xml you want by using below statement.

setContentView(R.layout.page1);

Solution 2

what you're doing is fine with the setContentView(R.layout.page1);, but you need to set a button listener for that button:

Button myButton = (Button) findViewById(R.id.my_button);

myButton.setOnClickListener (new View.OnClickListener()
{
    public void onClick(View v)
    {
        setContentView(R.layout.page1);
    }
});
Share:
10,089
rzr418
Author by

rzr418

Updated on June 05, 2022

Comments

  • rzr418
    rzr418 almost 2 years

    I want to make a app that has buttons that open new XML pages for the user to view. I'm new to this and from the little I found, I think I need something like this but I'm not sure and could be wrong:

    Button myButton = (Button) findViewById(R.id.my_button);
    

    and

    setContentView(R.layout.page1);
    

    But I'm not sure of the imports and also if anyone knows on good tutorials that I could follow to learn more on coding Android apps.