I have an array of buttons in my Android app. I want pressing a button to cause a different button to turn white. I have code like this:
final Button [][] button = new Button[5][3];
for(int i = 0; i < tableRow.length; i++) {
for(int j = 0; j < button[i].length; j++) {
button[i][j] = new Button(this);
button[i][j].getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0xFF000000));
}
}
button[0][0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button[0][0].getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x00FFFFFF));
}
});
This works fine, but it turns button[0][0] white when it is pressed, while I want pressing button[0][0] to turn button[0][1] white. If I change it to this:
button[0][0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button[0][1].getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x00FFFFFF));
}
});
nothing happens when I press button[0][0]. Why?
button[0][1].invalidate();
will force the view to get redrawn.
Related
I have created an ImageView dynamically like below image.It works fine. Now I want to remove view when top cross ImageView is clicked. When I click, it crashes .Please help how to achieve it.
here is what i have done
private void postImage(List<Uri> urilist) {
for(int i=0; i< urilist.size(); i++) {
imgView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(150, 150);
lp.setMargins(20,10,20,10);
imgView.setLayoutParams(lp);
imgView.setId(i);
Log.d("uri list in loop",""+urilist.get(0));
Glide.with(getActivity())
.load(urilist.get(i))
.into(imgView);
layout.addView(imgView);
imgView1 = new ImageView(getActivity());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(50, 50);
lp1.setMargins(0,5,1,80);
imgView1.setLayoutParams(lp1);
imgView1.setId(i);
Log.d("uri list in loop",""+urilist.get(0));
Glide.with(getActivity())
.load(R.drawable.ic_action_cross)
.into(imgView1);
layout.addView(imgView1);
}
imgView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.removeViewAt(v.getId());
}
});
}
If it is because of the index (which definitely will crash in deletion of the 2nd item) then you can try below
imgView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewGroup parentView = (ViewGroup) v.getParent();
parentView.removeView(v);
}
});
Note: You should not set id of two views as same. Rather use some mathematical formula.
I mean #shakac, you can try like that;
for(int i = 0; i<layout.getChildCount(); i++)
{
if (layout.getChildAt(i).getId() == v.getId()){
layout.removeView(layout.getChildAt(i));
break;
}
}
But as I said on the comment, you will remove the cross button like that.
I have a set of ImageView in which some of them are red colored blink for some time like below.
I want to do that ,When I'm click on colored blinked imageViews it change as tic mark and when I'm click non blinked imageViews it change as cross mark.
My issue is my current code only tic the one imageview and all other imageViews are cross marked.
So how to make tic mark more than one ImageView.
Here is my code:
org_id = new int[]{R.id.img1_1, R.id.img1_2, R.id.img1_3, R.id.img1_4};
all = new int[]{R.id.img1_1, R.id.img1_2, R.id.img1_3, R.id.img1_4};
Random random = new Random();
for(int j=0;j<2;j++) {
id = all[random.nextInt(all.length)];
ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundResource", R.drawable.new_stateimg, R.drawable.org_state).setDuration(2000);
Toast.makeText(Game.this, "index" + findViewById(id), Toast.LENGTH_LONG).show();
animator.setEvaluator(new ArgbEvaluator());
animator.start();
for (int i=0; i < org_id.length; ++i) {
final int btn = org_id[i];
findViewById(btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ((findViewById(id)).equals(findViewById(btn)))
{
findViewById(id).setBackgroundResource(R.drawable.correct);
} else {
Toast.makeText(Game.this, "wrong", Toast.LENGTH_SHORT).show();
findViewById(btn).setBackgroundResource(R.drawable.cross);
}
}
});
}
}
Compare your id like
if (id.getId() == btn.getId())
Do it in your code.
public void onClick(View view) {
ImageView imgView = (ImageView)view; //edited
if(imgView.getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.correct).getConstantState()))
imgView.setBackgroundResource(R.drawable.incorrect);//set here you incorrect image which you want.
else
imgView.setBackgroundResource(R.drawable.correct);//set here your correct image
}
I have a button that moves around the screen and changes image and every 5th time it's pressed I want it to also do an additional function (print a command). I can't figure out how to only trigger an event every 5th button press though? I have tried counting with an int but I am getting errors about then referring to the int in the nested if condition. I had tried playing around with the final prefix but can't figure out how to get it working so that I may edit the variable both outside and inside the if condition.
Error:(58, 20) error: local variable i is accessed from within inner class; needs to be declared final
public void addListenerOnButton() {
int i=0;
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
;
//Move position
Random r = new Random();
int buttonHeight;
int buttonWidth;
int xPos = r.nextInt(480);
int yPos = r.nextInt(800);
imageButton.setX(xPos);
imageButton.setY(yPos);
//Change image
int[] imageIds = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
R.drawable.pic9,
R.drawable.pic10
};
int randomImageId = imageIds[r.nextInt(imageIds.length)];
imageButton.setImageResource(randomImageId);
if(i==5){
Toast offMsg = Toast.makeText(getBaseContext(),
"This is the fifth button press", Toast.LENGTH_SHORT);
offMsg.show();
int i=0;
}
}
});
}
}
Declare your counter variable inside the OnClickListener:
imageButton.setOnClickListener(new OnClickListener() {
int i = 0;
#Override
public void onClick(View arg0) {
...
if(++i == 5){
Toast offMsg = Toast.makeText(getBaseContext(),
"This is the fifth button press", Toast.LENGTH_SHORT);
offMsg.show();
i=0;
}
}
});
You get that error because you can't access local variables from an inner class.
Declare your variable 'i' as an instance variable i.e. define it outside addListenerOnButton() method and remove the int i = 0 declaration from addListenerOnButton() method and also change the int i = 0 line after offMsg.show() to i = 0.
You also need to increment the variable 'i' every time the image is clicked.
I am trying to build an android app that gets some questions from a database and its possible answers (the answers are created dynamically as UI buttons). What I have managed so far is that once the user clicks on an answer, if the answer is right the button's colour becomes green, and if the answer is wrong the colour becomes red. What I want to achieve is in case the answer was wrong, change the button's colour to red and find the button with the correct answer and change it's colour to green. I am currently trying to do this by looping through every child element of the clicked button's parent, and comparing its text with the right answer given from the database. This is what my code looks like:
final TableRow textRow = new TableRow(this);
textRow.addView(questionText, rowParamsQuestions);
layout.addView(textRow, layoutParams);
for(final String option : currentQuestion.getOptions())
{
final Button button = new Button(this);
button.setText(option);
button.setTextSize(16);
button.setBackgroundResource(R.drawable.rounded_shape);
button.setTextColor(Color.WHITE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button clicked = (Button) v;
if (!answeredQuestions.containsKey(currentQuestion)) {
answeredQuestions.put(currentQuestion, clicked.getText().toString());
if (clicked.getText().equals(currentQuestion.getAnswer())) {
clicked.setBackgroundResource(R.drawable.right_answer);
} else {
clicked.setBackgroundResource(R.drawable.wrong_answer);
for (int i = 0; i < textRow.getChildCount(); i++) {
View child = textRow.getChildAt(i);
if (child instanceof Button) {
if (((Button) child).getText().equals(currentQuestion.getAnswer())) {
child.setBackgroundResource(R.drawable.right_answer);
}
}
}
}
Thank you very much!
I have an array of bitmaps ArrayList<Bitmap> images = new ArrayList<>(3) and need to change the image of a ImageButton in every click, first images(0) then, after I click image(1) and finally image(2).
For this I am using myImageButton.setImageBitmap(images.get(0)) for the first image, how do I change to the next and then to the third?
You should use
int currentPos = 0;
onclick(){
currentPos = (currentPos+1)%(images.size()-1)
myImageButton.setImageBitmap(images.get(currentPos))
}
You can use
int currentPos = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
myImageButton.setImageBitmap(images.get(0))
}
#Override
public void onClick(View v){
currentPos++;
if(currentPos == 2){
// if the imagebutton has the bitmap from position 2, you can do what you want
//example:
currentPos = 0;// so you have the first Bitmap you had when the user didn't click on the ImageButton
}
myImageButton.setImageBitmap(images.get(currentPos));
}