• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Switching activities

Yathushan

Well-Known Member
Jan 3, 2010
205
7
London
Hi all,
I have just started on my first app. I was just wondering how can I move from one activity to another.
I have used onClickListeners. It does not seem to work for me even though I have pretty much followed every tutorial I can find step by step.

mainmenu.java
Code:
package com.transport.assistant;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class mainmenu extends Activity {
	/** Called when activity is initially created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		 // click-handlers for buttons
		Button underground = (Button) findViewById(R.id.ButtonM1);
		underground.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent myIntent = new Intent(view.getContext(), underground.class);
                		startActivityForResult(myIntent, 0);
			}
			
		});
	}
}

main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="@string/app_name">
    </TextView>
    
    <Button
    android:id="@+id/ButtonM1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Underground">
    </Button>
    
    <Button
    android:id="@+id/ButtonM2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Bus">
    </Button>
    
    <Button
    android:id="@+id/ButtonM3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/oyster_locations">
    </Button>
    
    <Button android:id="@+id/ButtonM4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/plan_journey">
    </Button>
    
    <Button
    android:id="@+id/ButtonM5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Settings">
    </Button>
    
</LinearLayout>

Any advise would be much appreciated!!
 
There shouldn't be any special code in the next activity, although, in the code you posted, you were launching for result, which is different than just launching the activity.
I have made many apps that launch new activities and the code I provided works just fine for me. I'm going to try to build a replica of what you've provided so far and see if I can get it working.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones