I am making a program of clicking the same 2 dublicate pics.
If user clicked the wrong pic after clicking the 1st pic, there will be negative value. If the two pics are same, I set those buttons to clickable(false). But my problem is, when the second pic is not same as 1st pic, I want to set clickable(true) to second pic, as well as the 1st pic but I have no idea how to get the id of 1st clicked button.
I have 16buttons and in this, I posted button1, other buttons are similar.
my code is.....
Collections.shuffle(buttonResources);
for(int i = 0; i < buttonResources.size(); i++)
{
ImageButton bt = findViewById(buttonResources.get(i));
buttons.add(bt);
bt.setBackgroundResource(pics.get(i/2));
bt.setTag(pics.get(i/2));
aaa = pics.get(i/2);
bt.setOnClickListener(this);
bt.setClickable(false);
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
AlphaAnimation alphaAnimation0 = new AlphaAnimation(1,0);
alphaAnimation0.setDuration(200);
alphaAnimation0.setFillAfter(true);
for(int i = 0; i < buttonResources.size(); i++)
{
ImageButton bt = findViewById(buttonResources.get(i));
buttons.add(bt);
bt.startAnimation(alphaAnimation0);
bt.setClickable(true);
}
}
}, 5000);
}
#Override
public void onClick(final View v1) {
switch (v1.getId()) {
case R.id.bt1:
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(300);
alphaAnimation1.setFillAfter(true);
bt1.startAnimation(alphaAnimation1);
aaa = (int) bt1.getTag();
bt1.setClickable(false);
if(bbb==0){
bt1.setClickable(false);
bbb = aaa;
}else if (bbb==aaa){
right++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
}else{
wrong++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(600);
alphaAnimation.setFillAfter(true);
bt1.startAnimation(alphaAnimation);
bt1.setClickable(true);
}
break;
add int bbbID=0;
then add these changes:
case R.id.bt1:
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(300);
alphaAnimation1.setFillAfter(true);
bt1.startAnimation(alphaAnimation1);
aaa = (int) bt1.getTag();
bt1.setClickable(false);
if(bbb==0){
bt1.setClickable(false);
bbb = aaa;
bbbID = v1.getId(); //////change
}else if (bbb==aaa){
right++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
bbbID = 0; //////change
}else{
wrong++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(600);
alphaAnimation.setFillAfter(true);
bt1.startAnimation(alphaAnimation);
bt1.setClickable(true);
for(int i=0;i<buttons.size();i++){ //////change
if(buttons.get(i).getId() == bbbID){ //////change
buttons.get(i).setClickable(true); //////change
}
}
}
break;
Related
I need that when I click on the button, the text in cenApple changes to i * 132, but when I click on it, countApple becomes 1 more, as it should be, and cenApple remains the same, but if I click on it again, cenApple already changes to i * 132.
View.OnClickListener appleListenerPlus = new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(countApple.getText().toString());
countApple.setText(String.valueOf(i + 1));
i = i;
int k = (i * 132);
String a = (String.valueOf(k));
cenApple.setText(a + " money");
}
};
why do you assigning same value again in this line: i = i;. it will stay the same...
when you change text then you should read it again or at least update i value also
int i = Integer.parseInt(countApple.getText().toString());
i++; // increment in here
countApple.setText(String.valueOf(i));
// or just read updated value again
//i = Integer.parseInt(countApple.getText().toString());
int k = (i * 132);
String a = (String.valueOf(k));
cenApple.setText(a + " money");
this code work very well its generate three button because I have three elements"car" and shuffle elements
button_1 "a" , button_2 "r" , button_3 "c" , my problem is when I click on every button its typing
in edit text became "a a a"
//the problem was here (var button)
edittext.setText(edittext.getText().toString() + button.getText());
i am trying to make game guessing the word
String array[]= {"car"};
int random = (int) (Math.random() * array.clone().length);
StringBuilder word = new StringBuilder(array[random]);
for (int i = 0; i < array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearLayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//******the problem was here (var button)
button.setVisibility(View.INVISIBLE);
edittext.setText(edittext.getText().toString() + button.getText());
}
});
}
Here is codes, and i am trying to receive a feedback from imageviews which are overlapped. One of the imageviews return isPressed method true, but the other one return me false. They are overlapped, so when i clicked one of them, both of their isPressed method should be true, i guess. Where am i wrong
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
ImageView[] imageViews;
Button button;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout gridLayout = findViewById(R.id.grid_layout);
GridLayout.LayoutParams[] layoutParams = new GridLayout.LayoutParams[9];
imageViews = new ImageView[18];
button = findViewById(R.id.button);
for (int i = 0, j = 0, k = -1, m = 0; j < 3 & k < 3 & m < imageViews.length || i < layoutParams.length; m++) {
if (m == 18) {
break;
}
if (m == 0) {
k++;
}
layoutParams[i] = new GridLayout.LayoutParams();
layoutParams[i].setGravity(Gravity.CENTER);
layoutParams[i].setMargins(25, 25, 25, 25);
layoutParams[i].columnSpec = GridLayout.spec(k, GridLayout.CENTER);
layoutParams[i].rowSpec = GridLayout.spec(j, GridLayout.CENTER);
imageViews[m] = new ImageView(MainActivity.this);
imageViews[m].setLayoutParams(layoutParams[i]);
gridLayout.addView(imageViews[m]); //---
if (m % 2 == 0) {
imageViews[m].setImageResource(R.drawable.first_image);
imageViews[m].setAlpha(1f);
} else {
imageViews[m].setImageResource(R.drawable.second_image);
imageViews[m].setAlpha(0.5f);
}
if (m % 2 == 1 & m != 0) {
j++;
i++;
}
if (m % 6 == 5 & m != 0) {
k++;
}
if (j == 3) {
j = 0;
}
}
actionView();
}
public void actionView() {
for (int m = 0; m < imageViews.length; m++) {
imageViews[m].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, " " + a + " " + "\n", Toast.LENGTH_SHORT).show();
Log.e(TAG, imageViews[1].isPressed() + "");
Log.e(TAG, imageViews[0].isPressed() + "");
Log.e(TAG, imageViews[1].isEnabled() + "");
Log.e(TAG, imageViews[0].isEnabled() + "");
Log.e(TAG, "--------------------");
a++;
for (int x = 0; x < imageViews.length; x++) {
if (imageViews[x].isPressed()) {
Log.e(TAG, x + " Image is clicked");
Log.e(TAG, " *****************");
}
}
}
});
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = 0;
}
});
}
}
If the views are overlapped and click is enabled for both only the view on top will receive the click event.
If you want to call on click event of both, call performClick of the view in background form the onclick listener of the view in front.
Like imageviewInBack.performClick()
I'm currently working on a school project (a small android game) and so far I've written a code which generates a random equation 2 seconds after the activity InGame is launched and displays it in a textview. Another 5 seconds later, the second equation is generated and displayed in a different textview. Now the user has to decide if the second equation has a bigger result than the first one by either pressing the button bigger or smaller. If it was correct, the next equation should be displayed and it would go on like this until the user decided wrong.
Here is my code so far:
(Code for the first equation):
// Generate random equation and display it in textview
String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation = "";
for (int i = 0; i < numOfOperations; i++) {
equation += numbers.get(i);
equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
TextEquation.setText(equation);
// Evaluate the result of the equation
double doubleAnswer1 = eval(equation);
String stringAnswer = Double.toString(doubleAnswer1);
TextView textAnswer = (TextView)findViewById(R.id.textView4);
textAnswer.setText(stringAnswer);
}
(Code for second equation (basically same as for first equation except the name of the strings and doubles are different)):
String equation2;
static double doubleAnswer2;
public void start2() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation2 = "";
for (int i = 0; i < numOfOperations; i++) {
equation2 += numbers.get(i);
equation2 += operations.get(i);
}
equation2 += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView3);
TextEquation.setText(equation2);
// Evaluate the result of the equation
double doubleAnswer2 = eval(equation2);
String stringAnswer = Double.toString(doubleAnswer2);
TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
textAnswer.setText(stringAnswer);
}
And here is my onCreate code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingame);
// Display first equation 2 seconds after the activity is launched
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
start1();
}
}, 2000);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
start2();
}
}, 7000);
// Check if user was right or wrong
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
}
The app launches correctly and it also displays the first and second equation, but when I press one of the button, it tells me in the logcat you are wrong but I decided 100% correct. However if I debug the app, it tells me that doubleAnswer1 and doubleAnswer2 are both = 0. That's why it all ways tells me 'you are wrong'. I don't know how to fix this, maybe I need to store the doubleAnswer1 and doubleAnswer2 somewhere.
I really don't know what to do, so it would really help me if someone has an idea what to do.
If anything is unclear in my question, feel free to ask and I will try to clarify the problem.
Thank you in advance for your help!
I think your problem lies here:
double doubleAnswer1 = eval(equation);
I did a quick internet search and did not find any native function called eval(). Instead you should look into Script Engine Manager for java:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
or exp4j which is shown here:
Executing math equation in Android
Edit:
change the following:
double doubleAnswer1 = eval(equation);
to:
doubleAnswer1 = eval(equation);
Similarly do the same for doubleAnswer2
how can i scroll a view (recyclerview) in relation to my tts,
ive looked at onUtterance but it seems to only have a start and stop listener, so i need to think outside the box, i give my tts a string from an Arraylist like this
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++)
{
list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
}
speakWords(words);
I was thinking about cutting the string up into sections and giving it to the TTS one string at a time and move the view as I go. I already give my gridlayout manager an int for the amount of columns (called columns).
The array list adds a comma after every word, so I was thinking something like
find the nth/(column) comma
split the string
check if tts is speaking and listen for onUtterance onDone to pass new string
read the string
move the view
and keep doing this until theres no words left and coding for the remainder % im not sure how to do all of that so if anyone wants to help feel free, (I think Im looking at StringUtils and creating each smaller string with a for loop and passing them to the tts in onUtteranceListener onDone, but im still a little new to android), but mainly does anyone have a better way
okay this is what i have so far but it could probably use some work im still an amateur, I've split the string into blocks based on the size of the screen, and the handler that scrolls the screen relies on the amount of letters in each broken up string, also the smoothscrolltoposition is scrolling a custom layout manager that scrolls pretty slowly im having an issue though where on some devices the array list counting the words will only reach 20 not sure why but ill ask and hopefully update this when ive fixed it so here is my speak and move method
public void speakAndMove(){
final ArrayList<String> list = new ArrayList<>();
SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(0);
for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) {
list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
}
Integer numOfWords = list.size();
words = list.toString();
Integer count = 0;
Integer startPoint = 0;
scrollPos = 0;
final Integer speed = words.length() * 15;
Integer leftOver = 0;
final int columns = getResources().getInteger(R.integer.grid_columns);
System.out.println(numOfWords);
ArrayList<String> arrayList = new ArrayList<>();
if (list.size() <= columns) {
if (words.contains("[]")) {
speakWords("");
} else if (words.contains(", 's")) {
formatString = words.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (words.contains(", ing")) {
formatString = words.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(words);
}
}
if (list.size()>=columns) {
for (int i = 0; i < words.length(); i++) {
if (words.charAt(i) == ',') {
count++;
if (count == columns) {
String ab = words.substring(startPoint, i + 1);
//speakWords(ab);
if (ab.contains(", 's")) {
formatString = ab.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (ab.contains(", ing")) {
formatString = ab.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(ab);
}
startPoint = i + 1;
count = 0;
leftOver = words.length() - startPoint;
}
//SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
System.out.println("main string"+" scroll " + scrollPos + " startpoint " + startPoint +" speed " + speed);
}
}
if (leftOver > 0) {
String ab2 = words.substring(startPoint, words.length());
//speakWords(ab2);
if (ab2.contains(", 's")) {
formatString = ab2.replaceFirst(", 's", "'s");
speakWords(formatString);
} else if (ab2.contains(", ing")) {
formatString = ab2.replaceFirst(", ing", "ing");
speakWords(formatString);
} else {
speakWords(ab2);
}
//SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
System.out.println("leftovers "+ leftOver + " scroll " + scrollPos + " startpoint " + startPoint +" count " + scrollPos);
count = 0;
//scrollPos = 0;
}
}
final Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
scrollPos = scrollPos + columns;
SpeakGrid.recyclerView.getLayoutManager().smoothScrollToPosition(SpeakGrid.recyclerView, null ,scrollPos);
System.out.println("position "+ scrollPos + " speed " + speed + " list size " + list.size());
if (scrollPos < list.size())
h.postDelayed(this,speed);
// close this activity
}
}, speed);
}