cosmicsafari
cosmicsafari

Reputation: 4049

Android App Development : How do change content on screen depending on event

Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.

Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.

So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.

If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?

Thanks.

Upvotes: 0

Views: 381

Answers (4)

apatel
apatel

Reputation: 607

Let me explain you this in simple terms.

In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.

And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.

Now let's go to your question about switching between multiple pages.

Suppose you have 2 activities: MainActivity and SecondActivity.

Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.

android:onClick="goToSecond"

Now in MainActivity.java you need to create a method which looks like this.


public void goToSecond(View v)
{
        Intent i=new Intent(MainActivity.this,SecondActivity.class);

startActivity(i);

}

This is a code snippet for switching to second activity.

And I also agree with other answers that you should check out developers.android.com

Hope it helps.

Upvotes: 1

SKK
SKK

Reputation: 5271

There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.

Upvotes: 0

Akash Shah
Akash Shah

Reputation: 132

Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.

Upvotes: 2

userM1433372
userM1433372

Reputation: 5497

I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html

Upvotes: 3

Related Questions