Trying to create a game that kids chase the marble. I want to have the buttons background change only if another buttons background equal something. But can't get the onclick with the a if statement to work. Here is the code I currently have.
package test.tablet.design;
import java.text.DateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MarbleGame extends Activity {
private Button btnA1mc; private Button btnA2mc; private Button btnA3mc; private Button btnA4mc; private Button btnB1mc; private Button btnB2mc; private Button btnB3mc; private Button btnB4mc;
private Button btnC1mc; private Button btnC2mc; private Button btnC3mc; private Button btnC4mc; private Button btnD1mc; private Button btnD2mc; private Button btnD3mc; private Button btnD4mc;
private Button btnE1mc; private Button btnE2mc; private Button btnE3mc; private Button btnE4mc; private Button btnF1mc; private Button btnF2mc; private Button btnF3mc; private Button btnF4mc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marble_chase);
TextView tv_time = (TextView) findViewById(R.id.tv_time);
String currenttime = DateFormat.getDateTimeInstance().format(new Date());
tv_time.setText(currenttime);
btnA1mc = (Button)findViewById(R.id.btnA1mc); btnA2mc = (Button)findViewById(R.id.btnA2mc); btnA3mc = (Button)findViewById(R.id.btnA3mc); btnA4mc = (Button)findViewById(R.id.btnA4mc);
btnB1mc = (Button)findViewById(R.id.btnB1mc); btnB2mc = (Button)findViewById(R.id.btnB2mc); btnB3mc = (Button)findViewById(R.id.btnB3mc); btnB4mc = (Button)findViewById(R.id.btnB4mc);
btnC1mc = (Button)findViewById(R.id.btnC1mc); btnC2mc = (Button)findViewById(R.id.btnC2mc); btnC3mc = (Button)findViewById(R.id.btnC3mc); btnC4mc = (Button)findViewById(R.id.btnC4mc);
btnD1mc = (Button)findViewById(R.id.btnD1mc); btnD2mc = (Button)findViewById(R.id.btnD2mc); btnD3mc = (Button)findViewById(R.id.btnD3mc); btnD4mc = (Button)findViewById(R.id.btnD4mc);
btnE1mc = (Button)findViewById(R.id.btnE1mc); btnE2mc = (Button)findViewById(R.id.btnE2mc); btnE3mc = (Button)findViewById(R.id.btnE3mc); btnE4mc = (Button)findViewById(R.id.btnE4mc);
btnF1mc = (Button)findViewById(R.id.btnF1mc); btnF2mc = (Button)findViewById(R.id.btnF2mc); btnF3mc = (Button)findViewById(R.id.btnF3mc); btnF4mc = (Button)findViewById(R.id.btnF4mc);
btnE2mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
btnE2mc.setBackgroundResource(R.drawable.marble_x);
btnB4mc.setBackgroundResource(R.drawable.marble);
}
});
btnB4mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnE2mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnB4mc.setBackgroundResource(R.drawable.marble_x);
btnC1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
btnC1mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnC1mc.setBackgroundResource(R.drawable.marble_x);
btnF1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
}
}
I would have an internal boolean set to true whenever the button is clicked and the background is changed.
boolean isBackgroundChanged = false;
Whenever the button is clicked, you change the background and set isBackgroundChanged to true.
And then, instead of doing:
btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)
you would have
if(isBackgroundChanged)...
You're trying to compare the Button object with a Drawable object, which will always return false.
You might instead want to use View.setTag() to track the state for each individual button. With a tag (named or unnamed) you can associate arbitrary data with an individual View. In the example below, auto-boxing is used to convert booleans into Booleans.
if ((Boolean)btn1.getTag()) {
btn1.setTag(false);
btn2.setTag(true);
// TODO actual background swapping
}
For readability and reducing duplication, you might want to use a helper function that can do the swapping for you, which will take two Buttons as arguments.
Related
I have a imageview that randomly generate 1 out of 2 possibles images clicking on one button.
I want that when one image is showed (R.drawable.aa) and I press other button, a toast is shown.
My problem is that once a random image is shown and click on the other button, nothing happens.
package com.example.isaiasalarcon.menu;
import java.util.Random;
import java.util.jar.Attributes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import static com.example.isaiasalarcon.menu.R.drawable.aa;
public class buho extends Activity {
// UI components
private Button drawButton;
private Button boton2;
private ImageView cardImage;
// Random object
private final static Random random = new Random();
// The card deck
private final static int[] cardDeck = new int[] {
R.drawable.aa,
R.drawable.a2,
};
private Integer q;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buho);
drawButton = (Button)findViewById(R.id.drawButton);
boton2 = (Button)findViewById(R.id.button2);
cardImage = (ImageView)findViewById(R.id.cardImage);
drawButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
Integer q = cardDeck[random.nextInt(cardDeck.length)];
cardImage.setImageResource(q);
}
});
boton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (q.equals(R.drawable.aa)) {
Toast toast = Toast.makeText(buho.this, "si", Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(buho.this, "no", Toast.LENGTH_LONG);
toast.show();
}
}
});
}
}
two things:
1) Integer q I don't see a reason why this needs to be an Integer. You should be able to int
2) your q inside the onClick is creating a NEW variable called q. You need to update your code to the following: (note how it doesn't have the Type declaration before it, so it means to use the previously declared variable)
drawButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
q = cardDeck[random.nextInt(cardDeck.length)];
cardImage.setImageResource(q);
}
});
I am developing an application that contains 10 set of questions. One questions belong to one page, thus i have 10 different pages. Each answer using radio button. I want to ask, how to assign value on radio button and bring along the value through the 10 pages and displays the result in the result page?
package com.project.logicalthinking;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class question1 extends Activity
{
private Button Button2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
addListenerRadioButton() ;
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(v.getContext(),question2.class);
startActivityForResult(intent,0);
}
}
));
}
private void addListenerRadioButton() {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnTouchListener(new OnTouchListener()
{
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
;
}
I'm not an android guru (someone correct me if I'm wrong), but could you declare static variables for each activity that define which option was selected and then in the Results activity check the static variable values?
public static int QUESTION1_NO_ANSWER = 0;
public static int QUESTION1_ANSWER1 = 1;
public static int QUESTION1_ANSWER2 = 2;
public static int QUESTION1_ANSWER3 = 3;
public static int selectedAnswer;
Then in your results....
if(QUESTION1.selectedAnswer = QUESTION1_ANSWER2)
//YAY got question right!
} else {
//uh oh, it was wrong
}
You will need to populate the questions, one by one, as the user selects Yes/No using the radio button and pressing "Next". Yes/No could be a radio button and "Next" is just a Button. A new question is loaded in the OnClickListener of the "Next" button.
I have created an app that suppose to toggle the button background color on every click. Here is the code:
package com.example.flash.light;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button screen = (Button) findViewById(R.id.screen);
Drawable background = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(background);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
}
}
});
}
Click on the button doesn't response.
Thanks
I belive the problem here is that the resource returned by your
getResources().getDrawable(int id)
is different everytime you call it. Android just constructs new instance of drawable class instead of returning old one. (At least I belive that there is no caching in this case)
Comparing three different instances with == operator will never return true.
Second thing is an obvious bug in the code:
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(**newBackground**);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(**newBackground**);
}
instead of background you should have newBackground there.
Try this :
public class MainActivity extends Activity {
private boolean isBlack = false;
#Override
public void onCreate(Bundle savedInstanceState) {
// your code
final Button screen = (Button) findViewById (R.id.screen);
isBlack = true;
screen.setBackgroundColor(Color.BLACK);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (isBlack){
screen.setBackgroundColor(Color.WHITE);
isBlack = false;
}
else{
screen.setBackgroundColor(Color.BLACK);
isBlack = true;
}
}
});
}
}
I am trying to write my first app- a simple application where the user enters a hex color code (EditText), hits enter (Button), and the background color of a ImageView is changed to the user's entered hex code. How I see it, I will have to gettext from the edittext, write the gettext to a file, then edit the file to append 0xAA before the hex code so that it can be entered in ImageView.setBackgroundColor(0xAA"HEXHEX"). Please let me know how I can do this, or if that is even the correct way to do this.
Here is my java so far (on button click, bg color changes to white, clear reverts it to black)
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class ChkhexActivity extends Activity {
private EditText hex;
private Button chk;
private Button clear;
private ImageView view;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
hex = (EditText)findViewById(R.id.hex);
chk = (Button)findViewById(R.id.chk);
clear = (Button)findViewById(R.id.clear);
view = (ImageView)findViewById(R.id.view);
chk.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Chk(); }});
clear.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Clear(); }});
}
private void Chk()
{
view.setBackgroundColor(0xFFffffff);
}
private void Clear()
{
hex.setText("");
view.setBackgroundColor(0xFF000000);
}
}
Very good exercise for a beginner.
Use Color.parseColor(). Don't forget to validate the Input first!
view.setBackgroundColor(Color.parseColor(edt.getText().toString()));
every time u want to change the color of imageview based on text entered in edittext.so u cant fix it like this
view.setBackgroundColor(0xFFffffff);
u have to get the text from edittext.some example like this..
public class test extends Activity{
private EditText ed;
private Button btn;
private ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ed=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
iv=(ImageView)findViewById(R.id.imageView1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=ed.getText().toString();
iv.setBackgroundColor(Color.parseColor(value));
}
});
}
}
the edittext text u entered can be like hex format example like #B0171F,#fafad2,..
i want to set a "0" in each "empyt" TextBoxes if the user clicks one button, to avoid an error in the app, any help? i dont know what to do
package com.doko.most;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class doko extends Activity {
private EditText bx1;
private EditText bx2;
private TextView txt3;
private Button btncalcular;
private Button btnreset;
private double variable1 = 0;
private double variable2 = 0;
private double variable3 = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
bx1 = (EditText)findViewById(R.id.bx1);
bx2 = (EditText)findViewById(R.id.bx2);
txt3 = (TextView)findViewById(R.id.txt3);
btncalcular = (Button)findViewById(R.id.btncalcular);
btnreset = (Button)findViewById(R.id.btnreset);
btncalcular.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calcular(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calcular()
{
variable1 = Double.parseDouble(bx1.getText().toString());
variable2 = Double.parseDouble(bx2.getText().toString());
variable3 = Math.sqrt(1*2/3600);
txt3.setText(Double.toString(variable3));
}
private void reset(){
bx1.setText("");
bx2.setText("");
}
}
try something like this, quick and dirty:
private void calcular() {
try {
variable1 = Double.parseDouble(bx1.getText().toString());
}
catch(NumberFormatException e) {
bx1.setText("0");
}
try {
variable2 = Double.parseDouble(bx2.getText().toString());
}
catch(NumberFormatException e) {
bx2.setText("0");
}
variable3 = Math.sqrt(1*2/3600);
txt3.setText(Double.toString(3));
}
set up Hint property at your EditText
Ex.
and check it when you getText()
use str.isEmpty() and str.length to check user's inpupt
another way :
set up inputType for the EditText to number and setup Input filter
ex.
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(4);
input.setFilters(FilterArray);