[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Creating button on new android xml page

I am using the attached code to navigate from the main page to the "dashboard" page..Once I get there I am not sure how to call a new function or class so I can use buttons on that page.
public class TheBulldogsNation extends Activity {
	  /** Called when the activity is first created. */   
	@Override    public void onCreate(Bundle savedInstanceState) {        
		super.onCreate(savedInstanceState);        
		setContentView(R.layout.main);
		
		final Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			setContentView(R.layout.dashboard);
	
			
		}
		});
		
	
		
				
}
		
}

Open in new window

0
brad0525
Asked:
brad0525
  • 3
  • 2
1 Solution
 
InquisitiveProgrammerCommented:
You want the user click on the button to take them to a new activity/screen?

If this is the case, you want to create an intent. Make sure you import android.content.Intent.

For instance, if you have your file named "dashboard.java", you might have code that looks like this:

Intent intent = new Intent(TheBulldogsNation.this, Dashboard.class);
startActivity(intent);

0
 
InquisitiveProgrammerCommented:
You also want to delete the following line in your code:

setContentView(R.layout.dashboard); following the OnClickListener method for the button
0
 
brad0525Author Commented:
Below is the code I am using..I took your code above and added it..I am still getting a forceclose when I click the button ("button1"). Any ideas? Thanks
package com.examples.bulldogsnation;

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




public class TheBulldogsNation extends Activity {
	  /** Called when the activity is first created. */   
	@Override    public void onCreate(Bundle savedInstanceState) {        
		super.onCreate(savedInstanceState);        
		setContentView(R.layout.main);
        
        
		final Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(TheBulldogsNation.this, Dashboard.class);
			startActivity(intent);


		}
		});
		
	
		
				
}
		
}

Open in new window

0
 
brad0525Author Commented:
Figured out that  I needed to add

<activity android:name=".Dashboard"></activity>

into the mainfext...Now it works.
0
 
brad0525Author Commented:
The only other thing I had to do was add
<activity android:name=".Dashboard"></activity>

to the mainfest..Now it works..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now