I create my views dynamically and the buttons,
I want to pass a string parameter in the onclick void that is in my loop, but always take the last value linkbutton node !!
if (type.equals("link") == true )
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (classe.equals("text") == true)
{
Button txtlink= new Button(getApplicationContext());
txtlink.setText(value);
txtlink.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
buttonClick(linkButton);
}
});
ll.addView(txtlink);
}
else if(classe.equals("button") == true)
{
Button btn_entregistrer = new Button(getApplicationContext());
btn_entregistrer.setText(value);
ll.addView(btn_entregistrer);
btn_entregistrer.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
buttonClick(linkButton);
}
});
}
}
what to do?
It will always take last value because you did not define and initialize linkButton variable inside the loop.
Before passing the value in click , Declare and initialization of variable compulsaory.
Inside the loop
String linkButton="value";
if (type.equals("link") == true )
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (classe.equals("text") == true)
{
Button txtlink= new Button(getApplicationContext());
txtlink.setText(value);
txtlink.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
buttonClick(linkButton);
}
});
ll.addView(txtlink);
}
else if(classe.equals("button") == true)
{
Button btn_entregistrer = new Button(getApplicationContext());
btn_entregistrer.setText(value);
ll.addView(btn_entregistrer);
btn_entregistrer.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
buttonClick(linkButton);
}
});
} }
As I see you are not changing the value for linkButton. That's the reason why always gets the same value.
Related
I am new to java and I don't know what to do. This is the code that I am trying to do, what I need is to mute the sound effects that are playing to other buttons using another button (playstop) and to also unmute the effects using the same button (playstop).
MediaPlayer audio1, audio2, audio3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audio1 = MediaPlayer.create(MainActivity.this, R.raw.addlife);
audio2 = MediaPlayer.create(MainActivity.this, R.raw.freeeze);
audio3 = MediaPlayer.create(MainActivity.this, R.raw.highlight);
Button addlife = (Button) findViewById(R.id.button);
Button freeze = (Button) findViewById(R.id.button2);
Button highlight = (Button) findViewById(R.id.button3);
Button playstop = (Button) findViewById(R.id.button4);
addlife.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
audio1.start();
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
}
});
freeze.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
audio2.start();
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
}
});
highlight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
audio3.start();
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
}
});
i = 0;
playstop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i == 0) {
audio1.stop();
audio2.stop();
audio3.stop();
playstop.setBackgroundResource(R.drawable.mute);
i++;
}
else if (i == 1){
audio1.start(); //does not work
audio2.start(); //does not work
audio3.start(); //does not work
playstop.setBackgroundResource(R.drawable.unmute);
i = 0;
}
}
});
}
If you are trying to find which sound is playing right now then MediaPlayer has method - isPlaying(). It returns true if it is playing right now.
In this case you need something like this:
if (audio1.isPlaying()){
audio1.stop()
addLife.setVisibility(View.VISIBLE)
}
If you need to pause/play currently playing sound you can use pause() and start() methods, but in this case you better to make some additional class to handle all manipulations. Example:
import android.media.MediaPlayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
public class MediaPlayerManager {
public final HashMap<String, MediaPlayer> mediaPlayerList;
public final ArrayList<String> keys;
private String pausedPlayerKey;
public MediaPlayerManager(MediaPlayer... mediaPlayers) {
mediaPlayerList = new HashMap<>();
keys = new ArrayList<>();
if (mediaPlayers != null && mediaPlayers.length > 0) {
for (MediaPlayer mediaPlayer : mediaPlayers) {
String uid = UUID.randomUUID().toString();
keys.add(uid);
mediaPlayerList.put(uid, mediaPlayer);
}
}
}
public boolean isAnyOnePlaying(){
for (String key : keys) {
MediaPlayer player = mediaPlayerList.get(key);
if (player != null && player.isPlaying()) {
return true;
}
}
return false;
}
public void pauseIfPlaying() {
for (String key : keys) {
MediaPlayer player = mediaPlayerList.get(key);
if (player != null && player.isPlaying()) {
player.pause();
pausedPlayerKey = key;
break;
}
}
}
public void resume() {
if (pausedPlayerKey != null) {
if (mediaPlayerList.containsKey(pausedPlayerKey) && mediaPlayerList.get(pausedPlayerKey) != null) {
Objects.requireNonNull(mediaPlayerList.get(pausedPlayerKey)).start();
pausedPlayerKey = null;
}
}
}
}
Then in onCreate method you can do this.
MediaPlayerManager mpm = new MediaPlayerManager({audio1, audio2, audio3});
And then:
playstop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpm.isAnyOnePlaying()){
mpm.pauseIfPlaying();
}else {
mpm.resume();
}
//help//
}
});
How do I enable the "Next" button after 'Yes' or 'No' from each question is pressed? I want to make my "Next" button active (change the background and enable) only after all the questions have been answered. I don't know whether I should use loop or if it's possible by if-else. Any help is very appreciated.
I have developed my program in a way that users can either press the 'yes' or 'no' button and after answering all questions the "Next" button should get activated (change the background and enable).
int one = 0;
int two = 0;
int three = 0;
int four = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimate_question_one);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String phone_price = extras.getString("phone_price");
final String numonly = phone_price.replaceAll("[^0-9]", "");
final int phoneprice = Integer.parseInt(numonly);
final Button ques_one_yes = (Button)findViewById(R.id.ques_one_yes);
final Button ques_one_no = (Button)findViewById(R.id.ques_one_no);
final Button ques_two_yes = (Button)findViewById(R.id.ques_two_yes);
final Button ques_two_no = (Button)findViewById(R.id.ques_two_no);
final Button ques_three_yes = (Button)findViewById(R.id.ques_three_yes);
final Button ques_three_no = (Button)findViewById(R.id.ques_three_no);
final Button ques_four_yes = (Button)findViewById(R.id.ques_four_yes);
final Button ques_four_no = (Button)findViewById(R.id.ques_four_no);
final Button nextstep = (Button)findViewById(R.id.next_step);
ques_one_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
one = 1;
ques_one_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_one_yes.setBackground(getDrawable(R.drawable.button_background));
ques_one_no.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_one_no.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_one_no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
one = 1;
ques_one_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_one_no.setBackground(getDrawable(R.drawable.button_background));
ques_one_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_one_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_two_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
two = 1;
ques_two_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_two_yes.setBackground(getDrawable(R.drawable.button_background));
ques_two_no.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_two_no.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_two_no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
two = 1;
ques_two_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_two_no.setBackground(getDrawable(R.drawable.button_background));
ques_two_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_two_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_three_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
three = 1;
ques_three_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_three_yes.setBackground(getDrawable(R.drawable.button_background));
ques_three_no.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_three_no.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_three_no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
three = 1;
ques_three_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_three_no.setBackground(getDrawable(R.drawable.button_background));
ques_three_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_three_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_four_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
four = 1;
ques_four_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_four_yes.setBackground(getDrawable(R.drawable.button_background));
ques_four_no.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_four_no.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
ques_four_no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
four = 1;
ques_four_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
ques_four_no.setBackground(getDrawable(R.drawable.button_background));
ques_four_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
ques_four_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
}
});
}
}
Quesions
you can try "RadioGroup" and "RadioButton"
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup rb, int id){
if (id == R.id.ques_one_yes)
}
})
and in layout file
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/ques_one_yes"
android:layout_width="wrap_content"
android:BackGround="#drawable/rb_bg"
android:layout_height="wrap_content"/>
</RadioGroup>
and in drawable file rb_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_background" android:state_checked="true"/>
<item android:drawable="#drawable/inactive_button_background"/>
</selector>
and the TextColor is other drawable file
The simplest implementation that comes to my mind is to check all of the int values you defined. You can do it in a helper method together with background and color changes
private void helperMethodName (Button buttonNo, Button buttonYes){
buttonNo.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
buttonNo.setBackground(getDrawable(R.drawable.button_background));
buttonYes.setTextColor(getApplication().getResources().getColor(R.color.black));
buttonYes.setBackground(getDrawable(R.drawable.inactive_button_background));
if (one != 0 && two != 0 && three != 0 && fore != 0) {
nextstep.setEnabled(true);
}
}
And then just call this method in your on clicks
public void onClick(View v) {
one = 1;
helperMethodName(ques_one_no, ques_one_yes);
}
Your are taking the initial values for all the options as 0 and making it 1 on OnClick.
Just give an if-else in your onCreate method like:
if(one = 1 && two = 1 && three = 1 && four = 1)
button.setEnable(true);
else
button.setEnable(false);
if all values are 1 then enable the button, else disable it.
In general if you want to achieve this implementation you can try the following ,
To enable/disable the next button:
fun enableNextButton(isEnable: Boolean) {
if(isEnable){
button_next?.alpha = 1.0f
button_next?.isEnabled = true
}else{
button_next?.alpha = 0.5f
button_next?.isEnabled = false
}
}
So call enableNextButton(true) if you want to enable the next button otherwise pass the false param to method. Also alpha sets the button blur which helps you to show the disable button view.
package com.deitel.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
double input1 = 0, input2 = 0d ,count=0;
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn_dot, btn_equal, btn_subtract, btn_multi, btn_add, btn_devision, btn_clear, btn_back;
TextView text_result;
boolean Addition, Subtraction, Multiplication, Devision, decimal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
btn_dot = findViewById(R.id.btn_dot);
btn_equal = findViewById(R.id.btn_equal);
btn_add = findViewById(R.id.btn_add);
btn_subtract = findViewById(R.id.btn_subtract);
btn_multi = findViewById(R.id.btn_multi);
btn_devision = findViewById(R.id.btn_devision);
btn_clear = findViewById(R.id.btn_clear);
btn_back = findViewById(R.id.btn_back);
text_result = findViewById(R.id.text_result);
btn0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "0");
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText(text_result.getText() + "9");
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text_result.getText().length() != 0) {
input1 = Float.parseFloat(text_result.getText() + "");
Addition = true;
decimal = false;
text_result.setText(null);
}
}
});
btn_subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text_result.getText().length() != 0) {
input1 = Float.parseFloat(text_result.getText() + "");
Subtraction = true;
decimal = false;
text_result.setText(null);
}
}
});
btn_multi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text_result.getText().length() != 0) {
input1 = Float.parseFloat(text_result.getText() + "");
Multiplication = true;
decimal = false;
text_result.setText(null);
}
}
});
btn_devision.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text_result.getText().length() != 0) {
input1 = Float.parseFloat(text_result.getText() + "");
Devision = true;
decimal = false;
text_result.setText(null);
}
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text_result.setText("");
input1 = 0.0;
input1 = 0.0;
}
});
btn_dot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(count==0){
count=1;
text_result.setText(text_result.getText()+".");
return;
}
else{
text_result.setText(text_result.getText()+"0.");
decimal=true;
}
}
});
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String number = text_result.getText().toString();
int input = number.length();
if (input > 0) {
text_result.setText(number.substring(0, input - 1));
}
}
});
btn_equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count=0;
if ((Addition || Subtraction || Multiplication || Devision) ) {
if (text_result.getText().toString().trim().equals("")){
input2=0;
return;
}else {
input2 = Float.parseFloat(text_result.getText() + "");
}
}
if (Addition) {
text_result.setText(input1 + input2 + "");
Addition = false;
}
if (Subtraction) {
text_result.setText(input1 - input2 + "");
Subtraction = false;
}
if (Multiplication) {
text_result.setText(input1 * input2 + "");
Multiplication = false;
}
if (Devision) {
text_result.setText(input1 / input2 + "");
Devision = false;
}
}
});
}
}
When I press dot button I want that dot button press only one time in input 1 like:2.5+3.7 etc.
But this code doesn't meet that requirements - it displays 2.3.4.5 etc..but I want only one dot in one input. When I press dot button I want that dot button press only one time in input 1 like:2.5+3.7 etc.
Here you can manage that with simple flag.
public class MainActivity extends AppCompatActivity {
// IDs of all the numeric buttons
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine};
// IDs of all the operator buttons
private int[] operatorButtons = {R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide};
// TextView used to display the output
private TextView txtScreen;
// Represent whether the lastly pressed key is numeric or not
private boolean lastNumeric;
// Represent that current state is in error or not
private boolean stateError;
// If true, do not allow to add another DOT
private boolean lastDot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the TextView
this.txtScreen = (TextView) findViewById(R.id.txtScreen);
// Find and set OnClickListener to numeric buttons
setNumericOnClickListener();
// Find and set OnClickListener to operator buttons, equal button and decimal point button
setOperatorOnClickListener();
}
/**
* Find and set OnClickListener to numeric buttons.
*/
private void setNumericOnClickListener() {
// Create a common OnClickListener
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Just append/set the text of clicked button
Button button = (Button) v;
if (stateError) {
// If current state is Error, replace the error message
txtScreen.setText(button.getText());
stateError = false;
} else {
// If not, already there is a valid expression so append to it
txtScreen.append(button.getText());
}
// Set the flag
lastNumeric = true;
}
};
// Assign the listener to all the numeric buttons
for (int id : numericButtons) {
findViewById(id).setOnClickListener(listener);
}
}
/**
* Find and set OnClickListener to operator buttons, equal button and decimal point button.
*/
private void setOperatorOnClickListener() {
// Create a common OnClickListener for operators
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// If the current state is Error do not append the operator
// If the last input is number only, append the operator
if (lastNumeric && !stateError) {
Button button = (Button) v;
txtScreen.append(button.getText());
lastNumeric = false;
lastDot = false; // Reset the DOT flag
}
}
};
// Assign the listener to all the operator buttons
for (int id : operatorButtons) {
findViewById(id).setOnClickListener(listener);
}
// Decimal point
findViewById(R.id.btnDot).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (lastNumeric && !stateError && !lastDot) {
txtScreen.append(".");
lastNumeric = false;
lastDot = true;
}
}
});
// Clear button
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(""); // Clear the screen
// Reset all the states and flags
lastNumeric = false;
stateError = false;
lastDot = false;
}
});
// Equal button
findViewById(R.id.btnEqual).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onEqual();
}
});
}
/**
* Logic to calculate the solution.
*/
private void onEqual() {
// If the current state is error, nothing to do.
// If the last input is a number only, solution can be found.
if (lastNumeric && !stateError) {
// Read the expression
String txt = txtScreen.getText().toString();
// Create an Expression (A class from exp4j library)
Expression expression = new ExpressionBuilder(txt).build();
try {
// Calculate the result and display
double result = expression.evaluate();
txtScreen.setText(Double.toString(result));
lastDot = true; // Result contains a dot
} catch (ArithmeticException ex) {
// Display an error message
txtScreen.setText("Error");
stateError = true;
lastNumeric = false;
}
}
}
}
So I basically want to know how would I allow the user to proceed to the next page when they hit pay if either txtCode or StreetCode is filled, only one of these two needs to be filled in order to pass to the next page but how would i get that to work as currently they both must filled to pass to the next page.
btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText)findViewById(R.id.txtReg);
txtCode = (EditText)findViewById(R.id.txtCode);
txtStreetName = (EditText)findViewById(R.id.txtStreetName);
dlCostTime = (Spinner)findViewById(R.id.dlCostTime);
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
Replace this code
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
with this
if( txtReg.getText().toString().trim().equals("")){
txtReg.setError("required!");
}else{
if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) ||
(!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}else{
//show message enter either code or street name not both
}
Replace this code
btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText) findViewById(R.id.txtReg);
txtCode = (EditText) findViewById(R.id.txtCode);
txtStreetName = (EditText) findViewById(R.id.txtStreetName);
dlCostTime = (Spinner) findViewById(R.id.dlCostTime);
if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
} else if (txtStreetName.getText().toString().trim().length() == 0) {
txtStreetName.setError("required!");
} else if (txtReg.getText().toString().trim().length() == 0) {
txtReg.setError("required!");
} else if (txtCode.getText().toString().trim().length() == 0) {
txtCode.setError("required!");
}
}
}
I am building a memory game with four cards(2x2). These four cards have an onClick named "cards". This onClick consists of an If statement that flips the cards back if they are not the same, and keeps them if they are the same.
The front image of the card is the same for the 4, but the back has different images.My problem is that I want the cards to flip, but they already have an onClick. So how can I write "if button clicked" in an If statement or is there another solution?
EDIT:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setVisibility(View.INVISIBLE);
pic1 = (ImageView) findViewById(R.id.imageView);
pic2 = (ImageView) findViewById(R.id.imageView2);
pic3 = (ImageView) findViewById(R.id.imageView3);
pic4 = (ImageView) findViewById(R.id.imageView4);
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
pic1.setVisibility(View.VISIBLE);
pic2.setVisibility(View.VISIBLE);
pic3.setVisibility(View.VISIBLE);
pic4.setVisibility(View.VISIBLE);
}
});
}
public void cards(View v) {
if (v.getId() == pic1.getId() ) {
pic1.setImageResource(R.drawable.img2);
pic1.setTag("img2");
} else if (v.getId() == pic2.getId()) {
pic2.setImageResource(R.drawable.img2);
pic2.setTag("img2");
} else if (v.getId() == pic3.getId()) {
pic3.setImageResource(R.drawable.img3);
pic3.setTag("img3");
} else if (v.getId() == pic4.getId()) {
pic4.setImageResource(R.drawable.img3);
pic4.setTag("img3");
}
if (R.drawable.img2 == R.drawable.img2) {
pic1.setImageResource(R.drawable.img2);
pic1.getTag();
pic2.setImageResource(R.drawable.img2);
pic2.getTag();
}
if (R.drawable.img3 == R.drawable.img3) {
pic3.setImageResource(R.drawable.img3);
pic3.getTag();
pic4.setImageResource(R.drawable.img3);
pic4.getTag();
}
if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
}
}
SECOND METHOD I'M TRYING: #Override
public void onClick(View v) {
button1.setVisibility(View.INVISIBLE);
pic1 = (ImageView) findViewById(R.id.imageView);
pic2 = (ImageView) findViewById(R.id.imageView2);
pic3 = (ImageView) findViewById(R.id.imageView3);
pic4 = (ImageView) findViewById(R.id.imageView4);
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
pic1.setVisibility(View.VISIBLE);
pic2.setVisibility(View.VISIBLE);
pic3.setVisibility(View.VISIBLE);
pic4.setVisibility(View.VISIBLE);
if (R.drawable.img2 == R.drawable.img2) {
pic1.setImageResource(R.drawable.img2);
pic2.setImageResource(R.drawable.img2);
}
if (R.drawable.img3 == R.drawable.img3) {
pic3.setImageResource(R.drawable.img3);
pic4.setImageResource(R.drawable.img3);
}
if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
pic1.setImageResource(R.drawable.img1);
pic2.setImageResource(R.drawable.img1);
pic3.setImageResource(R.drawable.img1);
pic4.setImageResource(R.drawable.img1);
}
}
});
}
public void pic1Click(View v){
pic1.setImageResource(R.drawable.img1);
}
public void pic2Click(View v){
pic2.setImageResource(R.drawable.img1);
}
public void pic3Click(View v){
pic3.setImageResource(R.drawable.img2);
}
public void pic4Click(View v){
pic4.setImageResource(R.drawable.img2);
}
So I think you are looking for a way to figure out which ImageButton the user clicked. You can get this from the view variable being passed to the onClick method.
You also need to keep track of which images have been clicked. You can keep track of this by adding a tag to the image.
public void myClickMethod(View v){
if (v.getId() == pic1.getId() ) {
pic1.setImageResource(R.drawable.img2);
pic1.addTag("img2");
} else if (v.getId() == pic2.getId() {
pic2.setImageResource(R.drawable.img2);
pic2.addTag("img2");
} else if (v.getId() == pic3.getId() {
pic3.setImageResource(R.drawable.img3);
pic3.addTag("img3");
} else if (v.getId() == pic4.getId() {
pic4.setImageResource(R.drawable.img3);
pic4.addTag("img3");
}
Of course you will want to set the tag every time you change the image (and the first time you create the image). So to check if you need a reset, you would need to check that two images have changed away from the default image they started with. You could also do this with global variables, but tags might be a little more intuitive.
You can reference the Android Dev Doc
At first, you need to know what is Event Listeners, and implement it.
After you did it,and then you can Register the Event Listeners with your implementation.
Example Code
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Try this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//write your codes here
}
});
}