Lewis Lebentz Lewis Lebentz - 1 year ago 60
Android Question

How do I add a new page to an Android app?

Apologies for the basic question, but I can't really find the answer online. Possibly because I'm not sure what I need to be searching for!

I have created a simple one page app following a guide. At the top there is a menu button, under that 'Settings'. I want to make it so that when the Settings button is clicked it takes you to a new settings page and allows the user to change the background colour, font colour or something simple like that.

However, I can't work out how to actually create a new page. I tried creating a new class and linking the intent under the action_settings bit in the main .java file, but that didn't work for me.

Please could someone give me some guidance?

GitHub repo: https://github.com/LewisLebentz/Quoter

Answer Source

If the settings button is on the action bar then follow these steps:

1) Create a new activity by right-clicking on the Java folder > New > Activity > Blank Activity. This will automatically create a layout file and a Java class. It will also automatically register the activity in the manifest.

2) Next you want to go to the Quoter.java file. Add the following code to it. @Override

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {

        // This code will start the new activity when the settings button is clicked on the bar at the top.
        Intent intent = new Intent(Quoter.this, newActivityName.class);
        startActivity(intent)
        return true;
    }

    return super.onOptionsItemSelected(item);
}

That's it! When you click the settings button, it should start a new activity! Let me know if it does not work!

By the way, you can search for "how to switch activity on button press in Android".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download