I want to know how I can obtain the value of a button and then use it on another activity to display content accordingly.
Example:
I have 3 buttons Image1, Image2 & Image3 on my MainActivity.
Now based on what button the user clicks (Image1, Image2 & Image3), a corresponding image is displayed on a new activity.
I know how to create these buttons, the new activity and also how to display an image on the new activity. How do I display the image based on what button the user clicks?
Your class should implement OnClickListener and override onClick()
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new OnClickListener(this));
button2.setOnClickListener(new OnClickListener(this));
button3.setOnClickListener(new OnClickListener(this));
#Override
public void onClick(View v){
switch(v.getId()) //get the id which is an int
{
case R.id.button1 : //if its button1 that is clicked
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
// use intents to pass information to secondActivity and display the image there
break;
case R.id.button2 :
Intent i= new Intent("com.example.secondActivity");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
case R.id.button3 :
Intent i= new Intent("com.example.secondActivityy");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
}
}
To pass values using intents
On Button click
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
In Second Activity retrieve it as below
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
int values= extras.getInt("key");
}
Why not use a switch case to determine which button was clicked by the user and show the corresponding / relevant image in the next activity? For example:
First, make your Activity implement the OnClickListener. Then, in the onCreate() cast your Buttons and set their setOnClickListener
#Override
public void onCreate(Bundle savedInstanceState) {
....
Button Image1 = (Button) findViewById(R.id.Image1);
Image1.setOnClickListener(this);
.... // THE REST OF THE BUTTONS
}
I am assuming you are passing a Bundle in the Intent for starting the next Activity. Change that code to pass information that contains which button was pressed.
For example:
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1");
startActivity(showPhoto);
#Override
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Image1:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1");
startActivity(showPhoto);
break;
case R.id.Image2:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE2");
startActivity(showPhoto);
break;
case R.id.Image3:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE3");
startActivity(showPhoto);
break;
}
}
}
I have 3 buttons Image1, Image2 & Image3 on my MainActivity. Now based
on what button the user clicks (Image1, Image2 & Image3), a
corresponding image is displayed on a new activity.
=> As you already said yo know how to create buttons and initiate it.
Now you just need to assign OnClickListener to every buttons and then pass Image id or URL into the Intent by which you are calling new activity.
Check the code posted by #IceMAN above, now as I mentioned above, put ImageURL or Image ID into Intent by using putExtra() method.
For example:
Intent intent= new Intent(CurrentClass.this, ImageActivity.class);
intent.putExtra("ImageURL",strImageURL);
startActivity(i);
For example you have 3 Buttons and ImageView in Main layout:
Main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn1"
android:text="button 1"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn2"
android:text="button 2"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn3"
android:text="button 3"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/iv"
/>
I your activity metod onClick handle events from three buttons. And we need to recognize that the button has been pressed.
MyActivity.java
public class MyActivity extends Activity implements View.OnClickListener {
Button btn1;
Button btn2;
Button btn3;
ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn1);
btn3 = (Button) findViewById(R.id.btn1);
iv = (ImageView) findViewById(R.id.iv);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
{
Toast toast = Toast.makeText(this, "onClickButton1", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_1);
break;
}
case R.id.btn2:
{
Toast toast = Toast.makeText(this, "onClickButton2", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_2);
break;
}
case R.id.btn3:
{
Toast toast = Toast.makeText(this, "onClickButton3", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_3);
break;
}
}
}
}
Where my_image_1, my_image_2, my_image_3 - images, from your Drawable folder.
Hope its Help.
Add this in onCreate of your activity that contains the 3 buttons
Button image1 = (Button) findViewById(R.id.image1);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image1"));
}
});
Button image2 = (Button) findViewById(R.id.image2);
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image2"));
}
});
Button image3 = (Button) findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image3"));
}
});
Add this in onCreate of your activity where you will set the image:
String imageName = getIntent().getStringExtra("ImageName");
if(imageName.equals("Image1")) {
//set image 1
}
else if(imageName.equals("Image2")) {
//set image 2
}
else if(imageName.equals("Image3")) {
//set image 3
}
You simply implement one ClickListener for each button, which will start the corresponding activity. See an example here.
I know this is and old question, but there is another way, which I prefer, because you can use the same default method for all buttons.
First add a tag to each button.
<Button
android:id="#+id/imageOneButton"
android:tag="1"
android:onClick="chooseImage" />
Then use this tag number to alter the behavior of your method.
public void chooseImage(View view) {
int imageNumber = Integer.parseInt(view.getTag().toString());
// I'll just send this tag number to a toast, but you can send it in your intent
// as an "Extra"
Toast.makeText(this, view.getTag().toString(), Toast.LENGTH_SHORT).show();
}
Related
I created an activity on android studio and I have put there something like 20 ImageButtons. I want to use it as on each click on an image it will move to a new activity. All of the Image Buttons are working on the same principle, my app is a game, and each image represents a level. I want to build one function that will be used on all buttons and will move the user to a new activity according to the data(the properties of the image button) and use that data on the new activity. Every level has its own activity and the main activity is the menu of the game.
Below is my code:
public ImageButton beatsCall; public void Beats(){ beatsCall=(ImageButton)findViewById(R.id.beats); beatsCall.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent toy = new Intent(Levels.this,Beats.class); startActivity(toy); } }); }
You need to provide more information and code. However, you may want to try set a distinct onClickListener and then set all the imageButtons to that listener that will perform an action depending on the button clicked. For example, say you have 4 imageButtons and you want to perform a different action (in your case, start a new activity) for each different button click.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//Start activity 1 here, for example
Intent intent = new Intent(this, YourNewActivity1.class);
String message = v.getId().toString;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
break;
case R.id.textView2:
//Start activity 2 here
break;
case R.id.textView3:
//Start activity 3 here
break;
case R.id.textView4:
//Start activity 4 here
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
This is assuming you have the imageButtons set up in your layout file and you have them initialized in your activity.
In your new activity, you can get the message as such:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (some condition with message){
do something
}
You may also check out this documentation for further information regarding intents.
Something like this? In your xml make your images clickable and give them ID's like this...
<ImageView
android:id="#+id/level_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
Then call a function like this in your Activity's onCreate
private void setupButtons() {
findViewById(R.id.level_1_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelOne.class));
}
});
findViewById(R.id.level_2_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelTwo.class));
}
});
}
You could assign a tag via android:tag to each of your views and then use your single listener to switch on the view's tag to branch the behavior you want.
I am creating an android studio colouring app and need to have it so that the user can select which image they want to colour. I want it so that they click on an image button on one activity and this changes the background of the next activity that the button takes them to however I have no idea how to go about this. Any help would be greatly appreciated. Thankyou
From what I understand, you want to change the color of the next activity on button click of current activity. So, what you can do is:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredColor to get the color according to your logic
intent.putExtra("color", getDesiredColor());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("color")){
rootLayout.setBackgroundColor(intent.getExtra("color"));
}
...
}
Let me know in case you have any doubts.
UPDATE:
With drawable your code will become something like this:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("drawable")){
rootLayout.setBackground(intent.getExtra("drawable"));
//Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
//Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
}
...
}
UPDATE 2:
You can have a hashmap mapping each imagebutton with a drawable.
e.g.
HashMap<Integer, Integer> mViewIdToDrawableMap = new HashMap<>();
mViewIdToDrawableMap.put(mImageButton1.getId(), R.drawable.image1);
mViewIdToDrawableMap.put(mImageButton2.getId(), R.drawable.image2);
mViewIdToDrawableMap.put(mImageButton3.getId(), R.drawable.image3);
public int getDesiredDrawable(View view){
return mViewIdToDrawableMap.get(view.getId());
}
How will you call this function:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable(view));
mContext.startActivity(intent);
}
});
Now, your last question what is rootLayout?
Lets say your activity2 where you want to show this image has somehitng like this as layyout:
<RelativeLayout>
<ImageView
...
android:id="id+/background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
...
/>
</RelativeLayout>
In your Activity2's onCreate, do somehting like this (after getting the drawable as I explained earlier):
//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);
This question already has answers here:
Multiple Buttons' OnClickListener() android
(11 answers)
Closed 7 years ago.
When you have many buttons in a view and all the button have listener. Your main activity gets dirty.
Anyone know how to organize listeners ?
Currently I used this way and implement onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
You could set the property:
android:onClick="buttonClicked"
in the xml file for each of those buttons, and use this in the java code:
public void buttonClicked(View view) {
if (view.getId() == R.id.button1) {
// button1 action
} else if (view.getId() == R.id.button2) {
//button2 action
} else if (view.getId() == R.id.button3){
//button3 action
}
}
You can implement onclicklistner for multiple buttons using swith case
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.firstButton:
// do your code
break;
case R.id.secButton:
// do your code
break;
case R.id.thirdButton:
// do your code
break;
......
default:
break;
}
}
Ya...It s the best way to use multiple onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
#Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.spotifyBtn:
intent = new Intent(this, SimpleSingleExample.class);
break;
case R.id.superDuoBtn:
intent = new Intent(this, CustomExample.class);
break;
case R.id.libraryBtn:
intent = new Intent(this, SequenceExample.class);
break;
case R.id.buildItBiggerBtn:
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
break;
}
if(intent!=null){
startActivity(intent);
}
}
If you want better way than you have to use Android Annotations, its simple and useful, you can find here
Add those View object references to some type of list, iterate through it usin a for-each loop, then call the setOnClickListener on each element which will reduce those lines to just 2 lines for you.
ArrayList <View> list = new ArrayList <>(spotify,superDuoBtn,libraryBtn, buildBiggerBtn, capstoneBtn);
for (View view : list) {
view.setOnClickListener(this);
}
The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks. As far as I know, there are four different ways to add listeners for handling button clicks. If you know of other ways, please post a comment and share them with us.
Xml
<?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"
>
<Button android:text="Inner Class (btn1)" android:id="#+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:text="Anonymous Inner Class (btn2)"
android:id="#+id/Button02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Implementing an Interface (btn3)"
android:id="#+id/Button03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Calling From XML Layout (btn4)"
android:id="#+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn4Listener">
</Button>
</LinearLayout>
in MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//method 1 - uses an inner class named btn1Listener...
Button btn1 = (Button)findViewById(R.id.Button01);
btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener...
Button btn2 = (Button)findViewById(R.id.Button02);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn2 - uses an anonymouse inner class");
}
});
//method 3 - note that this class implements
//the View.OnClickListener interface
//which means that we must implement the onClick()
//method (which you'll find below)..
Button btn3 = (Button)findViewById(R.id.Button03);
btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below
}
//here's the inner class used as a listener for btn1...
private View.OnClickListener btn1Listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
}
};
//here's a method that you must have when your activity implements the
//View.OnClickListener interface...
#Override
public void onClick(View v) {
showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
}
//here's the handler for btn4 (declared in the xml layout file)...
//note: this method only works with android 2.1 (api level 7), it must be public and
//must take a single parameter which is a View
public void btn4Listener(View v) {
showToastMessage("You clicked btn4 - listener was set up in the XML layout");
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
UPDATE:
I have 4 imageButtons and each one needs a "setOnClickListener" or similar. Once the listener fires, I have an intent to take the user to the next Activity, which is currently just a blank page:
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
I started out with 4 Buttons, and had it all working, and then decided to use imageButtons instead for aesthetics.
I'm not sure what the difference is in "buttons" and "imageButtons" are, but you most certainly can't interchange them. My best 'Googling' has not turned up any solution so far in understanding how to modify my earlier 'button' solution.
Any assist would be welcome,
Ed
Here is the MainActivity.java code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//In this next line, note that 'btn' will never be used, it's
//grayed out in the "Button btn...", and in the (R.id.btn) it is
//shown in red font, indicating I probably need to declare a resource of some kind?
Button btn = (Button)findViewById(R.id.btn);
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is the activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:clickable="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:background="#drawable/new_song"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:background="#drawable/loadsong"
android:layout_alignTop="#+id/imageButton"
android:layout_toRightOf="#+id/imageButton"
android:layout_toEndOf="#+id/imageButton"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"/>
</RelativeLayout>
ImageButton in Android is not a subclass of Button, but of ImageView, so you can't store a ImageButton in a Button variable. So you need to use ImageButton as your button object class. The method setOnClickListener is not static and should be set on the button object you want to use. Also the Button id you try to use is not in your xml (at least not the part you posted). So your code should read sth like this:
ImageButton btn = (ImageButton)findViewById(R.id.imageButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
});
If you originally had buttons that were working, you may have forgotten to change the cast type. For example
Button btn = (Button)findViewById(R.id.btn); //Will work.
ImageButton btn = (Button)findViewById(R.id.btn); //Might compile, but will not work.
Alternatively, if you left the XML tag as a normal button
<Button android:id = .../>
and tried to cast it to an image button, an error would be thrown. You would need to change the tag to <ImageButton android:id = .../>
Otherwise, I'm not sure what changes you made that would result in this going wrong. Further details as to the nature of the error would be helpful in solving the problem
You should ImageButton instead of Button.
Must use setOnClickListener to Button object btn instead of Button class
Update your onCreate() method as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton) findViewById(R.id.imageButton);
ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something...
}
});
}
Hope this will help~
I have a form with 6 buttons on it, and I am currently trying to get the 3rd to work. The first two buttons work correctly but when I click on the third button, nothing at all happens, and I get no errors. Can anyone help?
I have checked the form names, class names and button names but I can't get it to work. I have included the code for the buttons.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.main_menu);
initialize(); //calling this method
}
public void initialize(){
//Initializing variables
Film = (Button) findViewById(R.id.bFilm);
Music = (Button) findViewById(R.id.bMusic);
Poets = (Button) findViewById(R.id.bPoet);
Inventors = (Button) findViewById(R.id.bInventors);
Science = (Button) findViewById(R.id.bScience);
Sports = (Button) findViewById(R.id.bSport);
//initialzing onclicklisteners
Film.setOnClickListener(this);
Music.setOnClickListener(this);
Sports.setOnClickListener(this);
}
#Override
public void onClick(View view) {
//case statment for onclicklisteners
try{
switch(view.getId()){
case R.id.bFilm:
Intent film = new Intent(this, Film.class); //open film class
startActivity(film);
break;
case R.id.bMusic:
Intent music = new Intent(this, Music.class); //open music class
startActivity(music);
break;
case R.id.bPoet:
Intent poet = new Intent(this, Poets.class); //open poet class
startActivity(poet);
break;
case R.id.bInventors:
Intent invent = new Intent(this, Inventors.class); //open inventors class
startActivity(invent);
break;
case R.id.bScience:
Intent science = new Intent(this, Science.class); // open science class
startActivity(science);
break;
case R.id.bSport:
Intent sport = new Intent(this, Sports.class); //open sports class
startActivity(sport);
break;
}
If Sports is your 3rd button
Make sure you dont have any element in your xml with the id bSport expect the bSport button itself.
Remove the try{} catch(){}. and see if your app crash, and make sure you have added Sports activity to the Manifest
You have only set click listeners for 3 buttons (Film, Music, and Sports), you have to set all the listeners in your initlialize.
enter code here
public void initialize(){
//Initializing variables
Film = (Button) findViewById(R.id.bFilm);
Music = (Button) findViewById(R.id.bMusic);
Poets = (Button) findViewById(R.id.bPoet);
Inventors = (Button) findViewById(R.id.bInventors);
Science = (Button) findViewById(R.id.bScience);
Sports = (Button) findViewById(R.id.bSport);
//initialzing onclicklisteners
Film.setOnClickListener(this);
Music.setOnClickListener(this);
Sports.setOnClickListener(this);
Poets.setOnClickListener(this);
Inventors.setOnClickListener(this);
Science.setOnClickListener(this);
}