0

I am new to Android application development, and I've been working on a simple FlashLight app that switches between various colors on the screen. The initial activity draws up a layout with only 2 buttons, labeled Green and Blue. I've installed Click Listeners on both buttons and set intents on them both in order for each to load it's corresponding activity, but when I run the app I can only go from the first view to ONE of the other views(Green OR Blue, but not both). I want to be able to choose EITHER button and load the next activity, but I'm a bit lost. Maybe create a boolean that determines which button the user clicked? IDK. This may sound a bit confusing as I'm not good at describing technical things like this, but here's my code below.

package com.jbsoft.SimpleFlashlight; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.*; import android.widget.Button; import android.widget.Toast; public class SimpleFlashLightActivity extends Activity { Button GreenButton; Button BlueButton; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); BlueButton = (Button) findViewById(R.id.bluebutton); BlueButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent blueintent = new Intent(SimpleFlashLightActivity.this, BlueFlashLightActivity.class); startActivity(blueintent); Toast.makeText(v.getContext(), "SWITCH COLOR!", Toast.LENGTH_LONG); GreenButton = (Button) findViewById(R.id.bluebutton); GreenButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent greenintent = new Intent( SimpleFlashLightActivity.this, GreenFlashLightActivty.class); startActivity(greenintent); } }); } }); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater mi = getMenuInflater(); mi.inflate(R.menu.list_menu, menu); return true; } } 
1
  • why have you put the green buttons listener inside the blue's listener? Commented Sep 18, 2011 at 21:13

1 Answer 1

1

you are setting the onclicklistener for the greenbutton in the onclick event of the bluebotton. so the listener gets set only once the user clicks the bluebotton. or am i missing something here?

set the onclick listener for the greenbutton outside of the listnere for the bluebutton and it should work..

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that did the trick! Apparently I had my listeners for all 3 of my activities in the wrong place, in this case I had them inside previous onclick events..that was the culprit. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.